Skip to content

Commit

Permalink
refactor: remove manual guild deserializer
Browse files Browse the repository at this point in the history
  • Loading branch information
suneettipirneni committed Feb 8, 2023
1 parent 4680cf8 commit 88145d7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 724 deletions.
26 changes: 25 additions & 1 deletion twilight-model/src/gateway/event/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,31 @@ impl<'de, 'a> DeserializeSeed<'de> for DispatchEventWithTypeDeserializer<'a> {
"GUILD_BAN_ADD" => DispatchEvent::BanAdd(BanAdd::deserialize(deserializer)?),
"GUILD_BAN_REMOVE" => DispatchEvent::BanRemove(BanRemove::deserialize(deserializer)?),
"GUILD_CREATE" => {
DispatchEvent::GuildCreate(Box::new(GuildCreate::deserialize(deserializer)?))
// We inject the guild id into the channels, presences, threads, and voice states
// here. This is because this gateway event omits the guild id from those structures
// to reduce redundancy. Nevertheless, our structures should have them since they are
// valid items in the guild.

let mut guild = GuildCreate::deserialize(deserializer)?;
let guild_id = guild.id;

for channel in &mut guild.channels {
channel.guild_id = Some(guild_id);
}

for presence in &mut guild.presences {
presence.guild_id = Some(guild_id);
}

for thread in &mut guild.threads {
thread.guild_id = Some(guild_id);
}

for voice_state in &mut guild.voice_states {
voice_state.guild_id.replace(guild_id);
}

DispatchEvent::GuildCreate(Box::new(guild))
}
"GUILD_DELETE" => DispatchEvent::GuildDelete(GuildDelete::deserialize(deserializer)?),
"GUILD_EMOJIS_UPDATE" => {
Expand Down
Loading

0 comments on commit 88145d7

Please sign in to comment.