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
58
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum EntitlementType {
    /// Entitlement was purchased as an app subscription.
    ApplicationSubscription,
    Unknown(u8),
}

impl From<u8> for EntitlementType {
    fn from(value: u8) -> Self {
        match value {
            8 => Self::ApplicationSubscription,
            other => Self::Unknown(other),
        }
    }
}

impl From<EntitlementType> for u8 {
    fn from(value: EntitlementType) -> Self {
        match value {
            EntitlementType::ApplicationSubscription => 8,
            EntitlementType::Unknown(other) => other,
        }
    }
}

impl EntitlementType {
    pub const fn name(self) -> &'static str {
        match self {
            Self::ApplicationSubscription => "ApplicationSubscription",
            Self::Unknown(_) => "Unknown",
        }
    }
}

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

    #[test]
    fn variants() {
        serde_test::assert_tokens(&EntitlementType::ApplicationSubscription, &[Token::U8(8)]);
        serde_test::assert_tokens(&EntitlementType::Unknown(99), &[Token::U8(99)]);
    }

    #[test]
    fn names() {
        assert_eq!(
            EntitlementType::ApplicationSubscription.name(),
            "ApplicationSubscription"
        );
        assert_eq!(EntitlementType::Unknown(99).name(), "Unknown");
    }
}