Skip to content

Commit

Permalink
This is also true for guild_delete
Browse files Browse the repository at this point in the history
As the helpful comment we have says
  • Loading branch information
Erk- committed Oct 5, 2024
1 parent 44b9a2c commit 96b027b
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions twilight-model/src/gateway/payload/incoming/guild_delete.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
use crate::id::{marker::GuildMarker, Id};
use serde::{Deserialize, Deserializer, Serialize};
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct GuildDelete {
pub id: Id<GuildMarker>,
// If `unavailable` is `None` the user was removed from the guild.
#[serde(default, deserialize_with = "nullable_unavailable")]
pub unavailable: bool,
}

#[allow(clippy::unnecessary_wraps)]
fn nullable_unavailable<'de, D: Deserializer<'de>>(deserializer: D) -> Result<bool, D::Error> {
Ok(Deserialize::deserialize(deserializer).unwrap_or_default())
/// If `None` the user was removed from the guild.
pub unavailable: Option<bool>,
}

#[cfg(test)]
Expand All @@ -24,7 +18,7 @@ mod tests {
fn guild_delete_available() {
let expected = GuildDelete {
id: Id::new(123),
unavailable: true,
unavailable: Some(true),
};

serde_test::assert_de_tokens(
Expand All @@ -38,6 +32,7 @@ mod tests {
Token::NewtypeStruct { name: "Id" },
Token::Str("123"),
Token::Str("unavailable"),
Token::Some,
Token::Bool(true),
Token::StructEnd,
],
Expand All @@ -53,6 +48,7 @@ mod tests {
Token::NewtypeStruct { name: "Id" },
Token::Str("123"),
Token::Str("unavailable"),
Token::Some,
Token::Bool(true),
Token::StructEnd,
],
Expand All @@ -63,7 +59,7 @@ mod tests {
fn guild_delete_unavailable() {
let expected = GuildDelete {
id: Id::new(123),
unavailable: false,
unavailable: Some(false),
};

serde_test::assert_de_tokens(
Expand All @@ -77,6 +73,7 @@ mod tests {
Token::NewtypeStruct { name: "Id" },
Token::Str("123"),
Token::Str("unavailable"),
Token::Some,
Token::Bool(false),
Token::StructEnd,
],
Expand All @@ -92,6 +89,7 @@ mod tests {
Token::NewtypeStruct { name: "Id" },
Token::Str("123"),
Token::Str("unavailable"),
Token::Some,
Token::Bool(false),
Token::StructEnd,
],
Expand All @@ -102,21 +100,19 @@ mod tests {
fn guild_delete_unavailable_null_default() {
let expected = GuildDelete {
id: Id::new(123),
unavailable: false,
unavailable: None,
};

serde_test::assert_de_tokens(
&expected,
&[
Token::Struct {
name: "GuildDelete",
len: 2,
len: 1,
},
Token::Str("id"),
Token::NewtypeStruct { name: "Id" },
Token::Str("123"),
Token::Str("unavailable"),
Token::None,
Token::StructEnd,
],
);
Expand Down

0 comments on commit 96b027b

Please sign in to comment.