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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
use serde::{Deserialize, Serialize};

// Keep in sync with `twilight-validate::command`!
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum CommandType {
    /// Slash command.
    ///
    /// Text-based command that appears when a user types `/`.
    ChatInput,
    /// UI-based command.
    ///
    /// Appears when a user right clicks or taps on a user.
    User,
    /// UI-based command.
    ///
    /// Appears when a user right clicks or taps on a message.
    Message,
    /// Variant value is unknown to the library.
    Unknown(u8),
}

impl CommandType {
    pub const fn kind(self) -> &'static str {
        match self {
            Self::ChatInput => "ChatInput",
            Self::User => "User",
            Self::Message => "Message",
            Self::Unknown(_) => "Unknown",
        }
    }
}

impl From<u8> for CommandType {
    fn from(value: u8) -> Self {
        match value {
            1 => Self::ChatInput,
            2 => Self::User,
            3 => Self::Message,
            unknown => Self::Unknown(unknown),
        }
    }
}

impl From<CommandType> for u8 {
    fn from(value: CommandType) -> Self {
        match value {
            CommandType::ChatInput => 1,
            CommandType::User => 2,
            CommandType::Message => 3,
            CommandType::Unknown(unknown) => unknown,
        }
    }
}

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

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

    #[test]
    fn variants() {
        serde_test::assert_tokens(&CommandType::ChatInput, &[Token::U8(1)]);
        serde_test::assert_tokens(&CommandType::User, &[Token::U8(2)]);
        serde_test::assert_tokens(&CommandType::Message, &[Token::U8(3)]);
        serde_test::assert_tokens(&CommandType::Unknown(99), &[Token::U8(99)]);
    }

    #[test]
    fn kinds() {
        assert_eq!("ChatInput", CommandType::ChatInput.kind());
        assert_eq!("User", CommandType::User.kind());
        assert_eq!("Message", CommandType::Message.kind());
        assert_eq!("Unknown", CommandType::Unknown(99).kind());
    }
}