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

View File

@ -1,3 +1,4 @@
use std::any::type_name;
use std::borrow::Cow;
use std::fmt;
use std::net::IpAddr;
@ -34,7 +35,10 @@ impl<'xml, T: FromStr> FromXml<'xml> for FromXmlStr<T> {
*into = Some(FromXmlStr(value));
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);
}
let mut value = None;
FromXmlStr::<Self>::deserialize(deserializer, &mut value)?;
match value {
Some(value) => {
*into = Some(value.0);
let value = match deserializer.take_str()? {
"true" | "1" => true,
"false" | "0" => false,
val => {
return Err(Error::UnexpectedValue(format!(
"unable to parse bool from '{val}'"
)))
}
};
*into = Some(value);
Ok(())
}
None => Err(Error::MissingValue(Kind::Scalar)),
}
}
const KIND: Kind = Kind::Scalar;
}
@ -241,9 +248,9 @@ impl<'xml> FromXml<'xml> for &'xml str {
match decode(value) {
Cow::Borrowed(str) => *into = Some(str),
Cow::Owned(_) => {
return Err(Error::UnexpectedValue(
"string with escape characters cannot be deserialized as &str",
))
return Err(Error::UnexpectedValue(format!(
"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)?;
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;
@ -91,8 +94,8 @@ pub enum Error {
Other(std::string::String),
#[error("unexpected end of stream")]
UnexpectedEndOfStream,
#[error("unexpected value")]
UnexpectedValue(&'static str),
#[error("unexpected value: '{0}'")]
UnexpectedValue(String),
#[error("unexpected tag: {0}")]
UnexpectedTag(String),
#[error("missing tag")]

View File

@ -39,7 +39,7 @@ fn default_namespaces() {
from_str(
"<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

View File

@ -31,7 +31,7 @@ fn escape_back() {
from_str(
"<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