-
-
Notifications
You must be signed in to change notification settings - Fork 132
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(model): add invite type (#2346)
- Loading branch information
1 parent
dc2544f
commit 6e0e2a5
Showing
3 changed files
with
111 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters