Correct FromXml for Option<Vec<_>>

This commit is contained in:
Dirkjan Ochtman 2022-11-29 09:57:42 +01:00
parent 0ea0eca53f
commit 48450b3700
2 changed files with 37 additions and 11 deletions

View File

@ -237,18 +237,25 @@ impl<'xml, T: FromXml<'xml>> FromXml<'xml> for Option<T> {
deserializer: &mut Deserializer<'cx, 'xml>,
into: &mut Option<Self>,
) -> Result<(), Error> {
if into.is_some() {
return Err(Error::DuplicateValue);
match into.as_mut() {
Some(value) => {
<T>::deserialize(deserializer, value)?;
match value {
Some(_) => Ok(()),
None => Err(Error::MissingValue(&<T as FromXml<'_>>::KIND)),
}
}
None => {
let mut value = None;
<T>::deserialize(deserializer, &mut value)?;
match value {
Some(v) => {
*into = Some(Some(v));
Some(value) => {
*into = Some(Some(value));
Ok(())
}
None => Err(Error::MissingValue(&Kind::Scalar)),
None => return Err(Error::MissingValue(&<T as FromXml<'_>>::KIND)),
}
}
}
}

View File

@ -0,0 +1,19 @@
use similar_asserts::assert_eq;
use instant_xml::{from_str, to_string, FromXml, ToXml};
#[derive(Debug, Eq, FromXml, PartialEq, ToXml)]
struct Foo {
inner: Option<Vec<String>>,
}
#[test]
fn option_vec() {
let v = Foo {
inner: Some(vec!["a".to_string(), "b".to_string()]),
};
let xml = r#"<Foo><inner>a</inner><inner>b</inner></Foo>"#;
assert_eq!(xml, to_string(&v).unwrap());
assert_eq!(v, from_str(xml).unwrap());
}