diff --git a/instant-xml/src/de.rs b/instant-xml/src/de.rs index 0f2423f..e22c2c0 100644 --- a/instant-xml/src/de.rs +++ b/instant-xml/src/de.rs @@ -25,6 +25,26 @@ impl<'cx, 'xml> Deserializer<'cx, 'xml> { } } + pub fn take_str(&mut self) -> Result<&'xml str, Error> { + let (value, element) = match self.next() { + Some(Ok(Node::AttributeValue(s))) => (s, false), + Some(Ok(Node::Element(s))) => (s, true), + Some(Ok(_)) => return Err(Error::ExpectedScalar), + Some(Err(e)) => return Err(e), + None => return Ok(""), + }; + + if element { + match self.next() { + Some(Ok(_)) => return Err(Error::UnexpectedState), + Some(Err(e)) => return Err(e), + _ => {} + } + } + + Ok(value) + } + pub fn nested<'a>(&'a mut self, element: Element<'xml>) -> Deserializer<'a, 'xml> where 'cx: 'a, diff --git a/instant-xml/src/impls.rs b/instant-xml/src/impls.rs index ac240b7..9128b41 100644 --- a/instant-xml/src/impls.rs +++ b/instant-xml/src/impls.rs @@ -2,7 +2,7 @@ use std::borrow::Cow; use std::fmt; use std::str::FromStr; -use crate::de::{Kind, Node}; +use crate::de::Kind; use crate::ser::FieldAttribute; use crate::{Deserializer, Error, FromXml, Serializer, ToXml}; @@ -11,28 +11,11 @@ struct FromXmlStr(Option); impl<'xml, T: FromStr> FromXml<'xml> for FromXmlStr { fn deserialize(deserializer: &mut Deserializer<'_, 'xml>) -> Result { - let (value, element) = match deserializer.next() { - Some(Ok(Node::AttributeValue(s))) => (s, false), - Some(Ok(Node::Element(s))) => (s, true), - Some(Ok(_)) => return Err(Error::ExpectedScalar), - Some(Err(e)) => return Err(e), - None => return Ok(Self(None)), - }; - - let value = match T::from_str(value) { - Ok(value) => value, - Err(_) => return Err(Error::UnexpectedValue), - }; - - if element { - match deserializer.next() { - Some(Ok(_)) => return Err(Error::UnexpectedState), - Some(Err(e)) => return Err(e), - _ => {} - } + let value = deserializer.take_str()?; + match T::from_str(value) { + Ok(value) => Ok(Self(Some(value))), + Err(_) => Err(Error::UnexpectedValue), } - - Ok(Self(Some(value))) } const KIND: Kind = Kind::Scalar; @@ -153,24 +136,8 @@ impl<'xml> FromXml<'xml> for &'xml str { impl<'xml> FromXml<'xml> for Cow<'xml, str> { fn deserialize(deserializer: &mut Deserializer<'_, 'xml>) -> Result { - let (value, element) = match deserializer.next() { - Some(Ok(Node::AttributeValue(s))) => (s, false), - Some(Ok(Node::Element(s))) => (s, true), - Some(Ok(_)) => return Err(Error::ExpectedScalar), - Some(Err(e)) => return Err(e), - None => return Err(Error::MissingValue), - }; - - let value = escape_back(value); - if element { - match deserializer.next() { - Some(Ok(_)) => return Err(Error::UnexpectedState), - Some(Err(e)) => return Err(e), - _ => {} - } - } - - Ok(value) + let value = deserializer.take_str()?; + Ok(escape_back(value)) } const KIND: Kind = Kind::Scalar;