From 8c618ada791e304fcb5510f5961034623e69e5bc Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Wed, 22 Feb 2023 22:05:54 +0100 Subject: [PATCH] Ignore attributes when looking for scalar values --- instant-xml/src/de.rs | 15 +++++++++------ instant-xml/tests/scalar.rs | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/instant-xml/src/de.rs b/instant-xml/src/de.rs index 6e4a8d6..fb489ce 100644 --- a/instant-xml/src/de.rs +++ b/instant-xml/src/de.rs @@ -31,12 +31,15 @@ impl<'cx, 'xml> Deserializer<'cx, 'xml> { } pub fn take_str(&mut self) -> Result, Error> { - match self.next() { - Some(Ok(Node::AttributeValue(s))) => Ok(Some(s)), - Some(Ok(Node::Text(s))) => Ok(Some(s)), - Some(Ok(node)) => Err(Error::ExpectedScalar(format!("{node:?}"))), - Some(Err(e)) => Err(e), - None => Ok(None), + loop { + match self.next() { + Some(Ok(Node::AttributeValue(s))) => return Ok(Some(s)), + Some(Ok(Node::Text(s))) => return Ok(Some(s)), + Some(Ok(Node::Attribute(_))) => continue, + Some(Ok(node)) => return Err(Error::ExpectedScalar(format!("{node:?}"))), + Some(Err(e)) => return Err(e), + None => return Ok(None), + } } } diff --git a/instant-xml/tests/scalar.rs b/instant-xml/tests/scalar.rs index 9b806a9..3720d2f 100644 --- a/instant-xml/tests/scalar.rs +++ b/instant-xml/tests/scalar.rs @@ -74,3 +74,21 @@ fn scalars() { }) ); } + +#[derive(Debug, FromXml, PartialEq)] +struct ScalarElementAttr { + s: String, +} + +#[test] +fn scalar_element_attr() { + assert_eq!( + from_str::( + "hello" + ) + .unwrap(), + ScalarElementAttr { + s: "hello".to_string(), + } + ); +}