Generalize for_attr() to for_node()

This commit is contained in:
Dirkjan Ochtman 2022-11-26 13:11:11 -08:00
parent 894da25f8b
commit 508d29ccaa
2 changed files with 16 additions and 22 deletions

View File

@ -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))),

View File

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