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

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());
}