Handle default ns prefix
This commit is contained in:
parent
57dce9057e
commit
c7e68694cd
1
.gitignore
vendored
1
.gitignore
vendored
@ -18,3 +18,4 @@ Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
**/.idea
|
@ -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#"
|
||||
<book xmlns="http://www.sample.com/ns/domain">
|
||||
<author>Antoine de Saint-Exupéry</author>
|
||||
<title>Little prince</title>
|
||||
</book>
|
||||
"#;
|
||||
|
||||
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);
|
||||
}
|
@ -9,7 +9,7 @@ pub struct YaSerdeAttribute {
|
||||
pub default: Option<String>,
|
||||
pub default_namespace: Option<String>,
|
||||
pub flatten: bool,
|
||||
pub namespaces: BTreeMap<String, String>,
|
||||
pub namespaces: BTreeMap<Option<String>, String>,
|
||||
pub prefix: Option<String>,
|
||||
pub rename: Option<String>,
|
||||
pub skip_serializing_if: Option<String>,
|
||||
@ -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 {
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user