twilight_model/guild/
nsfw_level.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
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum NSFWLevel {
    Default,
    Explicit,
    Safe,
    AgeRestricted,
    Unknown(u8),
}

impl From<u8> for NSFWLevel {
    fn from(value: u8) -> Self {
        match value {
            0 => NSFWLevel::Default,
            1 => NSFWLevel::Explicit,
            2 => NSFWLevel::Safe,
            3 => NSFWLevel::AgeRestricted,
            unknown => NSFWLevel::Unknown(unknown),
        }
    }
}

impl From<NSFWLevel> for u8 {
    fn from(value: NSFWLevel) -> Self {
        match value {
            NSFWLevel::Default => 0,
            NSFWLevel::Explicit => 1,
            NSFWLevel::Safe => 2,
            NSFWLevel::AgeRestricted => 3,
            NSFWLevel::Unknown(unknown) => unknown,
        }
    }
}

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

    #[test]
    fn variants() {
        serde_test::assert_tokens(&NSFWLevel::Default, &[Token::U8(0)]);
        serde_test::assert_tokens(&NSFWLevel::Explicit, &[Token::U8(1)]);
        serde_test::assert_tokens(&NSFWLevel::Safe, &[Token::U8(2)]);
        serde_test::assert_tokens(&NSFWLevel::AgeRestricted, &[Token::U8(3)]);
        serde_test::assert_tokens(&NSFWLevel::Unknown(99), &[Token::U8(99)]);
    }
}