#[macro_use] extern crate yaserde; #[macro_use] extern crate yaserde_derive; fn init() { let _ = env_logger::builder().is_test(true).try_init(); } #[test] fn struct_simple_namespace() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde( rename = "book", prefix = "ns", namespaces = { "ns" = "http://www.sample.com/ns/domain" } )] pub struct Book { #[yaserde(prefix = "ns")] author: String, #[yaserde(prefix = "ns")] title: String, } let content = r#" Antoine de Saint-Exupéry Little prince "#; let model = Book { author: "Antoine de Saint-Exupéry".to_owned(), title: "Little prince".to_owned(), }; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, Book); } #[test] fn struct_multiple_namespaces() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde( rename = "book", prefix = "ns", namespaces = { "ns" = "http://www.sample.com/ns/domain", "ns2" = "http://www.sample.com/ns/domain_2" } )] pub struct Book { #[yaserde(prefix = "ns")] author: String, #[yaserde(prefix = "ns2")] title: String, } let content = r#" Antoine de Saint-Exupéry Little prince "#; let model = Book { author: "Antoine de Saint-Exupéry".to_owned(), title: "Little prince".to_owned(), }; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, Book); } #[test] fn struct_partial_namespace() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde( rename = "book", prefix = "ns", namespaces = { "ns" = "http://www.sample.com/ns/domain" } )] pub struct Book { author: String, #[yaserde(prefix = "ns")] title: String, } let content = r#" Antoine de Saint-Exupéry Little prince "#; let model = Book { author: "Antoine de Saint-Exupéry".to_owned(), title: "Little prince".to_owned(), }; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, Book); } #[test] fn struct_sub_namespace_definition() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde( rename = "book", prefix = "ns", namespaces = { "ns" = "http://www.sample.com/ns/domain", "ns2" = "http://www.sample.com/ns/domain_2" } )] pub struct Book { #[yaserde(prefix = "ns")] author: String, #[yaserde(prefix = "ns2", namespaces = { "ns2" = "http://www.sample.com/ns/domain_2" })] title: String, } let content = r#" Antoine de Saint-Exupéry Little prince "#; let model = Book { author: "Antoine de Saint-Exupéry".to_owned(), title: "Little prince".to_owned(), }; // TODO support namespace for attribute to specify local namespace // serialize_and_validate!(model, content); deserialize_and_validate!(content, model, Book); } #[test] fn struct_namespace_nested() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(prefix = "nsa", namespaces = { "nsa" = "http://www.sample.com/ns/a" })] struct A { #[yaserde(prefix = "nsa")] alpha: i32, } #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(prefix = "nsb", namespaces = { "nsb" = "http://www.sample.com/ns/b" })] struct B { // Note that name `nested` resides in `nsb` though it has a type from `nsa` #[yaserde(prefix = "nsb")] nested: A, } let content = r#" 32 "#; let model = B { nested: A { alpha: 32 }, }; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, B); } #[test] fn struct_namespace_nested_defined_at_root() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(prefix = "nsa", namespaces = { "nsa" = "http://www.sample.com/ns/a" })] struct A { #[yaserde(prefix = "nsa")] alpha: i32, } #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde( prefix = "nsb", namespaces = { "nsb" = "http://www.sample.com/ns/b", "nsa" = "http://www.sample.com/ns/a" } )] struct B { // Note that name `nested` resides in `nsb` though it has a type from `nsa` #[yaserde(prefix = "nsb")] nested: A, } let content = r#" 32 "#; let model = B { nested: A { alpha: 32 }, }; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, B); } #[test] fn struct_attribute_namespace() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde( rename = "root", namespaces = { "ns1" = "http://www.sample.com/ns/domain1", "ns2" = "http://www.sample.com/ns/domain2" } )] pub struct XmlStruct { #[yaserde(prefix = "ns1")] item_1: String, #[yaserde(attribute = true, prefix = "ns2")] item_2: String, } let model = XmlStruct { item_1: "something 1".to_string(), item_2: "something 2".to_string(), }; let content = r#" something 1 "#; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); } #[test] fn struct_implicit_default_namespace() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde( rename = "tt", default_namespace = "ttml", namespaces = { "ttml" = "http://www.w3.org/ns/ttml", "ttm" = "http://www.w3.org/ns/ttml#metadata" } )] pub struct XmlStruct { item: String, } let model = XmlStruct { item: "something".to_string(), }; let content = r#"something"#; serialize_and_validate!(model, content); // TODO // deserialize_and_validate!(content, model, XmlStruct); } #[test] fn struct_explicit_default_namespace() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde( rename = "tt", default_namespace = "ttml", namespaces = { "ttml" = "http://www.w3.org/ns/ttml", "ttm" = "http://www.w3.org/ns/ttml#metadata" } )] pub struct XmlStruct { item: String, } let model = XmlStruct { item: "something".to_string(), }; let content = r#"something"#; serialize_and_validate!(model, content); // TODO // deserialize_and_validate!(content, model, XmlStruct); } #[test] fn struct_default_namespace_via_attribute_with_prefix() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde( rename = "tt", prefix = "TTML", default_namespace = "TTML", namespaces = { "TTML" = "http://www.w3.org/ns/ttml", "ttm" = "http://www.w3.org/ns/ttml#metadata" } )] pub struct XmlStruct { #[yaserde(prefix = "TTML")] item: String, } let model = XmlStruct { item: "something".to_string(), }; let content = r#"something"#; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); } #[test] fn enum_namespace() { init(); #[derive(Debug, Default, PartialEq, YaDeserialize, YaSerialize)] #[yaserde( rename = "root", prefix = "ns", namespaces = { "ns" = "http://www.sample.com/ns/domain" } )] pub enum XmlStruct { #[yaserde(prefix = "ns")] #[default] Item, } let content = r#" ns:Item "#; let model = XmlStruct::Item; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); } #[test] fn enum_multi_namespaces() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde( rename = "root", namespaces = { "ns1" = "http://www.sample.com/ns/domain1", "ns2" = "http://www.sample.com/ns/domain2" } )] #[derive(Default)] pub enum XmlStruct { #[yaserde(prefix = "ns1")] #[default] Item1, #[yaserde(prefix = "ns2")] Item2, } let model = XmlStruct::Item1; let content = r#" ns1:Item1 "#; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct::Item2; let content = r#" ns2:Item2 "#; serialize_and_validate!(model, content); // TODO // deserialize_and_validate!(content, model, XmlStruct); } #[test] fn enum_attribute_namespace() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde( rename = "rootA", prefix = "ns", namespaces = { "ns" = "http://www.sample.com/ns/domain" } )] #[derive(Default)] pub enum XmlStruct { #[yaserde(prefix = "ns")] #[default] Item, #[yaserde(prefix = "ns")] ItemWithField(String), } let content = r#" ns:Item "#; let model = XmlStruct::Item; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); let model = XmlStruct::ItemWithField("Value".to_string()); let content = r#"Value"#; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, XmlStruct); } #[test] fn struct_bad_namespace() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde( rename = "book", prefix = "ns", namespaces = { "ns" = "http://www.sample.com/ns/domain", "ns2" = "http://www.sample.com/ns/domain_2" } )] pub struct Book { #[yaserde(prefix = "ns")] author: String, #[yaserde(prefix = "ns2", namespaces = { "ns2" = "http://www.sample.com/ns/domain_2" })] title: String, } let content = r#" Antoine de Saint-Exupéry Little prince "#; let loaded: Result = yaserde::de::from_str(content); assert_eq!( loaded, Err("bad namespace for book, found http://www.sample.com/ns/domain2".to_string()) ); } #[test] fn struct_default_namespace_no_prefix() { init(); #[derive(Debug, PartialEq, YaDeserialize, YaSerialize)] #[yaserde(rename = "book", prefix = "ns", default_namespace = "ns", namespaces = { "ns" = "http://www.sample.com/ns/domain" })] pub struct Book { #[yaserde(prefix = "ns")] author: String, #[yaserde(prefix = "ns")] title: String, } let content = r#" Antoine de Saint-Exupéry Little prince "#; let model = Book { author: "Antoine de Saint-Exupéry".to_owned(), title: "Little prince".to_owned(), }; serialize_and_validate!(model, content); deserialize_and_validate!(content, model, Book); }