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

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum InviteType {
    Guild,
    GroupDm,
    Friend,
    Unknown(u8),
}

impl From<u8> for InviteType {
    fn from(value: u8) -> Self {
        match value {
            0 => Self::Guild,
            1 => Self::GroupDm,
            2 => Self::Friend,
            unknown => Self::Unknown(unknown),
        }
    }
}

impl From<InviteType> for u8 {
    fn from(value: InviteType) -> Self {
        match value {
            InviteType::Guild => 0,
            InviteType::GroupDm => 1,
            InviteType::Friend => 2,
            InviteType::Unknown(unknown) => unknown,
        }
    }
}

impl InviteType {
    pub const fn name(&self) -> &str {
        match self {
            Self::Guild => "Guild",
            Self::GroupDm => "Group",
            Self::Friend => "Friend",
            Self::Unknown(_) => "Unknown",
        }
    }
}

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

    #[test]
    fn variants() {
        serde_test::assert_tokens(&InviteType::Guild, &[Token::U8(0)]);
        serde_test::assert_tokens(&InviteType::GroupDm, &[Token::U8(1)]);
        serde_test::assert_tokens(&InviteType::Friend, &[Token::U8(2)]);
        serde_test::assert_tokens(&InviteType::Unknown(99), &[Token::U8(99)]);
    }

    #[test]
    fn names() {
        assert_eq!(InviteType::Guild.name(), "Guild");
        assert_eq!(InviteType::GroupDm.name(), "Group");
        assert_eq!(InviteType::Friend.name(), "Friend");
        assert_eq!(InviteType::Unknown(99).name(), "Unknown");
    }
}