diff --git a/yaserde/tests/deserializer.rs b/yaserde/tests/deserializer.rs index dd212ce..08bc993 100644 --- a/yaserde/tests/deserializer.rs +++ b/yaserde/tests/deserializer.rs @@ -1,9 +1,11 @@ #[macro_use] +extern crate yaserde; +#[macro_use] extern crate yaserde_derive; -use std::io::Read; +use std::io::{Read, Write}; use yaserde::de::from_str; -use yaserde::YaDeserialize; +use yaserde::{YaDeserialize, YaSerialize}; macro_rules! convert_and_validate { ($content: expr, $struct: tt, $model: expr) => { @@ -168,6 +170,70 @@ fn de_attributes() { ); } +#[test] +fn de_attributes_custom_deserializer() { + mod other_mod { + use super::*; + + use xml::reader::XmlEvent; + + #[derive(Debug, Default, PartialEq)] + pub struct Attributes { + pub items: Vec, + } + + impl YaDeserialize for Attributes { + fn deserialize(reader: &mut yaserde::de::Deserializer) -> Result { + loop { + match reader.next_event()? { + XmlEvent::StartElement { .. } => {} + XmlEvent::Characters(ref text_content) => { + let items: Vec = text_content + .split(' ') + .map(|item| item.to_owned()) + .collect(); + return Ok(Attributes { items }); + } + _ => { + break; + } + } + } + + Err("Unable to parse attribute".to_string()) + } + } + } + + #[derive(Default, YaDeserialize, PartialEq, Debug)] + pub struct Struct { + #[yaserde(attribute)] + attr_option_string: Option, + #[yaserde(attribute)] + attr_option_struct: Option, + } + + convert_and_validate!( + r#""#, + Struct, + Struct { + attr_option_string: None, + attr_option_struct: None + } + ); + + convert_and_validate!( + r#""#, + Struct, + Struct { + attr_option_string: Some("some value".to_string()), + attr_option_struct: Some(other_mod::Attributes { + items: vec!["variant2".to_string(), "variant3".to_string()] + }) + } + ); +} + #[test] fn de_attributes_complex() { mod other_mod { @@ -841,7 +907,15 @@ fn de_same_field_name_sub_sub() { convert_and_validate!("", Struct, Struct::default()); convert_and_validate!( - "42", + " + + + + 42 + + + + ", Struct, Struct { sub: SubStruct { @@ -850,3 +924,37 @@ fn de_same_field_name_sub_sub() { } ); } + +#[test] +fn de_same_field_name_but_some_other_fields_or_something() { + #[derive(Default, PartialEq, Debug, YaDeserialize, YaSerialize)] + #[yaserde(rename = "foo")] + pub struct FooOuter { + pub other: bool, + #[yaserde(rename = "foo")] + pub foo: Option, + } + + #[derive(Default, PartialEq, Debug, YaDeserialize, YaSerialize)] + pub struct FooInner { + pub blah: bool, + } + + let content = r#" + + false + + false + + + "#; + + let model = FooOuter { + other: false, + foo: Some(FooInner { blah: false }), + }; + + serialize_and_validate!(model, content); + // TODO fix it + // deserialize_and_validate!(content, model, FooOuter); +} diff --git a/yaserde/tests/namespace.rs b/yaserde/tests/namespace.rs index 4052628..5c96149 100644 --- a/yaserde/tests/namespace.rs +++ b/yaserde/tests/namespace.rs @@ -407,7 +407,6 @@ fn enum_attribute_namespace() { let content = r#"Value"#; serialize_and_validate!(model, content); - // TODO deserialize_and_validate!(content, model, XmlStruct); } diff --git a/yaserde_derive/src/de/expand_enum.rs b/yaserde_derive/src/de/expand_enum.rs index fb1c37a..dba1625 100644 --- a/yaserde_derive/src/de/expand_enum.rs +++ b/yaserde_derive/src/de/expand_enum.rs @@ -51,7 +51,7 @@ pub fn parse( loop { match reader.peek()?.to_owned() { XmlEvent::StartElement{ref name, ref attributes, ..} => { - println!("{:?}", name.local_name.as_str()); + // trace!("{:?}", name.local_name.as_str()); match name.local_name.as_str() { #match_to_enum _named_element => {