From 48450b3700303df511803899812dabcc59874139 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Tue, 29 Nov 2022 09:57:42 +0100 Subject: [PATCH] Correct FromXml for Option> --- instant-xml/src/impls.rs | 29 ++++++++++++++++++----------- instant-xml/tests/option-vec.rs | 19 +++++++++++++++++++ 2 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 instant-xml/tests/option-vec.rs diff --git a/instant-xml/src/impls.rs b/instant-xml/src/impls.rs index 33390d9..ab2392f 100644 --- a/instant-xml/src/impls.rs +++ b/instant-xml/src/impls.rs @@ -237,18 +237,25 @@ impl<'xml, T: FromXml<'xml>> FromXml<'xml> for Option { deserializer: &mut Deserializer<'cx, 'xml>, into: &mut Option, ) -> Result<(), Error> { - if into.is_some() { - return Err(Error::DuplicateValue); - } - - let mut value = None; - ::deserialize(deserializer, &mut value)?; - match value { - Some(v) => { - *into = Some(Some(v)); - Ok(()) + match into.as_mut() { + Some(value) => { + ::deserialize(deserializer, value)?; + match value { + Some(_) => Ok(()), + None => Err(Error::MissingValue(&>::KIND)), + } + } + None => { + let mut value = None; + ::deserialize(deserializer, &mut value)?; + match value { + Some(value) => { + *into = Some(Some(value)); + Ok(()) + } + None => return Err(Error::MissingValue(&>::KIND)), + } } - None => Err(Error::MissingValue(&Kind::Scalar)), } } diff --git a/instant-xml/tests/option-vec.rs b/instant-xml/tests/option-vec.rs new file mode 100644 index 0000000..e776a93 --- /dev/null +++ b/instant-xml/tests/option-vec.rs @@ -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>, +} + +#[test] +fn option_vec() { + let v = Foo { + inner: Some(vec!["a".to_string(), "b".to_string()]), + }; + let xml = r#"ab"#; + + assert_eq!(xml, to_string(&v).unwrap()); + assert_eq!(v, from_str(xml).unwrap()); +}