Skip to content

Commit

Permalink
add default_namespace attribute
Browse files Browse the repository at this point in the history
issue #45
  • Loading branch information
MarcAntoine-Arnaud committed Feb 24, 2020
1 parent 389077e commit dc79806
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 31 deletions.
21 changes: 21 additions & 0 deletions yaserde/tests/se_namespace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
58 changes: 33 additions & 25 deletions yaserde_derive/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ use syn::Attribute;

#[derive(Debug, PartialEq, Clone)]
pub struct YaSerdeAttribute {
pub root: Option<String>,
pub rename: Option<String>,
pub prefix: Option<String>,
pub attribute: bool,
pub default: Option<String>,
pub default_namespace: Option<String>,
pub flatten: bool,
pub namespaces: BTreeMap<String, String>,
pub attribute: bool,
pub prefix: Option<String>,
pub root: Option<String>,
pub rename: Option<String>,
pub text: bool,
pub flatten: bool,
}

fn get_value(iter: &mut IntoIter) -> Option<String> {
Expand All @@ -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();
Expand All @@ -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();
Expand All @@ -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;
}
_ => {}
}
}
Expand All @@ -94,13 +99,14 @@ impl YaSerdeAttribute {

YaSerdeAttribute {
attribute,
default,
default_namespace,
flatten,
namespaces,
prefix,
rename,
root,
default,
text,
flatten,
}
}
}
Expand All @@ -112,14 +118,15 @@ fn parse_empty_attributes() {

assert_eq!(
YaSerdeAttribute {
root: None,
rename: None,
prefix: None,
attribute: false,
default: None,
default_namespace: None,
flatten: false,
namespaces: BTreeMap::new(),
attribute: false,
prefix: None,
root: None,
rename: None,
text: false,
flatten: false,
},
attrs
);
Expand Down Expand Up @@ -160,14 +167,15 @@ fn parse_attributes() {

assert_eq!(
YaSerdeAttribute {
root: None,
rename: None,
prefix: None,
attribute: true,
default: None,
default_namespace: None,
flatten: false,
namespaces: BTreeMap::new(),
attribute: true,
prefix: None,
root: None,
rename: None,
text: false,
flatten: false,
},
attrs
);
Expand Down
8 changes: 8 additions & 0 deletions yaserde_derive/src/ser/expand_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
))
Expand Down
8 changes: 8 additions & 0 deletions yaserde_derive/src/ser/expand_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
))
Expand Down
20 changes: 14 additions & 6 deletions yaserde_derive/src/ser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(),
};

Expand Down

0 comments on commit dc79806

Please sign in to comment.