Skip to content

Commit 5230695

Browse files
committed
Make all the incoming payloads able to handle unknown variants
1 parent e898ef6 commit 5230695

38 files changed

+1127
-519
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
target/
22
Cargo.lock
3+
idea

http/src/request/guild/get_audit_log.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ struct GetAuditLogFields {
4545
///
4646
/// for entry in audit_log.entries {
4747
/// println!("ID: {}", entry.id);
48-
/// println!(" Action Type: {}", entry.action_type as u8);
48+
/// println!(" Action Type: {}", u16::from(entry.action_type));
4949
/// println!(" Changes:");
5050
///
5151
/// for change in entry.changes {
@@ -134,7 +134,7 @@ impl<'a> GetAuditLog<'a> {
134134
impl TryIntoRequest for GetAuditLog<'_> {
135135
fn try_into_request(self) -> Result<Request, HttpError> {
136136
Ok(Request::from_route(&Route::GetAuditLogs {
137-
action_type: self.fields.action_type.map(|x| x as u64),
137+
action_type: self.fields.action_type.map(|x| u64::from(u16::from(x))),
138138
before: self.fields.before,
139139
guild_id: self.guild_id.get(),
140140
limit: self.fields.limit,

model/src/application/command/command_type.rs

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
1-
use serde_repr::{Deserialize_repr, Serialize_repr};
1+
use serde::{Deserialize, Serialize};
22

3-
#[derive(Clone, Copy, Debug, Deserialize_repr, Eq, Hash, PartialEq, Serialize_repr)]
4-
#[repr(u8)]
3+
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
4+
#[serde(from = "u8", into = "u8")]
55
pub enum CommandType {
66
/// Slash command.
77
///
88
/// Text-based command that appears when a user types `/`.
9-
ChatInput = 1,
9+
ChatInput,
1010
/// UI-based command.
1111
///
1212
/// Appears when a user right clicks or taps om a user.
13-
User = 2,
13+
User,
1414
/// UI-based command.
1515
///
1616
/// Appears when a user right clicks or taps on a message.
17-
Message = 3,
17+
Message,
18+
/// Not yet known/supported
19+
Unknown(u8),
1820
}
1921

2022
impl CommandType {
@@ -23,6 +25,29 @@ impl CommandType {
2325
Self::ChatInput => "ChatInput",
2426
Self::User => "User",
2527
Self::Message => "Message",
28+
CommandType::Unknown(_) => "Unknown",
29+
}
30+
}
31+
}
32+
33+
impl From<u8> for CommandType {
34+
fn from(value: u8) -> Self {
35+
match value {
36+
1 => Self::ChatInput,
37+
2 => Self::User,
38+
3 => Self::Message,
39+
unknown => Self::Unknown(unknown),
40+
}
41+
}
42+
}
43+
44+
impl From<CommandType> for u8 {
45+
fn from(value: CommandType) -> Self {
46+
match value {
47+
CommandType::ChatInput => 1,
48+
CommandType::User => 2,
49+
CommandType::Message => 3,
50+
CommandType::Unknown(unknown) => unknown,
2651
}
2752
}
2853
}
@@ -53,12 +78,14 @@ mod tests {
5378
serde_test::assert_tokens(&CommandType::ChatInput, &[Token::U8(1)]);
5479
serde_test::assert_tokens(&CommandType::User, &[Token::U8(2)]);
5580
serde_test::assert_tokens(&CommandType::Message, &[Token::U8(3)]);
81+
serde_test::assert_tokens(&CommandType::Unknown(4), &[Token::U8(4)]);
5682
}
5783

5884
#[test]
5985
fn test_kinds() {
6086
assert_eq!("ChatInput", CommandType::ChatInput.kind());
6187
assert_eq!("User", CommandType::User.kind());
6288
assert_eq!("Message", CommandType::Message.kind());
89+
assert_eq!("Unknown", CommandType::Unknown(99).kind());
6390
}
6491
}

model/src/application/component/button.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::channel::ReactionType;
2-
use serde_repr::{Deserialize_repr, Serialize_repr};
2+
use serde::{Deserialize, Serialize};
33

44
/// Clickable interactive components that render on messages.
55
///
@@ -36,34 +36,62 @@ pub struct Button {
3636
/// Refer to [the Discord Docs/Button Object] for additional information.
3737
///
3838
/// [the Discord Docs/Button Object]: https://discord.com/developers/docs/interactions/message-components#button-object-button-styles
39-
#[derive(Clone, Copy, Debug, Deserialize_repr, Eq, Hash, PartialEq, PartialOrd, Serialize_repr)]
40-
#[repr(u8)]
39+
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, PartialOrd, Serialize)]
40+
#[serde(from = "u8", into = "u8")]
4141
pub enum ButtonStyle {
4242
/// Button indicates a primary action.
4343
///
4444
/// Selecting this button style requires specifying the
4545
/// [`Button::custom_id`] field.
46-
Primary = 1,
46+
Primary,
4747
/// Button indicates a secondary action.
4848
///
4949
/// Selecting this button style requires specifying the
5050
/// [`Button::custom_id`] field.
51-
Secondary = 2,
51+
Secondary,
5252
/// Button indicates a successful action.
5353
///
5454
/// Selecting this button style requires specifying the
5555
/// [`Button::custom_id`] field.
56-
Success = 3,
56+
Success,
5757
/// Button indicates a dangerous action.
5858
///
5959
/// Selecting this button style requires specifying the
6060
/// [`Button::custom_id`] field.
61-
Danger = 4,
61+
Danger,
6262
/// Button indicates an action with a link.
6363
///
6464
/// Selecting this button style requires specifying the [`Button::url`]
6565
/// field.
66-
Link = 5,
66+
Link,
67+
/// Variant value is unknown to the library.
68+
Unknown(u8),
69+
}
70+
71+
impl From<u8> for ButtonStyle {
72+
fn from(value: u8) -> Self {
73+
match value {
74+
1 => ButtonStyle::Primary,
75+
2 => ButtonStyle::Secondary,
76+
3 => ButtonStyle::Success,
77+
4 => ButtonStyle::Danger,
78+
5 => ButtonStyle::Link,
79+
unknown => ButtonStyle::Unknown(unknown),
80+
}
81+
}
82+
}
83+
84+
impl From<ButtonStyle> for u8 {
85+
fn from(value: ButtonStyle) -> Self {
86+
match value {
87+
ButtonStyle::Primary => 1,
88+
ButtonStyle::Secondary => 2,
89+
ButtonStyle::Success => 3,
90+
ButtonStyle::Danger => 4,
91+
ButtonStyle::Link => 5,
92+
ButtonStyle::Unknown(unknown) => unknown,
93+
}
94+
}
6795
}
6896

6997
#[cfg(test)]
@@ -72,7 +100,7 @@ mod tests {
72100
use super::*;
73101
use serde::{Deserialize, Serialize};
74102
use serde_test::Token;
75-
use static_assertions::{assert_fields, assert_impl_all, const_assert_eq};
103+
use static_assertions::{assert_fields, assert_impl_all};
76104
use std::{fmt::Debug, hash::Hash};
77105

78106
assert_fields!(Button: custom_id, disabled, emoji, label, style, url);
@@ -91,11 +119,6 @@ mod tests {
91119
Serialize,
92120
Sync
93121
);
94-
const_assert_eq!(1, ButtonStyle::Primary as u8);
95-
const_assert_eq!(2, ButtonStyle::Secondary as u8);
96-
const_assert_eq!(3, ButtonStyle::Success as u8);
97-
const_assert_eq!(4, ButtonStyle::Danger as u8);
98-
const_assert_eq!(5, ButtonStyle::Link as u8);
99122

100123
#[test]
101124
fn test_button_style() {
@@ -104,5 +127,6 @@ mod tests {
104127
serde_test::assert_tokens(&ButtonStyle::Success, &[Token::U8(3)]);
105128
serde_test::assert_tokens(&ButtonStyle::Danger, &[Token::U8(4)]);
106129
serde_test::assert_tokens(&ButtonStyle::Link, &[Token::U8(5)]);
130+
serde_test::assert_tokens(&ButtonStyle::Unknown(6), &[Token::U8(6)]);
107131
}
108132
}

model/src/application/component/kind.rs

Lines changed: 40 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,60 @@
1-
use serde_repr::{Deserialize_repr, Serialize_repr};
1+
use serde::{Deserialize, Serialize};
22
use std::fmt::{Display, Formatter, Result as FmtResult};
33

44
/// Type of Component.
55
///
66
/// See [Discord Docs/Message Components].
77
///
88
/// [Discord Docs/Message Components]: https://discord.com/developers/docs/interactions/message-components#component-types
9-
#[derive(Clone, Copy, Debug, Deserialize_repr, Eq, Hash, PartialEq, Serialize_repr)]
10-
#[repr(u8)]
9+
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
10+
#[serde(from = "u8", into = "u8")]
1111
pub enum ComponentType {
1212
/// Component is an [`ActionRow`].
1313
///
1414
/// [`ActionRow`]: super::ActionRow
15-
ActionRow = 1,
15+
ActionRow,
1616

1717
/// Component is an [`Button`].
1818
///
1919
/// [`Button`]: super::Button
20-
Button = 2,
20+
Button,
2121

2222
/// Component is an [`SelectMenu`].
2323
///
2424
/// [`SelectMenu`]: super::SelectMenu
25-
SelectMenu = 3,
25+
SelectMenu,
2626

2727
/// Component is an [`TextInput`].
2828
///
2929
/// [`TextInput`]: super::TextInput
30-
TextInput = 4,
30+
TextInput,
31+
32+
///Variant value is unknown to the library.
33+
Unknown(u8),
34+
}
35+
36+
impl From<u8> for ComponentType {
37+
fn from(value: u8) -> Self {
38+
match value {
39+
1 => ComponentType::ActionRow,
40+
2 => ComponentType::Button,
41+
3 => ComponentType::SelectMenu,
42+
4 => ComponentType::TextInput,
43+
unknown => ComponentType::Unknown(unknown),
44+
}
45+
}
46+
}
47+
48+
impl From<ComponentType> for u8 {
49+
fn from(value: ComponentType) -> Self {
50+
match value {
51+
ComponentType::ActionRow => 1,
52+
ComponentType::Button => 2,
53+
ComponentType::SelectMenu => 3,
54+
ComponentType::TextInput => 4,
55+
ComponentType::Unknown(unknown) => unknown,
56+
}
57+
}
3158
}
3259

3360
impl ComponentType {
@@ -52,6 +79,7 @@ impl ComponentType {
5279
Self::Button => "Button",
5380
Self::SelectMenu => "SelectMenu",
5481
Self::TextInput => "TextInput",
82+
Self::Unknown(_) => "Unknown",
5583
}
5684
}
5785
}
@@ -67,7 +95,7 @@ mod tests {
6795
use super::*;
6896
use serde::{Deserialize, Serialize};
6997
use serde_test::Token;
70-
use static_assertions::{assert_impl_all, const_assert_eq};
98+
use static_assertions::assert_impl_all;
7199
use std::{fmt::Debug, hash::Hash};
72100

73101
assert_impl_all!(
@@ -82,21 +110,22 @@ mod tests {
82110
Serialize,
83111
Sync
84112
);
85-
const_assert_eq!(1, ComponentType::ActionRow as u8);
86-
const_assert_eq!(2, ComponentType::Button as u8);
87-
const_assert_eq!(3, ComponentType::SelectMenu as u8);
88113

89114
#[test]
90115
fn test_variants() {
91116
serde_test::assert_tokens(&ComponentType::ActionRow, &[Token::U8(1)]);
92117
serde_test::assert_tokens(&ComponentType::Button, &[Token::U8(2)]);
93118
serde_test::assert_tokens(&ComponentType::SelectMenu, &[Token::U8(3)]);
119+
serde_test::assert_tokens(&ComponentType::TextInput, &[Token::U8(4)]);
120+
serde_test::assert_tokens(&ComponentType::Unknown(99), &[Token::U8(99)]);
94121
}
95122

96123
#[test]
97124
fn test_names() {
98125
assert_eq!("ActionRow", ComponentType::ActionRow.name());
99126
assert_eq!("Button", ComponentType::Button.name());
100127
assert_eq!("SelectMenu", ComponentType::SelectMenu.name());
128+
assert_eq!("TextInput", ComponentType::TextInput.name());
129+
assert_eq!("Unknown", ComponentType::Unknown(99).name());
101130
}
102131
}

0 commit comments

Comments
 (0)