11use serde:: de:: { Deserialize , Deserializer , Error as DeError } ;
22use serde:: ser:: { Serialize , Serializer } ;
3- use serde_json:: from_value ;
3+ use serde_json:: value :: RawValue ;
44
55use super :: {
66 CommandInteraction ,
@@ -16,7 +16,7 @@ use crate::model::guild::PartialMember;
1616use crate :: model:: id:: { ApplicationId , GuildId , InteractionId , MessageId , UserId } ;
1717use crate :: model:: monetization:: Entitlement ;
1818use crate :: model:: user:: User ;
19- use crate :: model:: utils:: { StrOrInt , deserialize_val , remove_from_map } ;
19+ use crate :: model:: utils:: StrOrInt ;
2020
2121/// [Discord docs](https://discord.com/developers/docs/interactions/receiving-and-responding#interaction-object)
2222#[ cfg_attr( feature = "typesize" , derive( typesize:: derive:: TypeSize ) ) ]
@@ -244,17 +244,27 @@ impl Interaction {
244244// Manual impl needed to emulate integer enum tags
245245impl < ' de > Deserialize < ' de > for Interaction {
246246 fn deserialize < D : Deserializer < ' de > > ( deserializer : D ) -> std:: result:: Result < Self , D :: Error > {
247- let map = JsonMap :: deserialize ( deserializer) ?;
247+ #[ derive( Deserialize ) ]
248+ struct InteractionRaw {
249+ #[ serde( rename = "type" ) ]
250+ kind : InteractionType ,
251+ }
248252
249- let raw_kind = map . get ( "type" ) . ok_or_else ( || DeError :: missing_field ( "type" ) ) ? . clone ( ) ;
250- let value = Value :: from ( map ) ;
253+ let raw_data = < & RawValue > :: deserialize ( deserializer ) ? ;
254+ let raw = InteractionRaw :: deserialize ( raw_data ) . map_err ( DeError :: custom ) ? ;
251255
252- match deserialize_val ( raw_kind) ? {
253- InteractionType :: Command => from_value ( value) . map ( Interaction :: Command ) ,
254- InteractionType :: Component => from_value ( value) . map ( Interaction :: Component ) ,
255- InteractionType :: Autocomplete => from_value ( value) . map ( Interaction :: Autocomplete ) ,
256- InteractionType :: Modal => from_value ( value) . map ( Interaction :: Modal ) ,
257- InteractionType :: Ping => from_value ( value) . map ( Interaction :: Ping ) ,
256+ match raw. kind {
257+ InteractionType :: Command => {
258+ Deserialize :: deserialize ( raw_data) . map ( Interaction :: Command )
259+ } ,
260+ InteractionType :: Component => {
261+ Deserialize :: deserialize ( raw_data) . map ( Interaction :: Component )
262+ } ,
263+ InteractionType :: Autocomplete => {
264+ Deserialize :: deserialize ( raw_data) . map ( Interaction :: Autocomplete )
265+ } ,
266+ InteractionType :: Modal => Deserialize :: deserialize ( raw_data) . map ( Interaction :: Modal ) ,
267+ InteractionType :: Ping => Deserialize :: deserialize ( raw_data) . map ( Interaction :: Ping ) ,
258268 InteractionType ( _) => return Err ( DeError :: custom ( "Unknown interaction type" ) ) ,
259269 }
260270 . map_err ( DeError :: custom)
@@ -494,16 +504,23 @@ pub enum MessageInteractionMetadata {
494504
495505impl < ' de > serde:: Deserialize < ' de > for MessageInteractionMetadata {
496506 fn deserialize < D : Deserializer < ' de > > ( deserializer : D ) -> StdResult < Self , D :: Error > {
497- let mut data = JsonMap :: deserialize ( deserializer) ?;
498- let kind: InteractionType = remove_from_map ( & mut data, "type" ) ?;
507+ #[ derive( Deserialize ) ]
508+ struct InteractionRaw {
509+ #[ serde( rename = "type" ) ]
510+ kind : InteractionType ,
511+ }
499512
500- match kind {
501- InteractionType :: Command => deserialize_val ( Value :: from ( data) ) . map ( Self :: Command ) ,
502- InteractionType :: Component => deserialize_val ( Value :: from ( data) ) . map ( Self :: Component ) ,
503- InteractionType :: Modal => deserialize_val ( Value :: from ( data) ) . map ( Self :: ModalSubmit ) ,
513+ let raw_data = <& RawValue >:: deserialize ( deserializer) ?;
514+ let raw = InteractionRaw :: deserialize ( raw_data) . map_err ( DeError :: custom) ?;
515+
516+ match raw. kind {
517+ InteractionType :: Command => Deserialize :: deserialize ( raw_data) . map ( Self :: Command ) ,
518+ InteractionType :: Component => Deserialize :: deserialize ( raw_data) . map ( Self :: Component ) ,
519+ InteractionType :: Modal => Deserialize :: deserialize ( raw_data) . map ( Self :: ModalSubmit ) ,
504520
505521 unknown => Ok ( Self :: Unknown ( unknown) ) ,
506522 }
523+ . map_err ( DeError :: custom)
507524 }
508525}
509526
0 commit comments