Implement matches() method on Kind

This commit is contained in:
Dirkjan Ochtman 2022-11-24 18:49:44 -08:00
parent e5dcac3281
commit 1e3ae38b25
2 changed files with 13 additions and 4 deletions

View File

@ -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 {

View File

@ -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)]