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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fmt::{Display, Formatter, Result as FmtResult};
#[derive(Clone, Copy, Debug, Deserialize_repr, Eq, Hash, PartialEq, Serialize_repr)]
#[non_exhaustive]
#[repr(u8)]
pub enum InteractionType {
Ping = 1,
ApplicationCommand = 2,
MessageComponent = 3,
ApplicationCommandAutocomplete = 4,
ModalSubmit = 5,
}
impl InteractionType {
pub const fn kind(self) -> &'static str {
match self {
Self::Ping => "Ping",
Self::ApplicationCommand => "ApplicationCommand",
Self::MessageComponent => "MessageComponent",
Self::ApplicationCommandAutocomplete => "ApplicationCommandAutocomplete",
Self::ModalSubmit => "ModalSubmit",
}
}
}
#[derive(Debug)]
pub struct UnknownInteractionTypeError {
value: u8,
}
impl Display for UnknownInteractionTypeError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
f.write_str("unknown interaction type: ")?;
Display::fmt(&self.value, f)
}
}
impl TryFrom<u8> for InteractionType {
type Error = UnknownInteractionTypeError;
fn try_from(i: u8) -> Result<Self, Self::Error> {
match i {
1 => Ok(Self::Ping),
2 => Ok(Self::ApplicationCommand),
3 => Ok(Self::MessageComponent),
4 => Ok(Self::ApplicationCommandAutocomplete),
5 => Ok(Self::ModalSubmit),
other => Err(UnknownInteractionTypeError { value: other }),
}
}
}
#[cfg(test)]
mod tests {
use super::{InteractionType, UnknownInteractionTypeError};
use serde::{Deserialize, Serialize};
use static_assertions::{assert_impl_all, const_assert_eq};
use std::{fmt::Debug, hash::Hash};
assert_impl_all!(
InteractionType: Clone,
Copy,
Debug,
Deserialize<'static>,
Eq,
Hash,
PartialEq,
Serialize,
Send,
Sync
);
const_assert_eq!(1, InteractionType::Ping as u8);
const_assert_eq!(2, InteractionType::ApplicationCommand as u8);
const_assert_eq!(3, InteractionType::MessageComponent as u8);
const_assert_eq!(4, InteractionType::ApplicationCommandAutocomplete as u8);
const_assert_eq!(5, InteractionType::ModalSubmit as u8);
#[test]
fn kind() {
assert_eq!("Ping", InteractionType::Ping.kind());
assert_eq!(
"ApplicationCommand",
InteractionType::ApplicationCommand.kind()
);
assert_eq!("MessageComponent", InteractionType::MessageComponent.kind());
assert_eq!(
"ApplicationCommandAutocomplete",
InteractionType::ApplicationCommandAutocomplete.kind()
);
assert_eq!("ModalSubmit", InteractionType::ModalSubmit.kind());
}
#[test]
fn try_from() -> Result<(), UnknownInteractionTypeError> {
assert_eq!(InteractionType::Ping, InteractionType::try_from(1)?);
assert_eq!(
InteractionType::ApplicationCommand,
InteractionType::try_from(2)?
);
assert_eq!(
InteractionType::MessageComponent,
InteractionType::try_from(3)?
);
assert_eq!(
InteractionType::ApplicationCommandAutocomplete,
InteractionType::try_from(4)?,
);
assert_eq!(InteractionType::ModalSubmit, InteractionType::try_from(5)?);
assert!(InteractionType::try_from(u8::MAX).is_err());
Ok(())
}
}