No need to pass visitor instance to methods

This commit is contained in:
Dirkjan Ochtman 2022-09-05 17:09:35 +02:00
parent cbf78e8b46
commit 2aa8a4b117
3 changed files with 36 additions and 44 deletions

View File

@ -138,8 +138,7 @@ impl Deserializer {
type Value = #ident #ty_generics;
fn visit_struct(
&self,
deserializer: &mut ::instant_xml::Deserializer<'xml>
deserializer: &mut ::instant_xml::Deserializer<'xml>,
) -> Result<Self::Value, ::instant_xml::Error> {
use ::instant_xml::de::Node;
@ -194,11 +193,7 @@ impl Deserializer {
}
#namespaces_map;
deserializer.deserialize_struct(
StructVisitor{
marker: std::marker::PhantomData,
lifetime: std::marker::PhantomData
},
deserializer.deserialize_struct::<StructVisitor>(
#name,
#default_namespace,
&namespaces_map

View File

@ -96,7 +96,6 @@ impl<'xml> Deserializer<'xml> {
pub fn deserialize_struct<V>(
&mut self,
visitor: V,
name: &str,
def_default_namespace: &'xml str,
def_namespaces: &HashMap<&'xml str, &'xml str>,
@ -151,7 +150,7 @@ impl<'xml> Deserializer<'xml> {
.filter(|(k, v)| self.parser_namespaces.insert(k, v).is_none())
.collect::<Vec<_>>();
let ret = visitor.visit_struct(self)?;
let ret = V::visit_struct(self)?;
// Process close tag
let item = match self.parser.next() {
@ -197,7 +196,7 @@ impl<'xml> Deserializer<'xml> {
ret
}
pub(crate) fn deserialize_element<V>(&mut self, visitor: V) -> Result<V::Value, Error>
pub(crate) fn deserialize_element<V>(&mut self) -> Result<V::Value, Error>
where
V: Visitor<'xml>,
{
@ -209,7 +208,7 @@ impl<'xml> Deserializer<'xml> {
match self.parser.next() {
Some(Ok(XmlRecord::Element(v))) => {
let ret = visitor.visit_str(v);
let ret = V::visit_str(v);
self.parser.next();
ret
}
@ -217,12 +216,9 @@ impl<'xml> Deserializer<'xml> {
}
}
pub(crate) fn deserialize_attribute<V>(&mut self, visitor: V) -> Result<V::Value, Error>
where
V: Visitor<'xml>,
{
pub(crate) fn deserialize_attribute<V: Visitor<'xml>>(&mut self) -> Result<V::Value, Error> {
match self.tag_attributes.pop() {
Some(attr) => visitor.visit_str(attr.value),
Some(attr) => V::visit_str(attr.value),
None => Err(Error::UnexpectedEndOfStream),
}
}
@ -383,11 +379,11 @@ impl<'xml> Iterator for XmlParser<'xml> {
pub trait Visitor<'xml>: Sized {
type Value;
fn visit_str(self, _value: &'xml str) -> Result<Self::Value, Error> {
fn visit_str(_value: &'xml str) -> Result<Self::Value, Error> {
unimplemented!();
}
fn visit_struct(&self, _deserializer: &mut Deserializer<'xml>) -> Result<Self::Value, Error> {
fn visit_struct(_deserializer: &mut Deserializer<'xml>) -> Result<Self::Value, Error> {
unimplemented!();
}
}

View File

@ -18,7 +18,8 @@ where
<T as FromStr>::Err: std::fmt::Display,
{
type Value = T;
fn visit_str(self, value: &str) -> Result<Self::Value, Error> {
fn visit_str(value: &str) -> Result<Self::Value, Error> {
match FromStr::from_str(value) {
Ok(v) => Ok(v),
Err(e) => Err(Error::Other(e.to_string())),
@ -31,8 +32,8 @@ struct BoolVisitor;
impl<'xml> Visitor<'xml> for BoolVisitor {
type Value = bool;
fn visit_str(self, value: &str) -> Result<Self::Value, Error> {
FromStrToVisitor(PhantomData::<Self::Value>).visit_str(value)
fn visit_str(value: &str) -> Result<Self::Value, Error> {
FromStrToVisitor::<Self::Value>::visit_str(value)
}
}
@ -41,8 +42,8 @@ impl<'xml> FromXml<'xml> for bool {
fn deserialize(deserializer: &mut Deserializer) -> Result<Self, Error> {
match deserializer.consume_next_type() {
EntityType::Element => deserializer.deserialize_element(BoolVisitor),
EntityType::Attribute => deserializer.deserialize_attribute(BoolVisitor),
EntityType::Element => deserializer.deserialize_element::<BoolVisitor>(),
EntityType::Attribute => deserializer.deserialize_attribute::<BoolVisitor>(),
}
}
}
@ -105,8 +106,8 @@ where
{
type Value = T;
fn visit_str(self, value: &str) -> Result<Self::Value, Error> {
FromStrToVisitor(PhantomData::<Self::Value>).visit_str(value)
fn visit_str(value: &str) -> Result<Self::Value, Error> {
FromStrToVisitor::<Self::Value>::visit_str(value)
}
}
@ -115,12 +116,12 @@ macro_rules! from_xml_for_number {
impl<'xml> FromXml<'xml> for $typ {
fn deserialize(deserializer: &mut Deserializer) -> Result<Self, Error> {
match deserializer.consume_next_type() {
EntityType::Element => deserializer.deserialize_element(NumberVisitor {
marker: PhantomData,
}),
EntityType::Attribute => deserializer.deserialize_attribute(NumberVisitor {
marker: PhantomData,
}),
EntityType::Element => {
deserializer.deserialize_element::<NumberVisitor<$typ>>()
}
EntityType::Attribute => {
deserializer.deserialize_attribute::<NumberVisitor<$typ>>()
}
}
}
@ -147,7 +148,7 @@ struct StringVisitor;
impl<'xml> Visitor<'xml> for StringVisitor {
type Value = String;
fn visit_str(self, value: &str) -> Result<Self::Value, Error> {
fn visit_str(value: &str) -> Result<Self::Value, Error> {
Ok(escape_back(value).into_owned())
}
}
@ -158,8 +159,8 @@ impl<'xml> FromXml<'xml> for String {
fn deserialize(deserializer: &mut Deserializer) -> Result<Self, Error> {
//<&'xml str>::deserialize(deserializer);
match deserializer.consume_next_type() {
EntityType::Element => deserializer.deserialize_element(StringVisitor),
EntityType::Attribute => deserializer.deserialize_attribute(StringVisitor),
EntityType::Element => deserializer.deserialize_element::<StringVisitor>(),
EntityType::Attribute => deserializer.deserialize_attribute::<StringVisitor>(),
}
}
}
@ -169,7 +170,7 @@ struct CharVisitor;
impl<'xml> Visitor<'xml> for CharVisitor {
type Value = char;
fn visit_str(self, value: &str) -> Result<Self::Value, Error> {
fn visit_str(value: &str) -> Result<Self::Value, Error> {
match value.len() {
1 => Ok(value.chars().next().expect("char type")),
_ => Err(Error::Other("Expected char type".to_string())),
@ -182,8 +183,8 @@ impl<'xml> FromXml<'xml> for char {
fn deserialize(deserializer: &mut Deserializer) -> Result<Self, Error> {
match deserializer.consume_next_type() {
EntityType::Element => deserializer.deserialize_element(CharVisitor),
EntityType::Attribute => deserializer.deserialize_attribute(CharVisitor),
EntityType::Element => deserializer.deserialize_element::<CharVisitor>(),
EntityType::Attribute => deserializer.deserialize_attribute::<CharVisitor>(),
}
}
}
@ -193,7 +194,7 @@ struct StrVisitor;
impl<'a> Visitor<'a> for StrVisitor {
type Value = &'a str;
fn visit_str(self, value: &'a str) -> Result<Self::Value, Error> {
fn visit_str(value: &'a str) -> Result<Self::Value, Error> {
match escape_back(value) {
Cow::Owned(v) => Err(Error::Other(format!("Unsupported char: {}", v))),
Cow::Borrowed(v) => Ok(v),
@ -206,8 +207,8 @@ impl<'xml> FromXml<'xml> for &'xml str {
fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result<Self, Error> {
match deserializer.consume_next_type() {
EntityType::Element => deserializer.deserialize_element(StrVisitor),
EntityType::Attribute => deserializer.deserialize_attribute(StrVisitor),
EntityType::Element => deserializer.deserialize_element::<StrVisitor>(),
EntityType::Attribute => deserializer.deserialize_attribute::<StrVisitor>(),
}
}
}
@ -217,18 +218,18 @@ struct CowStrVisitor;
impl<'a> Visitor<'a> for CowStrVisitor {
type Value = Cow<'a, str>;
fn visit_str(self, value: &'a str) -> Result<Self::Value, Error> {
fn visit_str(value: &'a str) -> Result<Self::Value, Error> {
Ok(escape_back(value))
}
}
impl<'xml> FromXml<'xml> for Cow<'xml, str> {
const KIND: Kind = <&str>::KIND;
const KIND: Kind = Kind::Scalar;
fn deserialize(deserializer: &mut Deserializer<'xml>) -> Result<Self, Error> {
match deserializer.consume_next_type() {
EntityType::Element => deserializer.deserialize_element(CowStrVisitor),
EntityType::Attribute => deserializer.deserialize_attribute(CowStrVisitor),
EntityType::Element => deserializer.deserialize_element::<CowStrVisitor>(),
EntityType::Attribute => deserializer.deserialize_attribute::<CowStrVisitor>(),
}
}
}