diff --git a/instant-xml-macros/src/de.rs b/instant-xml-macros/src/de.rs index e343b69..dbb5382 100644 --- a/instant-xml-macros/src/de.rs +++ b/instant-xml-macros/src/de.rs @@ -218,7 +218,7 @@ impl Deserializer { )); out.extend(quote!( - const TAG_NAME: ::instant_xml::TagName = ::instant_xml::TagName::Custom(#name); + const KIND: ::instant_xml::Kind = ::instant_xml::Kind::Element(#name); )); out = quote!( @@ -251,10 +251,7 @@ impl Deserializer { tokens.enum_.extend(quote!(#enum_name,)); tokens.consts.extend(quote!( - const #const_field_var_str: &str = match <#no_lifetime_type>::TAG_NAME { - ::instant_xml::TagName::FieldName => #field_var_str, - ::instant_xml::TagName::Custom(v) => v, - }; + const #const_field_var_str: &str = <#no_lifetime_type>::KIND.name(#field_var_str); )); if is_element { diff --git a/instant-xml/src/impls.rs b/instant-xml/src/impls.rs index 1fda514..10416a9 100644 --- a/instant-xml/src/impls.rs +++ b/instant-xml/src/impls.rs @@ -4,7 +4,7 @@ use std::marker::PhantomData; use std::str::FromStr; use crate::de::{EntityType, Visitor}; -use crate::{Deserializer, Error, FieldAttribute, FromXml, Serializer, TagName, ToXml}; +use crate::{Deserializer, Error, FieldAttribute, FromXml, Kind, Serializer, ToXml}; // Deserializer struct FromStrToVisitor(PhantomData) @@ -37,7 +37,7 @@ impl<'xml> Visitor<'xml> for BoolVisitor { } impl<'xml> FromXml<'xml> for bool { - const TAG_NAME: TagName = TagName::FieldName; + const KIND: Kind = Kind::Scalar; fn deserialize(deserializer: &mut Deserializer) -> Result { match deserializer.consume_next_type() { @@ -113,8 +113,6 @@ where macro_rules! from_xml_for_number { ($typ:ty) => { impl<'xml> FromXml<'xml> for $typ { - const TAG_NAME: TagName = TagName::FieldName; - fn deserialize(deserializer: &mut Deserializer) -> Result { match deserializer.consume_next_type() { EntityType::Element => deserializer.deserialize_element(NumberVisitor { @@ -125,6 +123,8 @@ macro_rules! from_xml_for_number { }), } } + + const KIND: Kind = Kind::Scalar; } }; } @@ -153,7 +153,7 @@ impl<'xml> Visitor<'xml> for StringVisitor { } impl<'xml> FromXml<'xml> for String { - const TAG_NAME: TagName = TagName::FieldName; + const KIND: Kind = Kind::Scalar; fn deserialize(deserializer: &mut Deserializer) -> Result { //<&'xml str>::deserialize(deserializer); @@ -178,7 +178,7 @@ impl<'xml> Visitor<'xml> for CharVisitor { } impl<'xml> FromXml<'xml> for char { - const TAG_NAME: TagName = TagName::FieldName; + const KIND: Kind = Kind::Scalar; fn deserialize(deserializer: &mut Deserializer) -> Result { match deserializer.consume_next_type() { @@ -202,7 +202,7 @@ impl<'a> Visitor<'a> for StrVisitor { } impl<'xml> FromXml<'xml> for &'xml str { - const TAG_NAME: TagName = TagName::FieldName; + const KIND: Kind = Kind::Scalar; fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result { match deserializer.consume_next_type() { @@ -223,7 +223,7 @@ impl<'a> Visitor<'a> for CowStrVisitor { } impl<'xml> FromXml<'xml> for Cow<'xml, str> { - const TAG_NAME: TagName = <&str>::TAG_NAME; + const KIND: Kind = <&str>::KIND; fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result { match deserializer.consume_next_type() { @@ -237,7 +237,7 @@ impl<'xml, T> FromXml<'xml> for Option where T: FromXml<'xml>, { - const TAG_NAME: TagName = ::TAG_NAME; + const KIND: Kind = ::KIND; fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result { match ::deserialize(deserializer) { diff --git a/instant-xml/src/lib.rs b/instant-xml/src/lib.rs index 1dd53b1..c3ad042 100644 --- a/instant-xml/src/lib.rs +++ b/instant-xml/src/lib.rs @@ -29,14 +29,7 @@ pub enum FieldAttribute<'xml> { Attribute, } -pub enum TagName { - FieldName, - Custom(&'static str), -} - pub trait FromXml<'xml>: Sized { - const TAG_NAME: TagName; - fn from_xml(input: &'xml str) -> Result { let mut deserializer = Deserializer::new(input); Self::deserialize(&mut deserializer) @@ -49,6 +42,22 @@ pub trait FromXml<'xml>: Sized { fn missing_value() -> Result { Err(Error::MissingValue) } + + const KIND: Kind; +} + +pub enum Kind { + Scalar, + Element(&'static str), +} + +impl Kind { + pub const fn name(&self, field: &'static str) -> &'static str { + match self { + Kind::Scalar => field, + Kind::Element(name) => name, + } + } } pub trait FromXmlOwned: for<'xml> FromXml<'xml> {}