twilight_model/user/
premium_type.rs

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use serde::{Deserialize, Serialize};

/// Type of premium tier for a [`User`].
///
/// [`User`]: super::User
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum PremiumType {
    /// User doesn't have premium.
    None,
    /// User has Nitro Classic.
    NitroClassic,
    /// User has the standard Nitro.
    Nitro,
    /// User has Nitro Basic.
    NitroBasic,
    /// Variant value is unknown to the library.
    Unknown(u8),
}

impl From<u8> for PremiumType {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::None,
            1 => Self::NitroClassic,
            2 => Self::Nitro,
            3 => Self::NitroBasic,
            unknown => PremiumType::Unknown(unknown),
        }
    }
}

impl From<PremiumType> for u8 {
    fn from(value: PremiumType) -> Self {
        match value {
            PremiumType::None => 0,
            PremiumType::NitroClassic => 1,
            PremiumType::Nitro => 2,
            PremiumType::NitroBasic => 3,
            PremiumType::Unknown(unknown) => unknown,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::PremiumType;
    use serde::{Deserialize, Serialize};
    use serde_test::Token;
    use static_assertions::assert_impl_all;
    use std::{fmt::Debug, hash::Hash};

    assert_impl_all!(
        PremiumType: Clone,
        Copy,
        Debug,
        Deserialize<'static>,
        Eq,
        Hash,
        PartialEq,
        Send,
        Serialize,
        Sync
    );

    #[test]
    fn variants() {
        serde_test::assert_tokens(&PremiumType::None, &[Token::U8(0)]);
        serde_test::assert_tokens(&PremiumType::NitroClassic, &[Token::U8(1)]);
        serde_test::assert_tokens(&PremiumType::Nitro, &[Token::U8(2)]);
        serde_test::assert_tokens(&PremiumType::NitroBasic, &[Token::U8(3)]);
        serde_test::assert_tokens(&PremiumType::Unknown(42), &[Token::U8(42)]);
    }
}