parent
389077e17c
commit
dc79806128
@ -161,6 +161,27 @@ fn ser_struct_default_namespace() {
|
|||||||
convert_and_validate!(model, content);
|
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]
|
#[test]
|
||||||
fn de_struct_namespace_nested() {
|
fn de_struct_namespace_nested() {
|
||||||
#[derive(YaSerialize, Default, PartialEq, Debug)]
|
#[derive(YaSerialize, Default, PartialEq, Debug)]
|
||||||
|
|||||||
@ -6,14 +6,15 @@ use syn::Attribute;
|
|||||||
|
|
||||||
#[derive(Debug, PartialEq, Clone)]
|
#[derive(Debug, PartialEq, Clone)]
|
||||||
pub struct YaSerdeAttribute {
|
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 root: Option<String>,
|
||||||
pub rename: 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 text: bool,
|
||||||
pub flatten: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_value(iter: &mut IntoIter) -> Option<String> {
|
fn get_value(iter: &mut IntoIter) -> Option<String> {
|
||||||
@ -33,13 +34,14 @@ fn get_value(iter: &mut IntoIter) -> Option<String> {
|
|||||||
impl YaSerdeAttribute {
|
impl YaSerdeAttribute {
|
||||||
pub fn parse(attrs: &[Attribute]) -> YaSerdeAttribute {
|
pub fn parse(attrs: &[Attribute]) -> YaSerdeAttribute {
|
||||||
let mut attribute = false;
|
let mut attribute = false;
|
||||||
|
let mut flatten = false;
|
||||||
|
let mut default = None;
|
||||||
|
let mut default_namespace = None;
|
||||||
let mut namespaces = BTreeMap::new();
|
let mut namespaces = BTreeMap::new();
|
||||||
let mut prefix = None;
|
let mut prefix = None;
|
||||||
let mut rename = None;
|
let mut rename = None;
|
||||||
let mut root = None;
|
let mut root = None;
|
||||||
let mut default = None;
|
|
||||||
let mut text = false;
|
let mut text = false;
|
||||||
let mut flatten = false;
|
|
||||||
|
|
||||||
for attr in attrs.iter() {
|
for attr in attrs.iter() {
|
||||||
let mut attr_iter = attr.clone().tokens.into_iter();
|
let mut attr_iter = attr.clone().tokens.into_iter();
|
||||||
@ -54,6 +56,15 @@ impl YaSerdeAttribute {
|
|||||||
"attribute" => {
|
"attribute" => {
|
||||||
attribute = true;
|
attribute = true;
|
||||||
}
|
}
|
||||||
|
"default" => {
|
||||||
|
default = get_value(&mut attr_iter);
|
||||||
|
}
|
||||||
|
"default_namespace" => {
|
||||||
|
default_namespace = get_value(&mut attr_iter);
|
||||||
|
}
|
||||||
|
"flatten" => {
|
||||||
|
flatten = true;
|
||||||
|
}
|
||||||
"namespace" => {
|
"namespace" => {
|
||||||
if let Some(namespace) = get_value(&mut attr_iter) {
|
if let Some(namespace) = get_value(&mut attr_iter) {
|
||||||
let splitted: Vec<&str> = namespace.split(": ").collect();
|
let splitted: Vec<&str> = namespace.split(": ").collect();
|
||||||
@ -74,15 +85,9 @@ impl YaSerdeAttribute {
|
|||||||
"root" => {
|
"root" => {
|
||||||
root = get_value(&mut attr_iter);
|
root = get_value(&mut attr_iter);
|
||||||
}
|
}
|
||||||
"default" => {
|
|
||||||
default = get_value(&mut attr_iter);
|
|
||||||
}
|
|
||||||
"text" => {
|
"text" => {
|
||||||
text = true;
|
text = true;
|
||||||
}
|
}
|
||||||
"flatten" => {
|
|
||||||
flatten = true;
|
|
||||||
}
|
|
||||||
_ => {}
|
_ => {}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -94,13 +99,14 @@ impl YaSerdeAttribute {
|
|||||||
|
|
||||||
YaSerdeAttribute {
|
YaSerdeAttribute {
|
||||||
attribute,
|
attribute,
|
||||||
|
default,
|
||||||
|
default_namespace,
|
||||||
|
flatten,
|
||||||
namespaces,
|
namespaces,
|
||||||
prefix,
|
prefix,
|
||||||
rename,
|
rename,
|
||||||
root,
|
root,
|
||||||
default,
|
|
||||||
text,
|
text,
|
||||||
flatten,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -112,14 +118,15 @@ fn parse_empty_attributes() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
YaSerdeAttribute {
|
YaSerdeAttribute {
|
||||||
|
attribute: false,
|
||||||
|
default: None,
|
||||||
|
default_namespace: None,
|
||||||
|
flatten: false,
|
||||||
|
namespaces: BTreeMap::new(),
|
||||||
|
prefix: None,
|
||||||
root: None,
|
root: None,
|
||||||
rename: None,
|
rename: None,
|
||||||
prefix: None,
|
|
||||||
default: None,
|
|
||||||
namespaces: BTreeMap::new(),
|
|
||||||
attribute: false,
|
|
||||||
text: false,
|
text: false,
|
||||||
flatten: false,
|
|
||||||
},
|
},
|
||||||
attrs
|
attrs
|
||||||
);
|
);
|
||||||
@ -160,14 +167,15 @@ fn parse_attributes() {
|
|||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
YaSerdeAttribute {
|
YaSerdeAttribute {
|
||||||
|
attribute: true,
|
||||||
|
default: None,
|
||||||
|
default_namespace: None,
|
||||||
|
flatten: false,
|
||||||
|
namespaces: BTreeMap::new(),
|
||||||
|
prefix: None,
|
||||||
root: None,
|
root: None,
|
||||||
rename: None,
|
rename: None,
|
||||||
prefix: None,
|
|
||||||
default: None,
|
|
||||||
namespaces: BTreeMap::new(),
|
|
||||||
attribute: true,
|
|
||||||
text: false,
|
text: false,
|
||||||
flatten: false,
|
|
||||||
},
|
},
|
||||||
attrs
|
attrs
|
||||||
);
|
);
|
||||||
|
|||||||
@ -12,6 +12,7 @@ pub fn serialize(
|
|||||||
name: &Ident,
|
name: &Ident,
|
||||||
root: &str,
|
root: &str,
|
||||||
namespaces: &BTreeMap<String, String>,
|
namespaces: &BTreeMap<String, String>,
|
||||||
|
default_namespace: &Option<String>,
|
||||||
) -> TokenStream {
|
) -> TokenStream {
|
||||||
let write_enum_content: TokenStream = data_enum
|
let write_enum_content: TokenStream = data_enum
|
||||||
.variants
|
.variants
|
||||||
@ -212,6 +213,13 @@ pub fn serialize(
|
|||||||
let add_namespaces: TokenStream = namespaces
|
let add_namespaces: TokenStream = namespaces
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(prefix, namespace)| {
|
.map(|(prefix, namespace)| {
|
||||||
|
if let Some(dn) = default_namespace {
|
||||||
|
if dn == prefix {
|
||||||
|
return Some(quote!(
|
||||||
|
.default_ns(#namespace)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
Some(quote!(
|
Some(quote!(
|
||||||
.ns(#prefix, #namespace)
|
.ns(#prefix, #namespace)
|
||||||
))
|
))
|
||||||
|
|||||||
@ -14,6 +14,7 @@ pub fn serialize(
|
|||||||
name: &Ident,
|
name: &Ident,
|
||||||
root: &str,
|
root: &str,
|
||||||
namespaces: &BTreeMap<String, String>,
|
namespaces: &BTreeMap<String, String>,
|
||||||
|
default_namespace: &Option<String>,
|
||||||
) -> TokenStream {
|
) -> TokenStream {
|
||||||
let build_attributes: TokenStream = data_struct
|
let build_attributes: TokenStream = data_struct
|
||||||
.fields
|
.fields
|
||||||
@ -208,6 +209,13 @@ pub fn serialize(
|
|||||||
let add_namespaces: TokenStream = namespaces
|
let add_namespaces: TokenStream = namespaces
|
||||||
.iter()
|
.iter()
|
||||||
.map(|(prefix, namespace)| {
|
.map(|(prefix, namespace)| {
|
||||||
|
if let Some(dn) = default_namespace {
|
||||||
|
if dn == prefix {
|
||||||
|
return Some(quote!(
|
||||||
|
.default_ns(#namespace)
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
Some(quote!(
|
Some(quote!(
|
||||||
.ns(#prefix, #namespace)
|
.ns(#prefix, #namespace)
|
||||||
))
|
))
|
||||||
|
|||||||
@ -22,12 +22,20 @@ pub fn expand_derive_serialize(ast: &syn::DeriveInput) -> Result<TokenStream, St
|
|||||||
};
|
};
|
||||||
|
|
||||||
let impl_block = match *data {
|
let impl_block = match *data {
|
||||||
syn::Data::Struct(ref data_struct) => {
|
syn::Data::Struct(ref data_struct) => expand_struct::serialize(
|
||||||
expand_struct::serialize(data_struct, name, &root, &root_attrs.namespaces)
|
data_struct,
|
||||||
}
|
name,
|
||||||
syn::Data::Enum(ref data_enum) => {
|
&root,
|
||||||
expand_enum::serialize(data_enum, name, &root, &root_attrs.namespaces)
|
&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!(),
|
syn::Data::Union(ref _data_union) => unimplemented!(),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user