twilight_model/application/command/
command_type.rs

1use serde::{Deserialize, Serialize};
2
3// Keep in sync with `twilight-validate::command`!
4#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
5#[non_exhaustive]
6#[serde(from = "u8", into = "u8")]
7pub enum CommandType {
8    /// Slash command.
9    ///
10    /// Text-based command that appears when a user types `/`.
11    ChatInput,
12    /// UI-based command.
13    ///
14    /// Appears when a user right clicks or taps on a user.
15    User,
16    /// UI-based command.
17    ///
18    /// Appears when a user right clicks or taps on a message.
19    Message,
20    /// Variant value is unknown to the library.
21    Unknown(u8),
22}
23
24impl CommandType {
25    pub const fn kind(self) -> &'static str {
26        match self {
27            Self::ChatInput => "ChatInput",
28            Self::User => "User",
29            Self::Message => "Message",
30            Self::Unknown(_) => "Unknown",
31        }
32    }
33}
34
35impl From<u8> for CommandType {
36    fn from(value: u8) -> Self {
37        match value {
38            1 => Self::ChatInput,
39            2 => Self::User,
40            3 => Self::Message,
41            unknown => Self::Unknown(unknown),
42        }
43    }
44}
45
46impl From<CommandType> for u8 {
47    fn from(value: CommandType) -> Self {
48        match value {
49            CommandType::ChatInput => 1,
50            CommandType::User => 2,
51            CommandType::Message => 3,
52            CommandType::Unknown(unknown) => unknown,
53        }
54    }
55}
56
57#[cfg(test)]
58mod tests {
59    use super::CommandType;
60    use serde::{Deserialize, Serialize};
61    use serde_test::Token;
62    use static_assertions::assert_impl_all;
63    use std::{fmt::Debug, hash::Hash};
64
65    assert_impl_all!(
66        CommandType: Clone,
67        Copy,
68        Debug,
69        Deserialize<'static>,
70        Eq,
71        Hash,
72        PartialEq,
73        Serialize,
74        Send,
75        Sync
76    );
77
78    #[test]
79    fn variants() {
80        serde_test::assert_tokens(&CommandType::ChatInput, &[Token::U8(1)]);
81        serde_test::assert_tokens(&CommandType::User, &[Token::U8(2)]);
82        serde_test::assert_tokens(&CommandType::Message, &[Token::U8(3)]);
83        serde_test::assert_tokens(&CommandType::Unknown(99), &[Token::U8(99)]);
84    }
85
86    #[test]
87    fn kinds() {
88        assert_eq!("ChatInput", CommandType::ChatInput.kind());
89        assert_eq!("User", CommandType::User.kind());
90        assert_eq!("Message", CommandType::Message.kind());
91        assert_eq!("Unknown", CommandType::Unknown(99).kind());
92    }
93}