Clarify FromXml identity const
This commit is contained in:
parent
9cfee92d03
commit
bebeba8d72
|
@ -218,7 +218,7 @@ impl Deserializer {
|
||||||
));
|
));
|
||||||
|
|
||||||
out.extend(quote!(
|
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!(
|
out = quote!(
|
||||||
|
@ -251,10 +251,7 @@ impl Deserializer {
|
||||||
tokens.enum_.extend(quote!(#enum_name,));
|
tokens.enum_.extend(quote!(#enum_name,));
|
||||||
|
|
||||||
tokens.consts.extend(quote!(
|
tokens.consts.extend(quote!(
|
||||||
const #const_field_var_str: &str = match <#no_lifetime_type>::TAG_NAME {
|
const #const_field_var_str: &str = <#no_lifetime_type>::KIND.name(#field_var_str);
|
||||||
::instant_xml::TagName::FieldName => #field_var_str,
|
|
||||||
::instant_xml::TagName::Custom(v) => v,
|
|
||||||
};
|
|
||||||
));
|
));
|
||||||
|
|
||||||
if is_element {
|
if is_element {
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::marker::PhantomData;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use crate::de::{EntityType, Visitor};
|
use crate::de::{EntityType, Visitor};
|
||||||
use crate::{Deserializer, Error, FieldAttribute, FromXml, Serializer, TagName, ToXml};
|
use crate::{Deserializer, Error, FieldAttribute, FromXml, Kind, Serializer, ToXml};
|
||||||
|
|
||||||
// Deserializer
|
// Deserializer
|
||||||
struct FromStrToVisitor<T: FromStr>(PhantomData<T>)
|
struct FromStrToVisitor<T: FromStr>(PhantomData<T>)
|
||||||
|
@ -37,7 +37,7 @@ impl<'xml> Visitor<'xml> for BoolVisitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'xml> FromXml<'xml> for bool {
|
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> {
|
fn deserialize(deserializer: &mut Deserializer) -> Result<Self, Error> {
|
||||||
match deserializer.consume_next_type() {
|
match deserializer.consume_next_type() {
|
||||||
|
@ -113,8 +113,6 @@ where
|
||||||
macro_rules! from_xml_for_number {
|
macro_rules! from_xml_for_number {
|
||||||
($typ:ty) => {
|
($typ:ty) => {
|
||||||
impl<'xml> FromXml<'xml> for $typ {
|
impl<'xml> FromXml<'xml> for $typ {
|
||||||
const TAG_NAME: TagName = TagName::FieldName;
|
|
||||||
|
|
||||||
fn deserialize(deserializer: &mut Deserializer) -> Result<Self, Error> {
|
fn deserialize(deserializer: &mut Deserializer) -> Result<Self, Error> {
|
||||||
match deserializer.consume_next_type() {
|
match deserializer.consume_next_type() {
|
||||||
EntityType::Element => deserializer.deserialize_element(NumberVisitor {
|
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 {
|
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> {
|
fn deserialize(deserializer: &mut Deserializer) -> Result<Self, Error> {
|
||||||
//<&'xml str>::deserialize(deserializer);
|
//<&'xml str>::deserialize(deserializer);
|
||||||
|
@ -178,7 +178,7 @@ impl<'xml> Visitor<'xml> for CharVisitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'xml> FromXml<'xml> for char {
|
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> {
|
fn deserialize(deserializer: &mut Deserializer) -> Result<Self, Error> {
|
||||||
match deserializer.consume_next_type() {
|
match deserializer.consume_next_type() {
|
||||||
|
@ -202,7 +202,7 @@ impl<'a> Visitor<'a> for StrVisitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'xml> FromXml<'xml> for &'xml str {
|
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> {
|
fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result<Self, Error> {
|
||||||
match deserializer.consume_next_type() {
|
match deserializer.consume_next_type() {
|
||||||
|
@ -223,7 +223,7 @@ impl<'a> Visitor<'a> for CowStrVisitor {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'xml> FromXml<'xml> for Cow<'xml, str> {
|
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> {
|
fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result<Self, Error> {
|
||||||
match deserializer.consume_next_type() {
|
match deserializer.consume_next_type() {
|
||||||
|
@ -237,7 +237,7 @@ impl<'xml, T> FromXml<'xml> for Option<T>
|
||||||
where
|
where
|
||||||
T: FromXml<'xml>,
|
T: FromXml<'xml>,
|
||||||
{
|
{
|
||||||
const TAG_NAME: TagName = <T>::TAG_NAME;
|
const KIND: Kind = <T>::KIND;
|
||||||
|
|
||||||
fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result<Self, Error> {
|
fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result<Self, Error> {
|
||||||
match <T>::deserialize(deserializer) {
|
match <T>::deserialize(deserializer) {
|
||||||
|
|
|
@ -29,14 +29,7 @@ pub enum FieldAttribute<'xml> {
|
||||||
Attribute,
|
Attribute,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub enum TagName {
|
|
||||||
FieldName,
|
|
||||||
Custom(&'static str),
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait FromXml<'xml>: Sized {
|
pub trait FromXml<'xml>: Sized {
|
||||||
const TAG_NAME: TagName;
|
|
||||||
|
|
||||||
fn from_xml(input: &'xml str) -> Result<Self, Error> {
|
fn from_xml(input: &'xml str) -> Result<Self, Error> {
|
||||||
let mut deserializer = Deserializer::new(input);
|
let mut deserializer = Deserializer::new(input);
|
||||||
Self::deserialize(&mut deserializer)
|
Self::deserialize(&mut deserializer)
|
||||||
|
@ -49,6 +42,22 @@ pub trait FromXml<'xml>: Sized {
|
||||||
fn missing_value() -> Result<Self, Error> {
|
fn missing_value() -> Result<Self, Error> {
|
||||||
Err(Error::MissingValue)
|
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> {}
|
pub trait FromXmlOwned: for<'xml> FromXml<'xml> {}
|
||||||
|
|
Loading…
Reference in New Issue