Improve error reporting, allow 0/1 as bool values

This commit is contained in:
Dirkjan Ochtman 2022-12-02 09:08:36 +01:00
parent fbe513ab5a
commit f4fb07fca6
5 changed files with 30 additions and 20 deletions

View File

@ -48,7 +48,7 @@ fn deserialize_scalar_enum(
}; };
let serialize_as = meta.serialize_as; let serialize_as = meta.serialize_as;
variants.extend(quote!(Ok(#serialize_as) => #ident::#v_ident,)); variants.extend(quote!(#serialize_as => #ident::#v_ident,));
} }
let generics = meta.xml_generics(BTreeSet::new()); let generics = meta.xml_generics(BTreeSet::new());
@ -75,9 +75,9 @@ fn deserialize_scalar_enum(
return Err(Error::DuplicateValue); return Err(Error::DuplicateValue);
} }
let value = match deserializer.take_str() { let value = match deserializer.take_str()? {
#variants #variants
_ => return Err(Error::UnexpectedValue("enum variant not found")), val => return Err(Error::UnexpectedValue(format!("enum variant not found for '{}'", val))),
}; };
*into = Some(value); *into = Some(value);

View File

@ -1,3 +1,4 @@
use std::any::type_name;
use std::borrow::Cow; use std::borrow::Cow;
use std::fmt; use std::fmt;
use std::net::IpAddr; use std::net::IpAddr;
@ -34,7 +35,10 @@ impl<'xml, T: FromStr> FromXml<'xml> for FromXmlStr<T> {
*into = Some(FromXmlStr(value)); *into = Some(FromXmlStr(value));
Ok(()) Ok(())
} }
Err(_) => Err(Error::UnexpectedValue("unable to parse value")), Err(_) => Err(Error::UnexpectedValue(format!(
"unable to parse {} from `{value}`",
type_name::<T>()
))),
} }
} }
@ -58,16 +62,19 @@ impl<'xml> FromXml<'xml> for bool {
return Err(Error::DuplicateValue); return Err(Error::DuplicateValue);
} }
let mut value = None; let value = match deserializer.take_str()? {
FromXmlStr::<Self>::deserialize(deserializer, &mut value)?; "true" | "1" => true,
match value { "false" | "0" => false,
Some(value) => { val => {
*into = Some(value.0); return Err(Error::UnexpectedValue(format!(
"unable to parse bool from '{val}'"
)))
}
};
*into = Some(value);
Ok(()) Ok(())
} }
None => Err(Error::MissingValue(Kind::Scalar)),
}
}
const KIND: Kind = Kind::Scalar; const KIND: Kind = Kind::Scalar;
} }
@ -241,9 +248,9 @@ impl<'xml> FromXml<'xml> for &'xml str {
match decode(value) { match decode(value) {
Cow::Borrowed(str) => *into = Some(str), Cow::Borrowed(str) => *into = Some(str),
Cow::Owned(_) => { Cow::Owned(_) => {
return Err(Error::UnexpectedValue( return Err(Error::UnexpectedValue(format!(
"string with escape characters cannot be deserialized as &str", "string with escape characters cannot be deserialized as &str: '{value}'",
)) )))
} }
} }

View File

@ -53,7 +53,10 @@ pub fn from_str<'xml, T: FromXml<'xml>>(input: &'xml str) -> Result<T, Error> {
let id = context.element_id(&root)?; let id = context.element_id(&root)?;
if !T::matches(id, None) { if !T::matches(id, None) {
return Err(Error::UnexpectedValue("unexpected root")); return Err(Error::UnexpectedValue(format!(
"unexpected root element {} in namespace {}",
id.name, id.ns
)));
} }
let mut value = None; let mut value = None;
@ -91,8 +94,8 @@ pub enum Error {
Other(std::string::String), Other(std::string::String),
#[error("unexpected end of stream")] #[error("unexpected end of stream")]
UnexpectedEndOfStream, UnexpectedEndOfStream,
#[error("unexpected value")] #[error("unexpected value: '{0}'")]
UnexpectedValue(&'static str), UnexpectedValue(String),
#[error("unexpected tag: {0}")] #[error("unexpected tag: {0}")]
UnexpectedTag(String), UnexpectedTag(String),
#[error("missing tag")] #[error("missing tag")]

View File

@ -39,7 +39,7 @@ fn default_namespaces() {
from_str( from_str(
"<NestedDe xmlns=\"WRONG\" xmlns:bar=\"BAZ\"><bar:flag>true</bar:flag></NestedDe>" "<NestedDe xmlns=\"WRONG\" xmlns:bar=\"BAZ\"><bar:flag>true</bar:flag></NestedDe>"
), ),
Err::<NestedDe, _>(Error::UnexpectedValue("unexpected root")) Err::<NestedDe, _>(Error::UnexpectedValue("unexpected root element NestedDe in namespace WRONG".to_owned()))
); );
// Correct child namespace // Correct child namespace

View File

@ -31,7 +31,7 @@ fn escape_back() {
from_str( from_str(
"<StructSpecialEntities xmlns=\"URI\"><string>&lt;&gt;&amp;&quot;&apos;adsad&quot;</string><str>str&amp;</str></StructSpecialEntities>" "<StructSpecialEntities xmlns=\"URI\"><string>&lt;&gt;&amp;&quot;&apos;adsad&quot;</string><str>str&amp;</str></StructSpecialEntities>"
), ),
Err::<StructSpecialEntities, _>(Error::UnexpectedValue("string with escape characters cannot be deserialized as &str")) Err::<StructSpecialEntities, _>(Error::UnexpectedValue("string with escape characters cannot be deserialized as &str: 'str&amp;'".to_owned()))
); );
// Borrowed // Borrowed