Clarify FromXml identity const

This commit is contained in:
Dirkjan Ochtman 2022-09-01 14:33:44 +02:00
parent 9cfee92d03
commit bebeba8d72
3 changed files with 27 additions and 21 deletions

View File

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

View File

@ -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<T: FromStr>(PhantomData<T>)
@ -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<Self, Error> {
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<Self, Error> {
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<Self, Error> {
//<&'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<Self, Error> {
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<Self, Error> {
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<Self, Error> {
match deserializer.consume_next_type() {
@ -237,7 +237,7 @@ impl<'xml, T> FromXml<'xml> for Option<T>
where
T: FromXml<'xml>,
{
const TAG_NAME: TagName = <T>::TAG_NAME;
const KIND: Kind = <T>::KIND;
fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result<Self, Error> {
match <T>::deserialize(deserializer) {

View File

@ -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<Self, Error> {
let mut deserializer = Deserializer::new(input);
Self::deserialize(&mut deserializer)
@ -49,6 +42,22 @@ pub trait FromXml<'xml>: Sized {
fn missing_value() -> Result<Self, Error> {
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> {}