From 1e3ae38b25442ad580f583c71d352fe85b57e525 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 24 Nov 2022 18:49:44 -0800 Subject: [PATCH] Implement matches() method on Kind --- instant-xml-macros/src/de.rs | 8 ++++---- instant-xml/src/lib.rs | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/instant-xml-macros/src/de.rs b/instant-xml-macros/src/de.rs index 17ec43d..4906f71 100644 --- a/instant-xml-macros/src/de.rs +++ b/instant-xml-macros/src/de.rs @@ -107,7 +107,9 @@ fn deserialize_wrapped_enum( let v_ident = &variant.ident; variants.extend( - quote!(if ::instant_xml::Kind::Element(id) == <#no_lifetime_type as FromXml>::KIND { + quote!(if <#no_lifetime_type as FromXml>::KIND.matches( + id, ::instant_xml::Id { ns: "", name: "" } + ) { let mut nested = deserializer.nested(data); #ident::#v_ident(#no_lifetime_type::deserialize(&mut nested)?) }), @@ -326,9 +328,7 @@ fn process_field( tokens.branches.extend(quote!(else)); } tokens.branches.extend(quote!( - if id == <#no_lifetime_type as FromXml>::KIND.name( - Id { ns: #ns, name: #field_tag } - ) + if <#no_lifetime_type as FromXml>::KIND.matches(id, Id { ns: #ns, name: #field_tag }) )); tokens.branches.extend(match field_meta.attribute { diff --git a/instant-xml/src/lib.rs b/instant-xml/src/lib.rs index 7579389..ddba774 100644 --- a/instant-xml/src/lib.rs +++ b/instant-xml/src/lib.rs @@ -127,6 +127,15 @@ impl<'a> Kind<'a> { Kind::Vec => field, } } + + #[inline] + pub fn matches(&self, id: Id<'_>, field: Id<'_>) -> bool { + match self { + Kind::Scalar => id == field, + Kind::Element(name) => id == *name, + Kind::Vec => id == field, + } + } } #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]