From 69eca9c2a5e8a9cf01def203a3d894e34da19d24 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Sat, 26 Nov 2022 12:52:57 -0800 Subject: [PATCH] Improve error reporting for missing values --- instant-xml-macros/src/de.rs | 2 +- instant-xml/src/impls.rs | 6 ++++-- instant-xml/src/lib.rs | 4 ++-- instant-xml/tests/de-ns.rs | 2 +- instant-xml/tests/entities.rs | 2 +- 5 files changed, 9 insertions(+), 7 deletions(-) diff --git a/instant-xml-macros/src/de.rs b/instant-xml-macros/src/de.rs index e7ba7e9..8e8381c 100644 --- a/instant-xml-macros/src/de.rs +++ b/instant-xml-macros/src/de.rs @@ -63,7 +63,7 @@ fn deserialize_scalar_enum( let value = match deserializer.take_str() { #variants - _ => return Err(Error::UnexpectedValue), + _ => return Err(Error::UnexpectedValue("enum variant not found")), }; *into = Some(value); diff --git a/instant-xml/src/impls.rs b/instant-xml/src/impls.rs index 720de62..2a52992 100644 --- a/instant-xml/src/impls.rs +++ b/instant-xml/src/impls.rs @@ -25,7 +25,7 @@ impl<'xml, T: FromStr> FromXml<'xml> for FromXmlStr { *into = Some(FromXmlStr(value)); Ok(()) } - Err(_) => Err(Error::UnexpectedValue), + Err(_) => Err(Error::UnexpectedValue("unable to parse value")), } } @@ -205,7 +205,9 @@ impl<'xml> FromXml<'xml> for &'xml str { *into = Some(s); Ok(()) } - Some(Cow::Owned(_)) => Err(Error::UnexpectedValue), + Some(Cow::Owned(_)) => Err(Error::UnexpectedValue( + "string with escape characters cannot be deserialized as &str", + )), None => Err(Error::MissingValue(&Kind::Scalar)), } } diff --git a/instant-xml/src/lib.rs b/instant-xml/src/lib.rs index 01c6b23..e681ed0 100644 --- a/instant-xml/src/lib.rs +++ b/instant-xml/src/lib.rs @@ -60,7 +60,7 @@ pub fn from_str<'xml, T: FromXml<'xml>>(input: &'xml str) -> Result { }; if id != expected { - return Err(Error::UnexpectedValue); + return Err(Error::UnexpectedValue("unexpected root")); } let mut value = None; @@ -99,7 +99,7 @@ pub enum Error { #[error("unexpected end of stream")] UnexpectedEndOfStream, #[error("unexpected value")] - UnexpectedValue, + UnexpectedValue(&'static str), #[error("unexpected tag")] UnexpectedTag, #[error("missing tag")] diff --git a/instant-xml/tests/de-ns.rs b/instant-xml/tests/de-ns.rs index 2458f0b..c808711 100644 --- a/instant-xml/tests/de-ns.rs +++ b/instant-xml/tests/de-ns.rs @@ -39,7 +39,7 @@ fn default_namespaces() { from_str( "true" ), - Err::(Error::UnexpectedValue) + Err::(Error::UnexpectedValue("unexpected root")) ); // Correct child namespace diff --git a/instant-xml/tests/entities.rs b/instant-xml/tests/entities.rs index 91c57f6..3dac1b2 100644 --- a/instant-xml/tests/entities.rs +++ b/instant-xml/tests/entities.rs @@ -30,7 +30,7 @@ fn escape_back() { from_str( "<>&"'adsad"str&" ), - Err::(Error::UnexpectedValue) + Err::(Error::UnexpectedValue("string with escape characters cannot be deserialized as &str")) ); // Borrowed