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 1 commit
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
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,
suneettipirneni marked this conversation as resolved.
Show resolved Hide resolved
Friend,
Unknown(u8),
}

impl From<u8> for InviteType {
fn from(value: u8) -> Self {
match value {
0 => InviteType::Guild,
1 => InviteType::GroupDM,
2 => InviteType::Friend,
unknown => InviteType::Unknown(unknown),
suneettipirneni marked this conversation as resolved.
Show resolved Hide resolved
}
}
}

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 DM",
Copy link
Member

@laralove143 laralove143 May 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in other similar types we follow the name of the variant itself so it'd be GroupDm

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the reason you used Group instead of GroupDm

Self::Friend => "Friend",
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 DM");
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
Loading