#[macro_use] extern crate yaserde; #[macro_use] extern crate yaserde_derive; #[test] fn basic_enum() { #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(rename = "base")] pub struct XmlStruct { color: Color, } #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(rename = "color")] #[derive(Default)] pub enum Color { #[default] White, Black, #[yaserde(rename = "custom")] Custom { enabled: String, u8_value: u8, i8_value: i8, u16_value: u16, i16_value: i16, u32_value: u32, i32_value: i32, u64_value: u64, i64_value: i64, f32_value: f32, f64_value: f64, color: RGBColor, alpha: Alpha, alphas: Vec, }, } assert_eq!(Color::default(), Color::White); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] pub struct RGBColor { red: String, green: String, blue: String, } #[derive(Debug, PartialEq, YaDeserialize, YaSerialize, Default)] pub enum Alpha { #[default] Transparent, Opaque, } let model = XmlStruct { color: Color::Black, }; let content = "Black"; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Color::Custom { enabled: "true".to_string(), u8_value: 8, i8_value: -8, u16_value: 16, i16_value: -16, u32_value: 32, i32_value: -32, u64_value: 64, i64_value: -64, f32_value: 32.0, f64_value: 64.0, color: RGBColor { red: "0".to_string(), green: "128".to_string(), blue: "255".to_string(), }, alpha: Alpha::Opaque, alphas: vec![Alpha::Opaque, Alpha::Transparent], }, }; let content = r#" true 8 -8 16 -16 32 -32 64 -64 32 64 0128255 Opaque Opaque Transparent "#; serialize_and_validate!(model, content); // TODO // deserialize_and_validate!(content, model, XmlStruct); } #[test] fn attribute_enum() { #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(rename = "base")] pub struct XmlStruct { #[yaserde(attribute = true)] color: Color, } #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(rename = "color")] #[derive(Default)] pub enum Color { #[yaserde(rename = "pink")] #[default] Pink, } let model = XmlStruct { color: Color::Pink }; let content = r#""#; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); } #[test] fn attribute_enum2() { #[derive(Debug, PartialEq, YaSerialize, YaDeserialize)] #[yaserde(rename = "child1")] struct Child1 { #[yaserde(attribute = true, rename = "val")] pub val: String, } impl Default for Child1 { fn default() -> Child1 { Child1 { val: "hello world".into(), } } } #[derive(Debug, PartialEq, YaSerialize, YaDeserialize)] #[yaserde(rename = "child2")] #[derive(Default)] struct Child2 { #[yaserde(attribute = true)] pub num: u8, } #[derive(Debug, PartialEq, YaSerialize, YaDeserialize)] #[yaserde(flatten = true)] enum Base { #[yaserde(flatten = true, rename = "child1")] C1(Child1), #[yaserde(flatten = true, rename = "child2")] C2(Child2), } impl Default for Base { fn default() -> Base { Base::C1(Child1 { val: "hello world".into(), }) } } let content = r#""#; let model = Base::C1(Child1 { val: "hello world".into(), }); serialize_and_validate!(model, content); deserialize_and_validate!(content, model, Base); let content = r#""#; let model = Base::C2(Child2 { num: 7 }); serialize_and_validate!(model, content); deserialize_and_validate!(content, model, Base); #[derive(Debug, PartialEq, YaSerialize, YaDeserialize)] #[yaserde(rename = "base")] enum Base2 { #[yaserde(flatten = true)] C1(Child1), } impl Default for Base2 { fn default() -> Base2 { Base2::C1(Child1 { val: "hello world".into(), }) } } let content = r#""#; let model = Base2::C1(Child1 { val: "hello world".into(), }); serialize_and_validate!(model, content); println!("{:?}", yaserde::de::from_str::(content)); deserialize_and_validate!(content, model, Base2); } #[test] fn unnamed_enum() { #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(rename = "base")] pub struct XmlStruct { color: Enum, } #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] pub struct OtherStruct { fi: i32, se: i32, } #[derive(Debug, PartialEq, YaDeserialize, YaSerialize, Default)] pub enum Enum { #[default] Simple, Field(String), FullPath(String), Integer(i32), UserStruct(OtherStruct), OptionString(Option), OptionUserStruct(Option), Strings(Vec), Ints(Vec), Structs(Vec), #[yaserde(rename = "renamed")] ToRename(u32), #[yaserde(rename = "renamed.with.dots")] ToRenameDots(u32), } let model = XmlStruct { color: Enum::Field("some_text".to_owned()), }; let content = "some_text"; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::FullPath("some_text".to_owned()), }; let content = "some_text"; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::Integer(56), }; let content = "56"; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::UserStruct(OtherStruct { fi: 24, se: 42 }), }; let content = "2442"; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::OptionString(Some("some_text".to_owned())), }; let content = "some_text"; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::OptionString(None), }; let content = ""; serialize_and_validate!(model, content); // TODO // deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::OptionUserStruct(Some(OtherStruct { fi: 12, se: 23 })), }; let content = "1223"; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::OptionUserStruct(None), }; let content = ""; serialize_and_validate!(model, content); // TODO // deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::Strings(vec!["abc".to_owned(), "def".to_owned()]), }; let content = "abcdef"; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::Ints(vec![23, 45]), }; let content = "2345"; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::Structs(vec![ OtherStruct { fi: 12, se: 23 }, OtherStruct { fi: 34, se: 45 }, ]), }; let content = "12233445"; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::ToRename(87), }; let content = "87"; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct { color: Enum::ToRenameDots(84), }; let content = "84"; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); } #[test] fn tagged_enum() { #[derive(Debug, PartialEq, YaSerialize, YaDeserialize, Default)] #[yaserde(tag = "type", rename = "foobar")] enum XmlEnum { #[default] #[yaserde(rename = "foo")] Foo, #[yaserde(rename = "bar")] Bar, } #[derive(Debug, PartialEq, YaSerialize, YaDeserialize, Default)] #[yaserde(rename = "base")] struct XmlStruct { #[yaserde(rename = "foobar")] foo_bar: XmlEnum, } let model = XmlEnum::Foo; let content = ""; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlEnum); let model = XmlEnum::Bar; let content = ""; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlEnum); let model = XmlStruct { foo_bar: XmlEnum::Foo, }; let content = ""; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); }