Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(model): add invite type #2346

Merged
merged 3 commits into from
Oct 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 31 additions & 38 deletions twilight-http/src/response/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,44 +639,37 @@ mod tests {
use http_body_util::Full;
use twilight_model::guild::invite::Invite;

const COMPRESSED: [u8; 685] = [
3, 160, 2, 0, 228, 178, 169, 189, 190, 59, 251, 86, 18, 36, 232, 63, 98, 235, 98, 82,
176, 41, 41, 85, 35, 35, 17, 115, 161, 13, 136, 164, 79, 156, 11, 128, 135, 156, 152,
152, 180, 129, 13, 171, 193, 102, 195, 157, 71, 218, 254, 219, 18, 240, 113, 244, 96,
217, 32, 111, 115, 13, 223, 109, 125, 34, 131, 147, 3, 127, 186, 220, 156, 31, 172, 6,
165, 49, 2, 52, 31, 77, 219, 188, 30, 204, 209, 52, 35, 1, 124, 93, 154, 86, 97, 4, 63,
216, 40, 140, 0, 125, 199, 13, 172, 48, 176, 29, 26, 216, 190, 107, 7, 44, 64, 2, 216,
203, 78, 99, 4, 88, 126, 24, 232, 0, 9, 224, 60, 182, 114, 62, 96, 4, 24, 42, 223, 218,
75, 165, 246, 161, 37, 109, 47, 244, 67, 139, 186, 178, 210, 129, 148, 74, 239, 67,
166, 145, 0, 46, 112, 24, 16, 65, 191, 180, 45, 1, 84, 155, 245, 199, 70, 211, 12, 253,
11, 170, 169, 52, 235, 65, 43, 8, 107, 219, 213, 174, 178, 36, 11, 234, 218, 9, 153,
47, 253, 58, 96, 174, 242, 105, 237, 208, 10, 9, 224, 232, 58, 198, 8, 30, 177, 220,
228, 156, 139, 117, 124, 47, 202, 77, 206, 227, 181, 136, 243, 213, 38, 185, 225, 72,
0, 111, 249, 229, 106, 123, 197, 69, 177, 202, 57, 79, 5, 79, 227, 211, 75, 190, 70, 2,
152, 242, 219, 111, 166, 236, 120, 126, 149, 20, 69, 178, 77, 11, 36, 96, 51, 20, 175,
139, 251, 109, 87, 252, 234, 148, 231, 226, 134, 231, 201, 191, 100, 21, 151, 201, 54,
21, 103, 113, 201, 47, 168, 3, 252, 44, 143, 211, 164, 188, 23, 215, 249, 165, 59, 208,
248, 255, 36, 233, 77, 82, 114, 81, 236, 46, 227, 98, 131, 4, 48, 78, 147, 171, 184,
228, 107, 145, 172, 182, 41, 18, 192, 93, 206, 111, 18, 126, 139, 245, 140, 213, 246,
234, 234, 58, 77, 202, 123, 124, 38, 128, 57, 133, 137, 159, 125, 253, 33, 24, 129, 77,
0, 223, 101, 223, 152, 47, 177, 76, 173, 32, 188, 240, 67, 183, 213, 208, 105, 113,
124, 127, 144, 121, 3, 46, 10, 177, 88, 198, 113, 152, 204, 65, 26, 53, 27, 218, 43,
58, 221, 27, 24, 106, 224, 223, 75, 78, 76, 201, 235, 106, 168, 246, 19, 32, 95, 102,
3, 109, 179, 159, 228, 212, 232, 178, 95, 109, 221, 204, 213, 48, 41, 136, 119, 201,
49, 28, 140, 25, 231, 232, 228, 36, 7, 3, 142, 97, 33, 211, 131, 4, 35, 120, 252, 65,
141, 71, 228, 101, 59, 165, 52, 100, 212, 98, 148, 185, 174, 47, 70, 232, 233, 120,
126, 115, 174, 62, 232, 118, 132, 143, 198, 28, 64, 203, 145, 0, 234, 110, 248, 223,
152, 124, 65, 96, 51, 207, 243, 3, 230, 185, 142, 31, 184, 212, 182, 220, 94, 72, 178,
55, 48, 45, 179, 193, 63, 2, 112, 182, 198, 168, 231, 250, 97, 232, 50, 47, 176, 100,
72, 41, 219, 55, 144, 251, 97, 49, 129, 211, 5, 84, 227, 17, 181, 123, 141, 144, 122,
142, 27, 184, 14, 245, 125, 47, 100, 30, 38, 36, 114, 214, 112, 109, 207, 102, 142,
239, 91, 174, 19, 80, 230, 120, 34, 228, 90, 42, 154, 20, 95, 176, 140, 74, 26, 189,
240, 44, 210, 14, 175, 179, 83, 112, 168, 112, 157, 26, 192, 216, 201, 167, 69, 5, 182,
122, 90, 84, 165, 237, 240, 108, 205, 9, 188, 128, 134, 140, 81, 215, 113, 68, 114,
161, 245, 8, 11, 73, 137, 158, 53, 102, 179, 236, 155, 29, 44, 203, 41, 15, 255, 158,
255, 8, 96, 63, 215, 31, 24, 65, 45, 219, 89, 171, 208, 234, 16, 173, 63, 2, 65, 179,
58, 215, 1, 108, 54, 67, 211, 215, 3, 18, 64, 243, 53, 106, 140, 192, 250, 251, 51, 0,
3,
const COMPRESSED: [u8; 553] = [
27, 235, 4, 0, 44, 10, 99, 99, 102, 244, 145, 235, 87, 95, 83, 76, 203, 31, 27, 6, 65,
20, 107, 75, 245, 103, 243, 139, 3, 81, 204, 15, 49, 13, 177, 83, 150, 163, 53, 249,
217, 44, 58, 93, 125, 117, 56, 81, 249, 9, 5, 129, 64, 112, 146, 109, 175, 185, 252,
39, 174, 169, 143, 248, 160, 111, 79, 250, 15, 22, 21, 139, 72, 171, 182, 215, 97, 1,
109, 52, 192, 105, 131, 236, 70, 240, 211, 16, 175, 237, 1, 164, 242, 21, 250, 7, 182,
87, 200, 84, 121, 177, 139, 184, 62, 86, 239, 221, 212, 206, 23, 176, 184, 173, 182,
83, 250, 176, 218, 222, 73, 192, 165, 108, 20, 233, 138, 102, 8, 186, 0, 34, 79, 212,
190, 139, 237, 164, 11, 13, 236, 223, 90, 18, 161, 105, 219, 189, 211, 233, 56, 100,
27, 53, 61, 230, 220, 103, 22, 220, 157, 206, 198, 33, 124, 46, 160, 49, 72, 66, 109,
130, 156, 126, 25, 231, 164, 31, 17, 102, 112, 78, 240, 195, 215, 22, 58, 199, 29, 244,
246, 17, 248, 182, 159, 244, 231, 2, 187, 178, 212, 133, 198, 226, 154, 196, 194, 109,
105, 237, 98, 73, 70, 73, 174, 133, 214, 16, 22, 165, 73, 132, 37, 25, 78, 185, 13, 20,
226, 205, 111, 76, 80, 87, 156, 171, 130, 243, 102, 245, 66, 54, 21, 241, 150, 144,
113, 204, 11, 45, 205, 147, 31, 35, 223, 39, 159, 14, 134, 11, 233, 90, 91, 234, 149,
220, 63, 225, 191, 155, 78, 23, 26, 233, 239, 12, 87, 75, 185, 112, 53, 5, 218, 162,
88, 143, 73, 163, 240, 198, 80, 106, 205, 225, 201, 11, 211, 102, 187, 59, 131, 4, 18,
68, 104, 61, 114, 222, 250, 243, 104, 191, 186, 190, 228, 118, 222, 138, 144, 82, 50,
65, 20, 233, 128, 139, 237, 52, 175, 75, 228, 168, 57, 75, 2, 210, 98, 28, 86, 21, 106,
108, 25, 67, 189, 94, 185, 253, 174, 74, 73, 20, 161, 213, 76, 117, 19, 241, 59, 175,
156, 167, 74, 184, 148, 214, 21, 90, 95, 105, 76, 80, 157, 146, 182, 184, 240, 89, 31,
94, 80, 68, 218, 177, 126, 147, 26, 184, 109, 211, 32, 123, 49, 11, 120, 16, 190, 124,
255, 23, 39, 117, 103, 82, 62, 214, 102, 187, 195, 122, 245, 115, 31, 4, 29, 84, 181,
80, 204, 22, 61, 140, 159, 161, 228, 241, 229, 231, 219, 229, 202, 193, 72, 193, 139,
151, 179, 135, 40, 217, 140, 251, 3, 18, 106, 142, 249, 255, 73, 62, 156, 133, 5, 28,
112, 57, 94, 73, 161, 245, 238, 26, 20, 197, 81, 11, 225, 137, 62, 144, 221, 198, 148,
35, 107, 194, 189, 8, 41, 125, 129, 244, 238, 35, 213, 254, 254, 246, 176, 184, 172,
112, 85, 54, 235, 239, 79, 250, 151, 27, 34, 79, 149, 124, 0, 103, 230, 132, 251, 122,
82, 46, 52, 132, 228, 234, 159, 186, 221, 203, 94, 0, 236, 182, 125, 236, 47, 243, 7,
38, 9, 241, 2, 45, 199, 19, 230, 15, 178, 197, 116, 37, 88, 0, 215, 103, 13, 104, 114,
248, 15, 240, 7,
];

let decompressed = decompress(Full::new(COMPRESSED.as_slice())).await?;
Expand Down
66 changes: 66 additions & 0 deletions twilight-model/src/guild/invite/invite_type.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, PartialEq, Serialize)]
#[non_exhaustive]
#[serde(from = "u8", into = "u8")]
pub enum InviteType {
Guild,
GroupDm,
Friend,
Unknown(u8),
}

impl From<u8> for InviteType {
fn from(value: u8) -> Self {
match value {
0 => Self::Guild,
1 => Self::GroupDm,
2 => Self::Friend,
unknown => Self::Unknown(unknown),
}
}
}

impl From<InviteType> for u8 {
fn from(value: InviteType) -> Self {
match value {
InviteType::Guild => 0,
InviteType::GroupDm => 1,
InviteType::Friend => 2,
InviteType::Unknown(unknown) => unknown,
}
}
}

impl InviteType {
pub const fn name(&self) -> &str {
match self {
Self::Guild => "Guild",
Self::GroupDm => "Group",
Self::Friend => "Friend",
Erk- marked this conversation as resolved.
Show resolved Hide resolved
Self::Unknown(_) => "Unknown",
}
}
}

#[cfg(test)]
mod tests {
use super::InviteType;
use serde_test::Token;

#[test]
fn variants() {
serde_test::assert_tokens(&InviteType::Guild, &[Token::U8(0)]);
serde_test::assert_tokens(&InviteType::GroupDm, &[Token::U8(1)]);
serde_test::assert_tokens(&InviteType::Friend, &[Token::U8(2)]);
serde_test::assert_tokens(&InviteType::Unknown(99), &[Token::U8(99)]);
}

#[test]
fn names() {
assert_eq!(InviteType::Guild.name(), "Guild");
assert_eq!(InviteType::GroupDm.name(), "Group");
assert_eq!(InviteType::Friend.name(), "Friend");
assert_eq!(InviteType::Unknown(99).name(), "Unknown");
}
}
18 changes: 14 additions & 4 deletions twilight-model/src/guild/invite/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
mod channel;
mod guild;
mod invite_type;
mod target_type;
mod welcome_screen;

pub use self::{
channel::InviteChannel,
guild::InviteGuild,
invite_type::InviteType,
target_type::TargetType,
welcome_screen::{WelcomeScreen, WelcomeScreenChannel},
};
Expand Down Expand Up @@ -39,15 +41,17 @@ pub struct Invite {
pub target_user: Option<User>,
#[serde(skip_serializing_if = "Option::is_none")]
pub temporary: Option<bool>,
#[serde(rename = "type")]
pub kind: InviteType,
#[serde(skip_serializing_if = "Option::is_none")]
pub uses: Option<u64>,
}

#[cfg(test)]
mod tests {
use super::{
welcome_screen::WelcomeScreenChannel, Invite, InviteChannel, InviteGuild, TargetType, User,
WelcomeScreen,
welcome_screen::WelcomeScreenChannel, Invite, InviteChannel, InviteGuild, InviteType,
TargetType, User, WelcomeScreen,
};
use crate::{
channel::ChannelType,
Expand Down Expand Up @@ -109,6 +113,7 @@ mod tests {
target_type: Some(TargetType::Stream),
target_user: None,
temporary: None,
kind: InviteType::Guild,
uses: None,
};

Expand All @@ -117,7 +122,7 @@ mod tests {
&[
Token::Struct {
name: "Invite",
len: 5,
len: 6,
},
Token::Str("approximate_member_count"),
Token::Some,
Expand All @@ -142,6 +147,8 @@ mod tests {
Token::Str("target_type"),
Token::Some,
Token::U8(1),
Token::Str("type"),
Token::U8(0),
Token::StructEnd,
],
);
Expand Down Expand Up @@ -235,6 +242,7 @@ mod tests {
verified: None,
}),
temporary: Some(false),
kind: InviteType::Guild,
uses: Some(3),
};

Expand All @@ -243,7 +251,7 @@ mod tests {
&[
Token::Struct {
name: "Invite",
len: 14,
len: 15,
},
Token::Str("approximate_member_count"),
Token::Some,
Expand Down Expand Up @@ -417,6 +425,8 @@ mod tests {
Token::Str("temporary"),
Token::Some,
Token::Bool(false),
Token::Str("type"),
Token::U8(0),
Token::Str("uses"),
Token::Some,
Token::U64(3),
Expand Down