Correct FromXml for Option<Vec<_>>
This commit is contained in:
parent
0ea0eca53f
commit
48450b3700
|
@ -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);
|
||||
}
|
||||
|
||||
let mut value = None;
|
||||
<T>::deserialize(deserializer, &mut value)?;
|
||||
match value {
|
||||
Some(v) => {
|
||||
*into = Some(Some(v));
|
||||
Ok(())
|
||||
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(value) => {
|
||||
*into = Some(Some(value));
|
||||
Ok(())
|
||||
}
|
||||
None => return Err(Error::MissingValue(&<T as FromXml<'_>>::KIND)),
|
||||
}
|
||||
}
|
||||
None => Err(Error::MissingValue(&Kind::Scalar)),
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
Loading…
Reference in New Issue