twilight_model/application/interaction/
interaction_type.rs1use serde_repr::{Deserialize_repr, Serialize_repr};
2use std::fmt::{Display, Formatter, Result as FmtResult};
3
4#[derive(Clone, Copy, Debug, Deserialize_repr, Eq, Hash, PartialEq, Serialize_repr)]
10#[non_exhaustive]
11#[repr(u8)]
12pub enum InteractionType {
13 Ping = 1,
19 ApplicationCommand = 2,
21 MessageComponent = 3,
25 ApplicationCommandAutocomplete = 4,
27 ModalSubmit = 5,
29}
30
31impl InteractionType {
32 pub const fn kind(self) -> &'static str {
33 match self {
34 Self::Ping => "Ping",
35 Self::ApplicationCommand => "ApplicationCommand",
36 Self::MessageComponent => "MessageComponent",
37 Self::ApplicationCommandAutocomplete => "ApplicationCommandAutocomplete",
38 Self::ModalSubmit => "ModalSubmit",
39 }
40 }
41}
42
43#[derive(Debug)]
44pub struct UnknownInteractionTypeError {
45 value: u8,
46}
47
48impl Display for UnknownInteractionTypeError {
49 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
50 f.write_str("unknown interaction type: ")?;
51
52 Display::fmt(&self.value, f)
53 }
54}
55
56impl TryFrom<u8> for InteractionType {
57 type Error = UnknownInteractionTypeError;
58
59 fn try_from(i: u8) -> Result<Self, Self::Error> {
60 match i {
61 1 => Ok(Self::Ping),
62 2 => Ok(Self::ApplicationCommand),
63 3 => Ok(Self::MessageComponent),
64 4 => Ok(Self::ApplicationCommandAutocomplete),
65 5 => Ok(Self::ModalSubmit),
66 other => Err(UnknownInteractionTypeError { value: other }),
67 }
68 }
69}
70
71#[cfg(test)]
72mod tests {
73 use super::{InteractionType, UnknownInteractionTypeError};
74 use serde::{Deserialize, Serialize};
75 use static_assertions::{assert_impl_all, const_assert_eq};
76 use std::{fmt::Debug, hash::Hash};
77
78 assert_impl_all!(
79 InteractionType: Clone,
80 Copy,
81 Debug,
82 Deserialize<'static>,
83 Eq,
84 Hash,
85 PartialEq,
86 Serialize,
87 Send,
88 Sync
89 );
90 const_assert_eq!(1, InteractionType::Ping as u8);
91 const_assert_eq!(2, InteractionType::ApplicationCommand as u8);
92 const_assert_eq!(3, InteractionType::MessageComponent as u8);
93 const_assert_eq!(4, InteractionType::ApplicationCommandAutocomplete as u8);
94 const_assert_eq!(5, InteractionType::ModalSubmit as u8);
95
96 #[test]
97 fn kind() {
98 assert_eq!("Ping", InteractionType::Ping.kind());
99 assert_eq!(
100 "ApplicationCommand",
101 InteractionType::ApplicationCommand.kind()
102 );
103 assert_eq!("MessageComponent", InteractionType::MessageComponent.kind());
104 assert_eq!(
105 "ApplicationCommandAutocomplete",
106 InteractionType::ApplicationCommandAutocomplete.kind()
107 );
108 assert_eq!("ModalSubmit", InteractionType::ModalSubmit.kind());
109 }
110
111 #[test]
112 fn try_from() -> Result<(), UnknownInteractionTypeError> {
113 assert_eq!(InteractionType::Ping, InteractionType::try_from(1)?);
114 assert_eq!(
115 InteractionType::ApplicationCommand,
116 InteractionType::try_from(2)?
117 );
118 assert_eq!(
119 InteractionType::MessageComponent,
120 InteractionType::try_from(3)?
121 );
122 assert_eq!(
123 InteractionType::ApplicationCommandAutocomplete,
124 InteractionType::try_from(4)?,
125 );
126 assert_eq!(InteractionType::ModalSubmit, InteractionType::try_from(5)?);
127 assert!(InteractionType::try_from(u8::MAX).is_err());
128
129 Ok(())
130 }
131}