Ignore attributes when looking for scalar values

This commit is contained in:
Dirkjan Ochtman 2023-02-22 22:05:54 +01:00
parent 8c2964b318
commit 8c618ada79
2 changed files with 27 additions and 6 deletions

View File

@ -31,12 +31,15 @@ impl<'cx, 'xml> Deserializer<'cx, 'xml> {
} }
pub fn take_str(&mut self) -> Result<Option<&'xml str>, Error> { pub fn take_str(&mut self) -> Result<Option<&'xml str>, Error> {
match self.next() { loop {
Some(Ok(Node::AttributeValue(s))) => Ok(Some(s)), match self.next() {
Some(Ok(Node::Text(s))) => Ok(Some(s)), Some(Ok(Node::AttributeValue(s))) => return Ok(Some(s)),
Some(Ok(node)) => Err(Error::ExpectedScalar(format!("{node:?}"))), Some(Ok(Node::Text(s))) => return Ok(Some(s)),
Some(Err(e)) => Err(e), Some(Ok(Node::Attribute(_))) => continue,
None => Ok(None), Some(Ok(node)) => return Err(Error::ExpectedScalar(format!("{node:?}"))),
Some(Err(e)) => return Err(e),
None => return Ok(None),
}
} }
} }

View File

@ -74,3 +74,21 @@ fn scalars() {
}) })
); );
} }
#[derive(Debug, FromXml, PartialEq)]
struct ScalarElementAttr {
s: String,
}
#[test]
fn scalar_element_attr() {
assert_eq!(
from_str::<ScalarElementAttr>(
"<ScalarElementAttr><s lang=\"en\">hello</s></ScalarElementAttr>"
)
.unwrap(),
ScalarElementAttr {
s: "hello".to_string(),
}
);
}