add default_namespace attribute

issue #45
This commit is contained in:
Marc-Antoine Arnaud 2020-02-24 14:47:18 +01:00
parent 389077e17c
commit dc79806128
5 changed files with 84 additions and 31 deletions

View File

@ -161,6 +161,27 @@ fn ser_struct_default_namespace() {
convert_and_validate!(model, content);
}
#[test]
fn ser_struct_default_namespace_via_attribute() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(
root = "tt",
default_namespace = "ttml",
namespace = "ttml: http://www.w3.org/ns/ttml",
namespace = "ttm: http://www.w3.org/ns/ttml#metadata"
)]
pub struct XmlStruct {
item: String,
}
let model = XmlStruct {
item: "something".to_string(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><tt xmlns=\"http://www.w3.org/ns/ttml\" xmlns:ttm=\"http://www.w3.org/ns/ttml#metadata\"><item>something</item></tt>";
convert_and_validate!(model, content);
}
#[test]
fn de_struct_namespace_nested() {
#[derive(YaSerialize, Default, PartialEq, Debug)]

View File

@ -6,14 +6,15 @@ use syn::Attribute;
#[derive(Debug, PartialEq, Clone)]
pub struct YaSerdeAttribute {
pub attribute: bool,
pub default: Option<String>,
pub default_namespace: Option<String>,
pub flatten: bool,
pub namespaces: BTreeMap<String, String>,
pub prefix: Option<String>,
pub root: Option<String>,
pub rename: Option<String>,
pub prefix: Option<String>,
pub default: Option<String>,
pub namespaces: BTreeMap<String, String>,
pub attribute: bool,
pub text: bool,
pub flatten: bool,
}
fn get_value(iter: &mut IntoIter) -> Option<String> {
@ -33,13 +34,14 @@ fn get_value(iter: &mut IntoIter) -> Option<String> {
impl YaSerdeAttribute {
pub fn parse(attrs: &[Attribute]) -> YaSerdeAttribute {
let mut attribute = false;
let mut flatten = false;
let mut default = None;
let mut default_namespace = None;
let mut namespaces = BTreeMap::new();
let mut prefix = None;
let mut rename = None;
let mut root = None;
let mut default = None;
let mut text = false;
let mut flatten = false;
for attr in attrs.iter() {
let mut attr_iter = attr.clone().tokens.into_iter();
@ -54,6 +56,15 @@ impl YaSerdeAttribute {
"attribute" => {
attribute = true;
}
"default" => {
default = get_value(&mut attr_iter);
}
"default_namespace" => {
default_namespace = get_value(&mut attr_iter);
}
"flatten" => {
flatten = true;
}
"namespace" => {
if let Some(namespace) = get_value(&mut attr_iter) {
let splitted: Vec<&str> = namespace.split(": ").collect();
@ -74,15 +85,9 @@ impl YaSerdeAttribute {
"root" => {
root = get_value(&mut attr_iter);
}
"default" => {
default = get_value(&mut attr_iter);
}
"text" => {
text = true;
}
"flatten" => {
flatten = true;
}
_ => {}
}
}
@ -94,13 +99,14 @@ impl YaSerdeAttribute {
YaSerdeAttribute {
attribute,
default,
default_namespace,
flatten,
namespaces,
prefix,
rename,
root,
default,
text,
flatten,
}
}
}
@ -112,14 +118,15 @@ fn parse_empty_attributes() {
assert_eq!(
YaSerdeAttribute {
attribute: false,
default: None,
default_namespace: None,
flatten: false,
namespaces: BTreeMap::new(),
prefix: None,
root: None,
rename: None,
prefix: None,
default: None,
namespaces: BTreeMap::new(),
attribute: false,
text: false,
flatten: false,
},
attrs
);
@ -160,14 +167,15 @@ fn parse_attributes() {
assert_eq!(
YaSerdeAttribute {
attribute: true,
default: None,
default_namespace: None,
flatten: false,
namespaces: BTreeMap::new(),
prefix: None,
root: None,
rename: None,
prefix: None,
default: None,
namespaces: BTreeMap::new(),
attribute: true,
text: false,
flatten: false,
},
attrs
);

View File

@ -12,6 +12,7 @@ pub fn serialize(
name: &Ident,
root: &str,
namespaces: &BTreeMap<String, String>,
default_namespace: &Option<String>,
) -> TokenStream {
let write_enum_content: TokenStream = data_enum
.variants
@ -212,6 +213,13 @@ pub fn serialize(
let add_namespaces: TokenStream = namespaces
.iter()
.map(|(prefix, namespace)| {
if let Some(dn) = default_namespace {
if dn == prefix {
return Some(quote!(
.default_ns(#namespace)
));
}
}
Some(quote!(
.ns(#prefix, #namespace)
))

View File

@ -14,6 +14,7 @@ pub fn serialize(
name: &Ident,
root: &str,
namespaces: &BTreeMap<String, String>,
default_namespace: &Option<String>,
) -> TokenStream {
let build_attributes: TokenStream = data_struct
.fields
@ -208,6 +209,13 @@ pub fn serialize(
let add_namespaces: TokenStream = namespaces
.iter()
.map(|(prefix, namespace)| {
if let Some(dn) = default_namespace {
if dn == prefix {
return Some(quote!(
.default_ns(#namespace)
));
}
}
Some(quote!(
.ns(#prefix, #namespace)
))

View File

@ -22,12 +22,20 @@ pub fn expand_derive_serialize(ast: &syn::DeriveInput) -> Result<TokenStream, St
};
let impl_block = match *data {
syn::Data::Struct(ref data_struct) => {
expand_struct::serialize(data_struct, name, &root, &root_attrs.namespaces)
}
syn::Data::Enum(ref data_enum) => {
expand_enum::serialize(data_enum, name, &root, &root_attrs.namespaces)
}
syn::Data::Struct(ref data_struct) => expand_struct::serialize(
data_struct,
name,
&root,
&root_attrs.namespaces,
&root_attrs.default_namespace,
),
syn::Data::Enum(ref data_enum) => expand_enum::serialize(
data_enum,
name,
&root,
&root_attrs.namespaces,
&root_attrs.default_namespace,
),
syn::Data::Union(ref _data_union) => unimplemented!(),
};