diff --git a/.gitignore b/.gitignore index 7e4a920..6fbd31e 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,4 @@ Cargo.lock # These are backup files generated by rustfmt **/*.rs.bk +**/.idea \ No newline at end of file diff --git a/yaserde/tests/namespace.rs b/yaserde/tests/namespace.rs index c6b5151..dce4606 100644 --- a/yaserde/tests/namespace.rs +++ b/yaserde/tests/namespace.rs @@ -468,3 +468,33 @@ fn struct_bad_namespace() { 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", + namespace = "http://www.sample.com/ns/domain" + )] + pub struct Book { + author: String, + 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); +} \ No newline at end of file diff --git a/yaserde_derive/src/common/attribute.rs b/yaserde_derive/src/common/attribute.rs index 6494844..1deb334 100644 --- a/yaserde_derive/src/common/attribute.rs +++ b/yaserde_derive/src/common/attribute.rs @@ -9,7 +9,7 @@ pub struct YaSerdeAttribute { pub default: Option, pub default_namespace: Option, pub flatten: bool, - pub namespaces: BTreeMap, + pub namespaces: BTreeMap, String>, pub prefix: Option, pub rename: Option, pub skip_serializing_if: Option, @@ -67,10 +67,10 @@ impl YaSerdeAttribute { if let Some(namespace) = get_value(&mut attr_iter) { let splitted: Vec<&str> = namespace.split(": ").collect(); if splitted.len() == 2 { - namespaces.insert(splitted[0].to_owned(), splitted[1].to_owned()); + namespaces.insert(Some(splitted[0].to_owned()), splitted[1].to_owned()); } if splitted.len() == 1 { - namespaces.insert("".to_owned(), splitted[0].to_owned()); + namespaces.insert(None, splitted[0].to_owned()); } } } @@ -139,7 +139,7 @@ impl YaSerdeAttribute { .namespaces .iter() .map(|(prefix, namespace)| { - if configured_prefix == Some(prefix.to_string()) { + if configured_prefix.eq(prefix) { Some(quote!(#namespace => {})) } else { None @@ -316,7 +316,7 @@ fn parse_attributes_with_values() { let attrs = YaSerdeAttribute::parse(&attributes); let mut namespaces = BTreeMap::new(); - namespaces.insert("example".to_string(), "http://example.org".to_string()); + namespaces.insert(Some("example".to_string()), "http://example.org".to_string()); assert_eq!( YaSerdeAttribute { diff --git a/yaserde_derive/src/ser/namespace.rs b/yaserde_derive/src/ser/namespace.rs index 5bf69c4..9e75894 100644 --- a/yaserde_derive/src/ser/namespace.rs +++ b/yaserde_derive/src/ser/namespace.rs @@ -7,12 +7,10 @@ pub fn generate_namespaces_definition(attributes: &YaSerdeAttribute) -> TokenStr .namespaces .iter() .map(|(prefix, namespace)| { - if let Some(dn) = &attributes.default_namespace { - if dn == prefix { + if attributes.default_namespace.eq(prefix) { return Some(quote!( .default_ns(#namespace) )); - } } Some(quote!( .ns(#prefix, #namespace)