From 508d29ccaae8701b0ba40dfc7e9406f341f654e9 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 26 Nov 2022 13:11:11 -0800 Subject: [PATCH] Generalize for_attr() to for_node() --- instant-xml-macros/src/de.rs | 9 +++++---- instant-xml/src/de.rs | 29 +++++++++++------------------ 2 files changed, 16 insertions(+), 22 deletions(-) diff --git a/instant-xml-macros/src/de.rs b/instant-xml-macros/src/de.rs index 100aeca..d9f2e2b 100644 --- a/instant-xml-macros/src/de.rs +++ b/instant-xml-macros/src/de.rs @@ -361,7 +361,7 @@ fn named_field( } else { tokens.r#match.extend(quote!( __Attributes::#enum_name => { - let mut nested = deserializer.for_attr(attr); + let mut nested = deserializer.for_node(Node::AttributeValue(attr.value)); let new = <#no_lifetime_type>::deserialize(&mut nested, &mut #enum_name)?; }, )); @@ -453,14 +453,15 @@ fn unnamed_field( let #name = match node { Node::Open(data) => { + let mut nested = deserializer.nested(data); let mut value: Option<#no_lifetime_type> = None; - <#no_lifetime_type>::deserialize(deserializer, &mut value)?; + <#no_lifetime_type>::deserialize(&mut nested, &mut value)?; value } Node::Text(data) => { - deserializer.push_front(Node::Text(data)); + let mut nested = deserializer.for_node(Node::Text(data)); let mut value: Option<#no_lifetime_type> = None; - <#no_lifetime_type>::deserialize(deserializer, &mut value)?; + <#no_lifetime_type>::deserialize(&mut nested, &mut value)?; value } node => return Err(Error::UnexpectedNode(format!("{:?}", node))), diff --git a/instant-xml/src/de.rs b/instant-xml/src/de.rs index 53d27fd..ca53ce9 100644 --- a/instant-xml/src/de.rs +++ b/instant-xml/src/de.rs @@ -59,23 +59,6 @@ impl<'cx, 'xml> Deserializer<'cx, 'xml> { Deserializer::new(element, self.context) } - pub fn for_attr<'a>(&'a mut self, attr: Attribute<'xml>) -> Deserializer<'a, 'xml> - where - 'cx: 'a, - { - self.context - .records - .push_front(Node::AttributeValue(attr.value)); - - Deserializer { - local: self.local, - prefix: self.prefix, - level: self.level, - done: self.done, - context: self.context, - } - } - pub fn ignore(&mut self) -> Result<(), Error> { loop { match self.next() { @@ -90,8 +73,18 @@ impl<'cx, 'xml> Deserializer<'cx, 'xml> { } } - pub fn push_front(&mut self, node: Node<'xml>) { + pub fn for_node<'a>(&'a mut self, node: Node<'xml>) -> Deserializer<'a, 'xml> + where + 'cx: 'a, + { self.context.records.push_front(node); + Deserializer { + local: self.local, + prefix: self.prefix, + level: self.level, + done: self.done, + context: self.context, + } } #[inline]