Merge pull request #70 from media-io/extend_flatten_support
Extend Flatten support
This commit is contained in:
commit
158bf492f7
166
yaserde/tests/de_flatten.rs
Normal file
166
yaserde/tests/de_flatten.rs
Normal file
@ -0,0 +1,166 @@
|
||||
#[macro_use]
|
||||
extern crate yaserde_derive;
|
||||
|
||||
use std::io::Read;
|
||||
use yaserde::de::from_str;
|
||||
use yaserde::YaDeserialize;
|
||||
|
||||
macro_rules! convert_and_validate {
|
||||
($content: expr, $struct: tt, $model: expr) => {
|
||||
let loaded: Result<$struct, String> = from_str($content);
|
||||
assert_eq!(loaded, Ok($model));
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn de_root_flatten_struct() {
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
||||
#[yaserde(flatten)]
|
||||
struct Content {
|
||||
binary_data: String,
|
||||
string_data: String,
|
||||
}
|
||||
|
||||
let content = r#"
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<binary_data>binary</binary_data>
|
||||
<string_data>string</string_data>
|
||||
"#;
|
||||
|
||||
convert_and_validate!(
|
||||
content,
|
||||
Content,
|
||||
Content {
|
||||
binary_data: "binary".to_string(),
|
||||
string_data: "string".to_string(),
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn de_root_flatten_enum() {
|
||||
#[derive(PartialEq, Debug, YaDeserialize)]
|
||||
#[yaserde(flatten)]
|
||||
pub enum Content {
|
||||
Binary(Binary),
|
||||
Data(Data),
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl Default for Content {
|
||||
fn default() -> Self {
|
||||
Content::Unknown
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
||||
pub struct Binary {
|
||||
binary_data: String,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
||||
pub struct Data {
|
||||
string_data: String,
|
||||
}
|
||||
|
||||
let content = r#"
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Binary>
|
||||
<binary_data>binary</binary_data>
|
||||
</Binary>
|
||||
"#;
|
||||
|
||||
convert_and_validate!(
|
||||
content,
|
||||
Content,
|
||||
Content::Binary(Binary {
|
||||
binary_data: "binary".to_string(),
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn de_flatten() {
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
||||
struct DateTime {
|
||||
#[yaserde(flatten)]
|
||||
date: Date,
|
||||
time: String,
|
||||
#[yaserde(flatten)]
|
||||
kind: DateKind,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
||||
struct Date {
|
||||
year: i32,
|
||||
month: i32,
|
||||
day: i32,
|
||||
#[yaserde(flatten)]
|
||||
extra: Extra,
|
||||
#[yaserde(flatten)]
|
||||
optional_extra: Option<OptionalExtra>,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
||||
pub struct Extra {
|
||||
week: i32,
|
||||
century: i32,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
||||
pub struct OptionalExtra {
|
||||
lunar_day: i32,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug, YaDeserialize)]
|
||||
pub enum DateKind {
|
||||
#[yaserde(rename = "holidays")]
|
||||
Holidays(Vec<String>),
|
||||
#[yaserde(rename = "working")]
|
||||
Working,
|
||||
}
|
||||
|
||||
impl Default for DateKind {
|
||||
fn default() -> Self {
|
||||
DateKind::Working
|
||||
}
|
||||
};
|
||||
|
||||
let content = r#"
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<DateTime>
|
||||
<year>2020</year>
|
||||
<month>1</month>
|
||||
<day>1</day>
|
||||
<week>1</week>
|
||||
<century>21</century>
|
||||
<lunar_day>1</lunar_day>
|
||||
<time>10:40:03</time>
|
||||
<holidays>New Year's Day</holidays>
|
||||
<holidays>Novy God Day</holidays>
|
||||
<holidays>Polar Bear Swim Day</holidays>
|
||||
</DateTime>
|
||||
"#;
|
||||
convert_and_validate!(
|
||||
content,
|
||||
DateTime,
|
||||
DateTime {
|
||||
date: Date {
|
||||
year: 2020,
|
||||
month: 1,
|
||||
day: 1,
|
||||
extra: Extra {
|
||||
week: 1,
|
||||
century: 21,
|
||||
},
|
||||
optional_extra: Some(OptionalExtra { lunar_day: 1 }),
|
||||
},
|
||||
time: "10:40:03".to_string(),
|
||||
kind: DateKind::Holidays(vec![
|
||||
"New Year's Day".into(),
|
||||
"Novy God Day".into(),
|
||||
"Polar Bear Swim Day".into()
|
||||
])
|
||||
}
|
||||
);
|
||||
}
|
||||
@ -691,92 +691,6 @@ fn de_custom() {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn de_flatten() {
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
||||
struct DateTime {
|
||||
#[yaserde(flatten)]
|
||||
date: Date,
|
||||
time: String,
|
||||
#[yaserde(flatten)]
|
||||
kind: DateKind,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
||||
struct Date {
|
||||
year: i32,
|
||||
month: i32,
|
||||
day: i32,
|
||||
#[yaserde(flatten)]
|
||||
extra: Extra,
|
||||
#[yaserde(flatten)]
|
||||
optional_extra: Option<OptionalExtra>,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
||||
pub struct Extra {
|
||||
week: i32,
|
||||
century: i32,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
||||
pub struct OptionalExtra {
|
||||
lunar_day: i32,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug, YaDeserialize)]
|
||||
pub enum DateKind {
|
||||
#[yaserde(rename = "holidays")]
|
||||
Holidays(Vec<String>),
|
||||
#[yaserde(rename = "working")]
|
||||
Working,
|
||||
}
|
||||
|
||||
impl Default for DateKind {
|
||||
fn default() -> Self {
|
||||
DateKind::Working
|
||||
}
|
||||
};
|
||||
|
||||
let content = r#"
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<DateTime>
|
||||
<year>2020</year>
|
||||
<month>1</month>
|
||||
<day>1</day>
|
||||
<week>1</week>
|
||||
<century>21</century>
|
||||
<lunar_day>1</lunar_day>
|
||||
<time>10:40:03</time>
|
||||
<holidays>New Year's Day</holidays>
|
||||
<holidays>Novy God Day</holidays>
|
||||
<holidays>Polar Bear Swim Day</holidays>
|
||||
</DateTime>
|
||||
"#;
|
||||
convert_and_validate!(
|
||||
content,
|
||||
DateTime,
|
||||
DateTime {
|
||||
date: Date {
|
||||
year: 2020,
|
||||
month: 1,
|
||||
day: 1,
|
||||
extra: Extra {
|
||||
week: 1,
|
||||
century: 21,
|
||||
},
|
||||
optional_extra: Some(OptionalExtra { lunar_day: 1 }),
|
||||
},
|
||||
time: "10:40:03".to_string(),
|
||||
kind: DateKind::Holidays(vec![
|
||||
"New Year's Day".into(),
|
||||
"Novy God Day".into(),
|
||||
"Polar Bear Swim Day".into()
|
||||
])
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn de_subitem_issue_12() {
|
||||
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
||||
|
||||
156
yaserde/tests/ser_flatten.rs
Normal file
156
yaserde/tests/ser_flatten.rs
Normal file
@ -0,0 +1,156 @@
|
||||
#[macro_use]
|
||||
extern crate yaserde_derive;
|
||||
|
||||
use std::io::Write;
|
||||
use yaserde::ser::to_string;
|
||||
use yaserde::YaSerialize;
|
||||
|
||||
macro_rules! convert_and_validate {
|
||||
($model: expr, $content: expr) => {
|
||||
let data: Result<String, String> = to_string(&$model);
|
||||
assert_eq!(
|
||||
data,
|
||||
Ok(
|
||||
String::from($content)
|
||||
.split("\n")
|
||||
.map(|s| s.trim())
|
||||
.collect::<String>()
|
||||
)
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ser_flatten() {
|
||||
#[derive(Default, PartialEq, Debug, YaSerialize)]
|
||||
struct DateTime {
|
||||
#[yaserde(flatten)]
|
||||
date: Date,
|
||||
time: String,
|
||||
#[yaserde(flatten)]
|
||||
kind: DateKind,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaSerialize)]
|
||||
struct Date {
|
||||
year: i32,
|
||||
month: i32,
|
||||
day: i32,
|
||||
#[yaserde(flatten)]
|
||||
extra: Extra,
|
||||
#[yaserde(flatten)]
|
||||
optional_extra: Option<OptionalExtra>,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaSerialize)]
|
||||
pub struct Extra {
|
||||
week: i32,
|
||||
century: i32,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaSerialize)]
|
||||
pub struct OptionalExtra {
|
||||
lunar_day: i32,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug, YaSerialize)]
|
||||
pub enum DateKind {
|
||||
#[yaserde(rename = "holidays")]
|
||||
Holidays(Vec<String>),
|
||||
#[yaserde(rename = "working")]
|
||||
Working,
|
||||
}
|
||||
|
||||
impl Default for DateKind {
|
||||
fn default() -> Self {
|
||||
DateKind::Working
|
||||
}
|
||||
};
|
||||
|
||||
let model = DateTime {
|
||||
date: Date {
|
||||
year: 2020,
|
||||
month: 1,
|
||||
day: 1,
|
||||
extra: Extra {
|
||||
week: 1,
|
||||
century: 21,
|
||||
},
|
||||
optional_extra: Some(OptionalExtra { lunar_day: 1 }),
|
||||
},
|
||||
time: "10:40:03".to_string(),
|
||||
kind: DateKind::Holidays(vec![
|
||||
"New Year's Day".into(),
|
||||
"Novy God Day".into(),
|
||||
"Polar Bear Swim Day".into(),
|
||||
]),
|
||||
};
|
||||
|
||||
let content = r#"
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<DateTime>
|
||||
<year>2020</year>
|
||||
<month>1</month>
|
||||
<day>1</day>
|
||||
<week>1</week>
|
||||
<century>21</century>
|
||||
<lunar_day>1</lunar_day>
|
||||
<time>10:40:03</time>
|
||||
<holidays>New Year's Day</holidays>
|
||||
<holidays>Novy God Day</holidays>
|
||||
<holidays>Polar Bear Swim Day</holidays>
|
||||
</DateTime>"#;
|
||||
|
||||
convert_and_validate!(model, content);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ser_root_flatten_struct() {
|
||||
#[derive(YaSerialize, PartialEq, Debug)]
|
||||
#[yaserde(flatten)]
|
||||
pub struct Content {
|
||||
binary_data: String,
|
||||
string_data: String,
|
||||
}
|
||||
|
||||
let model = Content {
|
||||
binary_data: "binary".to_string(),
|
||||
string_data: "string".to_string(),
|
||||
};
|
||||
let content = r#"<?xml version="1.0" encoding="utf-8"?><binary_data>binary</binary_data><string_data>string</string_data>"#;
|
||||
convert_and_validate!(model, content);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ser_root_flatten_enum() {
|
||||
#[derive(YaSerialize, PartialEq, Debug)]
|
||||
#[yaserde(flatten)]
|
||||
pub enum Content {
|
||||
Binary(Binary),
|
||||
Data(Data),
|
||||
}
|
||||
|
||||
#[derive(YaSerialize, PartialEq, Debug)]
|
||||
pub struct Binary {
|
||||
binary_data: String,
|
||||
}
|
||||
|
||||
#[derive(YaSerialize, PartialEq, Debug)]
|
||||
pub struct Data {
|
||||
string_data: String,
|
||||
}
|
||||
|
||||
let model = Content::Binary(Binary {
|
||||
binary_data: "binary".to_string(),
|
||||
});
|
||||
let content =
|
||||
r#"<?xml version="1.0" encoding="utf-8"?><Binary><binary_data>binary</binary_data></Binary>"#;
|
||||
convert_and_validate!(model, content);
|
||||
|
||||
let model = Content::Data(Data {
|
||||
string_data: "string".to_string(),
|
||||
});
|
||||
let content =
|
||||
r#"<?xml version="1.0" encoding="utf-8"?><Data><string_data>string</string_data></Data>"#;
|
||||
convert_and_validate!(model, content);
|
||||
}
|
||||
@ -329,87 +329,3 @@ fn ser_custom() {
|
||||
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><Date><Year>2020</Year><Month>1</Month><DoubleDay>10</DoubleDay></Date>";
|
||||
convert_and_validate!(model, content);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ser_flatten() {
|
||||
#[derive(Default, PartialEq, Debug, YaSerialize)]
|
||||
struct DateTime {
|
||||
#[yaserde(flatten)]
|
||||
date: Date,
|
||||
time: String,
|
||||
#[yaserde(flatten)]
|
||||
kind: DateKind,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaSerialize)]
|
||||
struct Date {
|
||||
year: i32,
|
||||
month: i32,
|
||||
day: i32,
|
||||
#[yaserde(flatten)]
|
||||
extra: Extra,
|
||||
#[yaserde(flatten)]
|
||||
optional_extra: Option<OptionalExtra>,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaSerialize)]
|
||||
pub struct Extra {
|
||||
week: i32,
|
||||
century: i32,
|
||||
}
|
||||
|
||||
#[derive(Default, PartialEq, Debug, YaSerialize)]
|
||||
pub struct OptionalExtra {
|
||||
lunar_day: i32,
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Debug, YaSerialize)]
|
||||
pub enum DateKind {
|
||||
#[yaserde(rename = "holidays")]
|
||||
Holidays(Vec<String>),
|
||||
#[yaserde(rename = "working")]
|
||||
Working,
|
||||
}
|
||||
|
||||
impl Default for DateKind {
|
||||
fn default() -> Self {
|
||||
DateKind::Working
|
||||
}
|
||||
};
|
||||
|
||||
let model = DateTime {
|
||||
date: Date {
|
||||
year: 2020,
|
||||
month: 1,
|
||||
day: 1,
|
||||
extra: Extra {
|
||||
week: 1,
|
||||
century: 21,
|
||||
},
|
||||
optional_extra: Some(OptionalExtra { lunar_day: 1 }),
|
||||
},
|
||||
time: "10:40:03".to_string(),
|
||||
kind: DateKind::Holidays(vec![
|
||||
"New Year's Day".into(),
|
||||
"Novy God Day".into(),
|
||||
"Polar Bear Swim Day".into(),
|
||||
]),
|
||||
};
|
||||
|
||||
let content = r#"
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<DateTime>
|
||||
<year>2020</year>
|
||||
<month>1</month>
|
||||
<day>1</day>
|
||||
<week>1</week>
|
||||
<century>21</century>
|
||||
<lunar_day>1</lunar_day>
|
||||
<time>10:40:03</time>
|
||||
<holidays>New Year's Day</holidays>
|
||||
<holidays>Novy God Day</holidays>
|
||||
<holidays>Polar Bear Swim Day</holidays>
|
||||
</DateTime>"#;
|
||||
|
||||
convert_and_validate!(model, content);
|
||||
}
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
use crate::attribute::*;
|
||||
use crate::field_type::*;
|
||||
use proc_macro2::TokenStream;
|
||||
use std::collections::BTreeMap;
|
||||
use syn::spanned::Spanned;
|
||||
use syn::DataEnum;
|
||||
use syn::Fields;
|
||||
@ -11,7 +10,7 @@ pub fn parse(
|
||||
data_enum: &DataEnum,
|
||||
name: &Ident,
|
||||
root: &str,
|
||||
_namespaces: &BTreeMap<String, String>,
|
||||
root_attributes: &YaSerdeAttribute,
|
||||
) -> TokenStream {
|
||||
let match_to_enum: TokenStream = data_enum
|
||||
.variants
|
||||
@ -20,6 +19,8 @@ pub fn parse(
|
||||
.filter_map(|f| f)
|
||||
.collect();
|
||||
|
||||
let flatten = root_attributes.flatten;
|
||||
|
||||
quote! {
|
||||
use xml::reader::XmlEvent;
|
||||
use yaserde::Visitor;
|
||||
@ -47,7 +48,7 @@ pub fn parse(
|
||||
|
||||
match name.local_name.as_str() {
|
||||
#match_to_enum
|
||||
named_element => {
|
||||
_named_element => {
|
||||
let _root = reader.next_event();
|
||||
}
|
||||
}
|
||||
@ -68,6 +69,13 @@ pub fn parse(
|
||||
XmlEvent::Characters(ref text_content) => {
|
||||
let _root = reader.next_event();
|
||||
}
|
||||
XmlEvent::EndDocument => {
|
||||
if #flatten {
|
||||
break;
|
||||
}
|
||||
|
||||
return Err(format!("End of document, missing some content ?"))
|
||||
}
|
||||
event => {
|
||||
return Err(format!("unknown event {:?}", event))
|
||||
}
|
||||
|
||||
@ -11,13 +11,13 @@ pub fn parse(
|
||||
data_struct: &DataStruct,
|
||||
name: &Ident,
|
||||
root: &str,
|
||||
prefix: &Option<String>,
|
||||
namespaces: &BTreeMap<String, String>,
|
||||
root_attributes: &YaSerdeAttribute,
|
||||
) -> TokenStream {
|
||||
let namespaces_matches: TokenStream = namespaces
|
||||
let namespaces_matches: TokenStream = root_attributes
|
||||
.namespaces
|
||||
.iter()
|
||||
.map(|(p, ns)| {
|
||||
if prefix.as_ref() == Some(p) {
|
||||
if root_attributes.prefix.as_ref() == Some(p) {
|
||||
Some(quote!(#ns => {}))
|
||||
} else {
|
||||
None
|
||||
@ -199,7 +199,7 @@ pub fn parse(
|
||||
&action,
|
||||
&field_attrs,
|
||||
label,
|
||||
&namespaces,
|
||||
&root_attributes.namespaces,
|
||||
field.span(),
|
||||
)
|
||||
};
|
||||
@ -368,6 +368,8 @@ pub fn parse(
|
||||
build_code_for_unused_xml_events(&call_flatten_visitors)
|
||||
};
|
||||
|
||||
let flatten = root_attributes.flatten;
|
||||
|
||||
quote! {
|
||||
use xml::reader::{XmlEvent, EventReader};
|
||||
use xml::writer::EventWriter;
|
||||
@ -407,7 +409,6 @@ pub fn parse(
|
||||
|
||||
loop {
|
||||
let event = reader.peek()?.to_owned();
|
||||
|
||||
match event {
|
||||
XmlEvent::StartElement{ref name, ref attributes, ..} => {
|
||||
let mut skipped = false;
|
||||
@ -443,6 +444,11 @@ pub fn parse(
|
||||
#write_unused
|
||||
depth -= 1;
|
||||
}
|
||||
XmlEvent::EndDocument => {
|
||||
if #flatten {
|
||||
break;
|
||||
}
|
||||
}
|
||||
XmlEvent::Characters(ref text_content) => {
|
||||
#set_text
|
||||
let event = reader.next_event()?;
|
||||
|
||||
@ -16,16 +16,10 @@ pub fn expand_derive_deserialize(ast: &syn::DeriveInput) -> Result<TokenStream,
|
||||
let root = root_attrs.clone().root.unwrap_or_else(|| name.to_string());
|
||||
|
||||
let impl_block = match *data {
|
||||
syn::Data::Struct(ref data_struct) => expand_struct::parse(
|
||||
data_struct,
|
||||
name,
|
||||
&root,
|
||||
&root_attrs.prefix,
|
||||
&root_attrs.namespaces,
|
||||
),
|
||||
syn::Data::Enum(ref data_enum) => {
|
||||
expand_enum::parse(data_enum, name, &root, &root_attrs.namespaces)
|
||||
syn::Data::Struct(ref data_struct) => {
|
||||
expand_struct::parse(data_struct, name, &root, &root_attrs)
|
||||
}
|
||||
syn::Data::Enum(ref data_enum) => expand_enum::parse(data_enum, name, &root, &root_attrs),
|
||||
syn::Data::Union(ref _data_union) => unimplemented!(),
|
||||
};
|
||||
|
||||
|
||||
@ -1,7 +1,6 @@
|
||||
use crate::attribute::*;
|
||||
use crate::field_type::*;
|
||||
use proc_macro2::TokenStream;
|
||||
use std::collections::BTreeMap;
|
||||
use syn::spanned::Spanned;
|
||||
use syn::DataEnum;
|
||||
use syn::Fields;
|
||||
@ -11,8 +10,7 @@ pub fn serialize(
|
||||
data_enum: &DataEnum,
|
||||
name: &Ident,
|
||||
root: &str,
|
||||
namespaces: &BTreeMap<String, String>,
|
||||
default_namespace: &Option<String>,
|
||||
root_attributes: &YaSerdeAttribute,
|
||||
) -> TokenStream {
|
||||
let write_enum_content: TokenStream = data_enum
|
||||
.variants
|
||||
@ -224,10 +222,11 @@ pub fn serialize(
|
||||
.filter_map(|x| x)
|
||||
.collect();
|
||||
|
||||
let add_namespaces: TokenStream = namespaces
|
||||
let add_namespaces: TokenStream = root_attributes
|
||||
.namespaces
|
||||
.iter()
|
||||
.map(|(prefix, namespace)| {
|
||||
if let Some(dn) = default_namespace {
|
||||
if let Some(dn) = &root_attributes.default_namespace {
|
||||
if dn == prefix {
|
||||
return Some(quote!(
|
||||
.default_ns(#namespace)
|
||||
@ -241,6 +240,8 @@ pub fn serialize(
|
||||
.filter_map(|x| x)
|
||||
.collect();
|
||||
|
||||
let flatten = root_attributes.flatten;
|
||||
|
||||
quote! {
|
||||
use xml::writer::XmlEvent;
|
||||
|
||||
@ -250,7 +251,7 @@ pub fn serialize(
|
||||
-> Result<(), String> {
|
||||
let skip = writer.skip_start_end();
|
||||
|
||||
if !skip {
|
||||
if !#flatten && !skip {
|
||||
if let Some(label) = writer.get_start_event_name() {
|
||||
let struct_start_event = XmlEvent::start_element(label.as_ref());
|
||||
writer.write(struct_start_event).map_err(|e| e.to_string())?;
|
||||
@ -264,7 +265,7 @@ pub fn serialize(
|
||||
#write_enum_content
|
||||
}
|
||||
|
||||
if !skip {
|
||||
if !#flatten && !skip {
|
||||
let struct_end_event = XmlEvent::end_element();
|
||||
writer.write(struct_end_event).map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
@ -2,7 +2,6 @@ use crate::attribute::*;
|
||||
use crate::field_type::*;
|
||||
use crate::ser::element::*;
|
||||
use proc_macro2::TokenStream;
|
||||
use std::collections::BTreeMap;
|
||||
use std::string::ToString;
|
||||
use syn::spanned::Spanned;
|
||||
use syn::DataStruct;
|
||||
@ -12,8 +11,7 @@ pub fn serialize(
|
||||
data_struct: &DataStruct,
|
||||
name: &Ident,
|
||||
root: &str,
|
||||
namespaces: &BTreeMap<String, String>,
|
||||
default_namespace: &Option<String>,
|
||||
root_attributes: &YaSerdeAttribute,
|
||||
) -> TokenStream {
|
||||
let build_attributes: TokenStream = data_struct
|
||||
.fields
|
||||
@ -26,7 +24,7 @@ pub fn serialize(
|
||||
|
||||
let label = &field.ident;
|
||||
|
||||
let label_name = build_label_name(&field, &field_attrs, default_namespace);
|
||||
let label_name = build_label_name(&field, &field_attrs, &root_attributes.default_namespace);
|
||||
|
||||
get_field_type(field).and_then(|f| match f {
|
||||
FieldType::FieldTypeString
|
||||
@ -205,10 +203,11 @@ pub fn serialize(
|
||||
.filter_map(|x| x)
|
||||
.collect();
|
||||
|
||||
let add_namespaces: TokenStream = namespaces
|
||||
let add_namespaces: TokenStream = root_attributes
|
||||
.namespaces
|
||||
.iter()
|
||||
.map(|(prefix, namespace)| {
|
||||
if let Some(dn) = default_namespace {
|
||||
if let Some(dn) = &root_attributes.default_namespace {
|
||||
if dn == prefix {
|
||||
return Some(quote!(
|
||||
.default_ns(#namespace)
|
||||
@ -239,7 +238,7 @@ pub fn serialize(
|
||||
));
|
||||
}
|
||||
|
||||
let label_name = build_label_name(&field, &field_attrs, default_namespace);
|
||||
let label_name = build_label_name(&field, &field_attrs, &root_attributes.default_namespace);
|
||||
let conditions = condition_generator(label, &field_attrs);
|
||||
|
||||
get_field_type(field).and_then(|f| match f {
|
||||
@ -381,6 +380,8 @@ pub fn serialize(
|
||||
.filter_map(|x| x)
|
||||
.collect();
|
||||
|
||||
let flatten = root_attributes.flatten;
|
||||
|
||||
quote! {
|
||||
use xml::writer::XmlEvent;
|
||||
|
||||
@ -390,7 +391,7 @@ pub fn serialize(
|
||||
-> Result<(), String> {
|
||||
let skip = writer.skip_start_end();
|
||||
|
||||
if !skip {
|
||||
if !#flatten && !skip {
|
||||
let yaserde_label = writer.get_start_event_name().unwrap_or_else(|| #root.to_string());
|
||||
let struct_start_event = XmlEvent::start_element(yaserde_label.as_ref())#add_namespaces;
|
||||
#build_attributes
|
||||
@ -399,7 +400,7 @@ pub fn serialize(
|
||||
|
||||
#struct_inspector
|
||||
|
||||
if !skip {
|
||||
if !#flatten && !skip {
|
||||
let struct_end_event = XmlEvent::end_element();
|
||||
writer.write(struct_end_event).map_err(|e| e.to_string())?;
|
||||
}
|
||||
|
||||
@ -2,7 +2,7 @@ pub mod element;
|
||||
pub mod expand_enum;
|
||||
pub mod expand_struct;
|
||||
|
||||
use crate::attribute;
|
||||
use crate::attribute::YaSerdeAttribute;
|
||||
use proc_macro2::TokenStream;
|
||||
use syn;
|
||||
use syn::Ident;
|
||||
@ -12,13 +12,14 @@ pub fn expand_derive_serialize(ast: &syn::DeriveInput) -> Result<TokenStream, St
|
||||
let attrs = &ast.attrs;
|
||||
let data = &ast.data;
|
||||
|
||||
let root_attrs = attribute::YaSerdeAttribute::parse(attrs);
|
||||
let root_attrs = YaSerdeAttribute::parse(attrs);
|
||||
let root = root_attrs.clone().root.unwrap_or_else(|| name.to_string());
|
||||
|
||||
let prefix = if root_attrs.default_namespace == root_attrs.prefix {
|
||||
"".to_string()
|
||||
} else {
|
||||
root_attrs
|
||||
.clone()
|
||||
.prefix
|
||||
.map_or("".to_string(), |prefix| prefix + ":")
|
||||
};
|
||||
@ -26,20 +27,10 @@ pub fn expand_derive_serialize(ast: &syn::DeriveInput) -> Result<TokenStream, St
|
||||
let root = format!("{}{}", prefix, root);
|
||||
|
||||
let impl_block = match *data {
|
||||
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::Struct(ref data_struct) => {
|
||||
expand_struct::serialize(data_struct, name, &root, &root_attrs)
|
||||
}
|
||||
syn::Data::Enum(ref data_enum) => expand_enum::serialize(data_enum, name, &root, &root_attrs),
|
||||
syn::Data::Union(ref _data_union) => unimplemented!(),
|
||||
};
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user