Skip to content

Commit

Permalink
Fix deserialization errors (#1951)
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev authored Jun 7, 2022
1 parent fee5da4 commit 31a3914
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/client/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::sync::Arc;
use futures::channel::mpsc::UnboundedSender as Sender;
use futures::future::{BoxFuture, FutureExt};
use tokio::sync::RwLock;
use tracing::{instrument, warn};
use tracing::{debug, instrument};
use typemap_rev::TypeMap;

#[cfg(feature = "gateway")]
Expand Down Expand Up @@ -721,7 +721,7 @@ async fn handle_event(
event_handler.typing_start(context, event).await;
});
},
DispatchEvent::Model(Event::Unknown) => warn!("An unknown event was received"),
DispatchEvent::Model(Event::Unknown) => debug!("An unknown event was received"),
DispatchEvent::Model(Event::UserUpdate(mut event)) => {
let _before = update(&cache_and_http, &mut event);
let event_handler = Arc::clone(event_handler);
Expand Down
27 changes: 22 additions & 5 deletions src/model/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use super::utils::{
add_guild_id_to_map,
deserialize_val,
emojis,
ignore_input,
remove_from_map,
remove_from_map_opt,
roles,
Expand Down Expand Up @@ -266,27 +267,43 @@ impl<'de> Deserialize<'de> for GuildMembersChunkEvent {
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Serialize)]
#[non_exhaustive]
pub struct GuildRoleCreateEvent {
#[serde(deserialize_with = "roles::deserialize_event")]
pub role: Role,
}

impl<'de> Deserialize<'de> for GuildRoleCreateEvent {
/// Custom deserializer to pass entire payload to roles::deserialize_event, does not work using `deserialize_with`
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> StdResult<Self, D::Error> {
Ok(Self {
role: roles::deserialize_event(deserializer)?,
})
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct GuildRoleDeleteEvent {
pub guild_id: GuildId,
pub role_id: RoleId,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[derive(Clone, Debug, Serialize)]
#[non_exhaustive]
pub struct GuildRoleUpdateEvent {
#[serde(deserialize_with = "roles::deserialize_event")]
pub role: Role,
}

impl<'de> Deserialize<'de> for GuildRoleUpdateEvent {
/// Custom deserializer to pass entire payload to roles::deserialize_event, does not work using `deserialize_with`
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> StdResult<Self, D::Error> {
Ok(Self {
role: roles::deserialize_event(deserializer)?,
})
}
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[non_exhaustive]
pub struct GuildStickersUpdateEvent {
Expand Down Expand Up @@ -822,7 +839,7 @@ pub enum Event {
/// A guild member has unsubscribed from a scheduled event.
GuildScheduledEventUserRemove(GuildScheduledEventUserRemoveEvent),
/// An event type not covered by the above
#[serde(other)]
#[serde(other, deserialize_with = "ignore_input")]
Unknown,
}

Expand Down
7 changes: 5 additions & 2 deletions src/model/guild/partial_guild.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1632,8 +1632,11 @@ impl<'de> Deserialize<'de> for PartialGuild {
banner: remove_from_map_opt(&mut map, "banner")?.flatten(),
vanity_url_code: remove_from_map_opt(&mut map, "vanity_url_code")?.flatten(),
welcome_screen: remove_from_map_opt(&mut map, "welcome_screen")?,
approximate_member_count: remove_from_map(&mut map, "approximate_member_count")?,
approximate_presence_count: remove_from_map(&mut map, "approximate_presence_count")?,
approximate_member_count: remove_from_map_opt(&mut map, "approximate_member_count")?,
approximate_presence_count: remove_from_map_opt(
&mut map,
"approximate_presence_count",
)?,
nsfw_level: remove_from_map(&mut map, "nsfw_level")?,
max_video_channel_users: remove_from_map(&mut map, "max_video_channel_users")?,
max_presences: remove_from_map_opt(&mut map, "max_presences")?.flatten(),
Expand Down
5 changes: 5 additions & 0 deletions src/model/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ pub fn is_false(v: &bool) -> bool {
!v
}

#[allow(clippy::unnecessary_wraps)]
pub fn ignore_input<'de, D: Deserializer<'de>>(_: D) -> StdResult<(), D::Error> {
Ok(())
}

pub fn deserialize_val<T, E>(val: Value) -> StdResult<T, E>
where
T: serde::de::DeserializeOwned,
Expand Down

0 comments on commit 31a3914

Please sign in to comment.