1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
/// Layout of a poll.
pub enum PollLayoutType {
    /// Default layout.
    Default,
    /// Unknown layout.
    Unknown(u8),
}

impl From<u8> for PollLayoutType {
    fn from(value: u8) -> Self {
        match value {
            1 => PollLayoutType::Default,
            unknown => PollLayoutType::Unknown(unknown),
        }
    }
}

impl From<PollLayoutType> for u8 {
    fn from(value: PollLayoutType) -> Self {
        match value {
            PollLayoutType::Default => 1,
            PollLayoutType::Unknown(unknown) => unknown,
        }
    }
}

impl PollLayoutType {
    pub const fn name(&self) -> &str {
        match self {
            PollLayoutType::Default => "Default",
            PollLayoutType::Unknown(_) => "Unknown",
        }
    }
}

#[cfg(test)]
mod tests {
    use super::PollLayoutType;
    use serde_test::Token;

    #[test]
    fn variants() {
        serde_test::assert_tokens(&PollLayoutType::Default, &[Token::U8(1)]);
        serde_test::assert_tokens(&PollLayoutType::Unknown(2), &[Token::U8(2)]);
    }

    #[test]
    fn names() {
        assert_eq!(PollLayoutType::Default.name(), "Default");
        assert_eq!(PollLayoutType::Unknown(2).name(), "Unknown");
    }
}