support Option type value
This commit is contained in:
parent
ef37615458
commit
266b77428b
116
yaserde/tests/der_option.rs
Normal file
116
yaserde/tests/der_option.rs
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
#[macro_use]
|
||||||
|
extern crate log;
|
||||||
|
extern crate xml;
|
||||||
|
extern crate yaserde;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate yaserde_derive;
|
||||||
|
|
||||||
|
use std::io::Read;
|
||||||
|
use yaserde::YaDeserialize;
|
||||||
|
use yaserde::de::from_str;
|
||||||
|
|
||||||
|
macro_rules! convert_and_validate {
|
||||||
|
($type: ty, $value: expr, $content: expr) => {{
|
||||||
|
#[derive(YaDeserialize, PartialEq, Debug)]
|
||||||
|
#[yaserde(root = "data")]
|
||||||
|
pub struct Data {
|
||||||
|
item: Option<$type>,
|
||||||
|
}
|
||||||
|
|
||||||
|
let model = Data { item: $value };
|
||||||
|
|
||||||
|
let source = if let Some(content) = $content {
|
||||||
|
String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data><item>") + content
|
||||||
|
+ "</item></data>"
|
||||||
|
} else {
|
||||||
|
String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data />")
|
||||||
|
};
|
||||||
|
|
||||||
|
let loaded: Result<Data, String> = from_str(&source);
|
||||||
|
assert_eq!(loaded, Ok(model));
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! convert_and_validate_for_attribute {
|
||||||
|
($type: ty, $value: expr, $content: expr) => {{
|
||||||
|
#[derive(YaDeserialize, PartialEq, Debug)]
|
||||||
|
#[yaserde(root = "data")]
|
||||||
|
pub struct Data {
|
||||||
|
#[yaserde(attribute)]
|
||||||
|
item: Option<$type>,
|
||||||
|
}
|
||||||
|
|
||||||
|
let model = Data { item: $value };
|
||||||
|
|
||||||
|
let source = if let Some(content) = $content {
|
||||||
|
String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data item=\"") + content
|
||||||
|
+ "\"></data>"
|
||||||
|
} else {
|
||||||
|
String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data />")
|
||||||
|
};
|
||||||
|
|
||||||
|
let loaded: Result<Data, String> = from_str(&source);
|
||||||
|
assert_eq!(loaded, Ok(model));
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn de_option() {
|
||||||
|
convert_and_validate!(String, Some("test".to_string()), Some("test"));
|
||||||
|
convert_and_validate!(String, None, None);
|
||||||
|
convert_and_validate!(bool, Some(true), Some("true"));
|
||||||
|
convert_and_validate!(bool, None, None);
|
||||||
|
convert_and_validate!(u8, Some(12 as u8), Some("12"));
|
||||||
|
convert_and_validate!(u8, None, None);
|
||||||
|
convert_and_validate!(i8, Some(12 as i8), Some("12"));
|
||||||
|
convert_and_validate!(i8, Some(-12 as i8), Some("-12"));
|
||||||
|
convert_and_validate!(i8, None, None);
|
||||||
|
convert_and_validate!(u16, Some(12 as u16), Some("12"));
|
||||||
|
convert_and_validate!(u16, None, None);
|
||||||
|
convert_and_validate!(i16, Some(12 as i16), Some("12"));
|
||||||
|
convert_and_validate!(i16, Some(-12 as i16), Some("-12"));
|
||||||
|
convert_and_validate!(i16, None, None);
|
||||||
|
convert_and_validate!(u32, Some(12 as u32), Some("12"));
|
||||||
|
convert_and_validate!(u32, None, None);
|
||||||
|
convert_and_validate!(i32, Some(12 as i32), Some("12"));
|
||||||
|
convert_and_validate!(i32, Some(-12 as i32), Some("-12"));
|
||||||
|
convert_and_validate!(i32, None, None);
|
||||||
|
convert_and_validate!(u64, Some(12 as u64), Some("12"));
|
||||||
|
convert_and_validate!(u64, None, None);
|
||||||
|
convert_and_validate!(i64, Some(12 as i64), Some("12"));
|
||||||
|
convert_and_validate!(i64, Some(-12 as i64), Some("-12"));
|
||||||
|
convert_and_validate!(i64, None, None);
|
||||||
|
convert_and_validate!(f32, Some(-12.5_f32 as f32), Some("-12.5"));
|
||||||
|
convert_and_validate!(f32, None, None);
|
||||||
|
convert_and_validate!(f64, Some(-12.5 as f64), Some("-12.5"));
|
||||||
|
convert_and_validate!(f64, None, None);
|
||||||
|
|
||||||
|
convert_and_validate_for_attribute!(String, Some("test".to_string()), Some("test"));
|
||||||
|
convert_and_validate_for_attribute!(String, None, None);
|
||||||
|
convert_and_validate_for_attribute!(bool, Some(true), Some("true"));
|
||||||
|
convert_and_validate_for_attribute!(bool, None, None);
|
||||||
|
convert_and_validate_for_attribute!(u8, Some(12 as u8), Some("12"));
|
||||||
|
convert_and_validate_for_attribute!(u8, None, None);
|
||||||
|
convert_and_validate_for_attribute!(i8, Some(12 as i8), Some("12"));
|
||||||
|
convert_and_validate_for_attribute!(i8, Some(-12 as i8), Some("-12"));
|
||||||
|
convert_and_validate_for_attribute!(i8, None, None);
|
||||||
|
convert_and_validate_for_attribute!(u16, Some(12 as u16), Some("12"));
|
||||||
|
convert_and_validate_for_attribute!(u16, None, None);
|
||||||
|
convert_and_validate_for_attribute!(i16, Some(12 as i16), Some("12"));
|
||||||
|
convert_and_validate_for_attribute!(i16, Some(-12 as i16), Some("-12"));
|
||||||
|
convert_and_validate_for_attribute!(i16, None, None);
|
||||||
|
convert_and_validate_for_attribute!(u32, Some(12 as u32), Some("12"));
|
||||||
|
convert_and_validate_for_attribute!(u32, None, None);
|
||||||
|
convert_and_validate_for_attribute!(i32, Some(12 as i32), Some("12"));
|
||||||
|
convert_and_validate_for_attribute!(i32, Some(-12 as i32), Some("-12"));
|
||||||
|
convert_and_validate_for_attribute!(i32, None, None);
|
||||||
|
convert_and_validate_for_attribute!(u64, Some(12 as u64), Some("12"));
|
||||||
|
convert_and_validate_for_attribute!(u64, None, None);
|
||||||
|
convert_and_validate_for_attribute!(i64, Some(12 as i64), Some("12"));
|
||||||
|
convert_and_validate_for_attribute!(i64, Some(-12 as i64), Some("-12"));
|
||||||
|
convert_and_validate_for_attribute!(i64, None, None);
|
||||||
|
convert_and_validate_for_attribute!(f32, Some(-12.5 as f32), Some("-12.5"));
|
||||||
|
convert_and_validate_for_attribute!(f32, None, None);
|
||||||
|
convert_and_validate_for_attribute!(f64, Some(-12.5 as f64), Some("-12.5"));
|
||||||
|
convert_and_validate_for_attribute!(f64, None, None);
|
||||||
|
}
|
||||||
@ -48,6 +48,7 @@ macro_rules! convert_and_validate_for_attribute {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn de_type() {
|
fn de_type() {
|
||||||
|
convert_and_validate!(String, "test".to_string(), "test");
|
||||||
convert_and_validate!(bool, true, "true");
|
convert_and_validate!(bool, true, "true");
|
||||||
convert_and_validate!(u8, 12 as u8, "12");
|
convert_and_validate!(u8, 12 as u8, "12");
|
||||||
convert_and_validate!(i8, 12 as i8, "12");
|
convert_and_validate!(i8, 12 as i8, "12");
|
||||||
@ -64,6 +65,7 @@ fn de_type() {
|
|||||||
convert_and_validate!(f32, -12.5_f32 as f32, "-12.5");
|
convert_and_validate!(f32, -12.5_f32 as f32, "-12.5");
|
||||||
convert_and_validate!(f64, -12.5 as f64, "-12.5");
|
convert_and_validate!(f64, -12.5 as f64, "-12.5");
|
||||||
|
|
||||||
|
convert_and_validate_for_attribute!(String, "test".to_string(), "test");
|
||||||
convert_and_validate_for_attribute!(bool, true, "true");
|
convert_and_validate_for_attribute!(bool, true, "true");
|
||||||
convert_and_validate_for_attribute!(u8, 12 as u8, "12");
|
convert_and_validate_for_attribute!(u8, 12 as u8, "12");
|
||||||
convert_and_validate_for_attribute!(i8, 12 as i8, "12");
|
convert_and_validate_for_attribute!(i8, 12 as i8, "12");
|
||||||
|
|||||||
116
yaserde/tests/se_option.rs
Normal file
116
yaserde/tests/se_option.rs
Normal file
@ -0,0 +1,116 @@
|
|||||||
|
#[macro_use]
|
||||||
|
extern crate log;
|
||||||
|
extern crate xml;
|
||||||
|
extern crate yaserde;
|
||||||
|
#[macro_use]
|
||||||
|
extern crate yaserde_derive;
|
||||||
|
|
||||||
|
use std::io::Write;
|
||||||
|
use yaserde::YaSerialize;
|
||||||
|
use yaserde::ser::to_string;
|
||||||
|
|
||||||
|
macro_rules! convert_and_validate {
|
||||||
|
($type: ty, $value: expr, $content: expr) => {{
|
||||||
|
#[derive(YaSerialize, PartialEq, Debug)]
|
||||||
|
#[yaserde(root = "data")]
|
||||||
|
pub struct Data {
|
||||||
|
item: Option<$type>,
|
||||||
|
}
|
||||||
|
let model = Data { item: $value };
|
||||||
|
|
||||||
|
let data: Result<String, String> = to_string(&model);
|
||||||
|
|
||||||
|
let content = if let Some(str_value) = $content {
|
||||||
|
"<data><item>".to_string() + str_value + "</item></data>"
|
||||||
|
} else {
|
||||||
|
"<data />".to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
let content = String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?>") + &content;
|
||||||
|
assert_eq!(data, Ok(content));
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
macro_rules! convert_and_validate_as_attribute {
|
||||||
|
($type: ty, $value: expr, $content: expr) => {{
|
||||||
|
#[derive(YaSerialize, PartialEq, Debug)]
|
||||||
|
#[yaserde(root = "data")]
|
||||||
|
pub struct Data {
|
||||||
|
#[yaserde(attribute)]
|
||||||
|
item: Option<$type>,
|
||||||
|
}
|
||||||
|
let model = Data { item: $value };
|
||||||
|
|
||||||
|
let data: Result<String, String> = to_string(&model);
|
||||||
|
|
||||||
|
let content = if let Some(str_value) = $content {
|
||||||
|
"<data item=\"".to_string() + str_value + "\" />"
|
||||||
|
} else {
|
||||||
|
"<data />".to_string()
|
||||||
|
};
|
||||||
|
|
||||||
|
let content = String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?>") + &content;
|
||||||
|
assert_eq!(data, Ok(content));
|
||||||
|
}};
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn ser_option() {
|
||||||
|
convert_and_validate!(String, Some("test".to_string()), Some("test"));
|
||||||
|
convert_and_validate!(String, None, None);
|
||||||
|
convert_and_validate!(bool, Some(true), Some("true"));
|
||||||
|
convert_and_validate!(bool, None, None);
|
||||||
|
convert_and_validate!(u8, Some(12 as u8), Some("12"));
|
||||||
|
convert_and_validate!(u8, None, None);
|
||||||
|
convert_and_validate!(i8, Some(12 as i8), Some("12"));
|
||||||
|
convert_and_validate!(i8, Some(-12 as i8), Some("-12"));
|
||||||
|
convert_and_validate!(i8, None, None);
|
||||||
|
convert_and_validate!(u16, Some(12 as u16), Some("12"));
|
||||||
|
convert_and_validate!(u16, None, None);
|
||||||
|
convert_and_validate!(i16, Some(12 as i16), Some("12"));
|
||||||
|
convert_and_validate!(i16, Some(-12 as i16), Some("-12"));
|
||||||
|
convert_and_validate!(i16, None, None);
|
||||||
|
convert_and_validate!(u32, Some(12 as u32), Some("12"));
|
||||||
|
convert_and_validate!(u32, None, None);
|
||||||
|
convert_and_validate!(i32, Some(12 as i32), Some("12"));
|
||||||
|
convert_and_validate!(i32, Some(-12 as i32), Some("-12"));
|
||||||
|
convert_and_validate!(i32, None, None);
|
||||||
|
convert_and_validate!(u64, Some(12 as u64), Some("12"));
|
||||||
|
convert_and_validate!(u64, None, None);
|
||||||
|
convert_and_validate!(i64, Some(12 as i64), Some("12"));
|
||||||
|
convert_and_validate!(i64, Some(-12 as i64), Some("-12"));
|
||||||
|
convert_and_validate!(i64, None, None);
|
||||||
|
convert_and_validate!(f32, Some(-12.5 as f32), Some("-12.5"));
|
||||||
|
convert_and_validate!(f32, None, None);
|
||||||
|
convert_and_validate!(f64, Some(-12.5 as f64), Some("-12.5"));
|
||||||
|
convert_and_validate!(f64, None, None);
|
||||||
|
|
||||||
|
convert_and_validate_as_attribute!(String, Some("test".to_string()), Some("test"));
|
||||||
|
convert_and_validate_as_attribute!(String, None, None);
|
||||||
|
convert_and_validate_as_attribute!(bool, Some(true), Some("true"));
|
||||||
|
convert_and_validate_as_attribute!(bool, None, None);
|
||||||
|
convert_and_validate_as_attribute!(u8, Some(12 as u8), Some("12"));
|
||||||
|
convert_and_validate_as_attribute!(u8, None, None);
|
||||||
|
convert_and_validate_as_attribute!(i8, Some(12 as i8), Some("12"));
|
||||||
|
convert_and_validate_as_attribute!(i8, Some(-12 as i8), Some("-12"));
|
||||||
|
convert_and_validate_as_attribute!(i8, None, None);
|
||||||
|
convert_and_validate_as_attribute!(u16, Some(12 as u16), Some("12"));
|
||||||
|
convert_and_validate_as_attribute!(u16, None, None);
|
||||||
|
convert_and_validate_as_attribute!(i16, Some(12 as i16), Some("12"));
|
||||||
|
convert_and_validate_as_attribute!(i16, Some(-12 as i16), Some("-12"));
|
||||||
|
convert_and_validate_as_attribute!(i16, None, None);
|
||||||
|
convert_and_validate_as_attribute!(u32, Some(12 as u32), Some("12"));
|
||||||
|
convert_and_validate_as_attribute!(u32, None, None);
|
||||||
|
convert_and_validate_as_attribute!(i32, Some(12 as i32), Some("12"));
|
||||||
|
convert_and_validate_as_attribute!(i32, Some(-12 as i32), Some("-12"));
|
||||||
|
convert_and_validate_as_attribute!(i32, None, None);
|
||||||
|
convert_and_validate_as_attribute!(u64, Some(12 as u64), Some("12"));
|
||||||
|
convert_and_validate_as_attribute!(u64, None, None);
|
||||||
|
convert_and_validate_as_attribute!(i64, Some(12 as i64), Some("12"));
|
||||||
|
convert_and_validate_as_attribute!(i64, Some(-12 as i64), Some("-12"));
|
||||||
|
convert_and_validate_as_attribute!(i64, None, None);
|
||||||
|
convert_and_validate_as_attribute!(f32, Some(-12.5 as f32), Some("-12.5"));
|
||||||
|
convert_and_validate_as_attribute!(f32, None, None);
|
||||||
|
convert_and_validate_as_attribute!(f64, Some(-12.5 as f64), Some("-12.5"));
|
||||||
|
convert_and_validate_as_attribute!(f64, None, None);
|
||||||
|
}
|
||||||
@ -44,6 +44,7 @@ macro_rules! convert_and_validate_as_attribute {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn ser_type() {
|
fn ser_type() {
|
||||||
|
convert_and_validate!(String, "test".to_string(), "test");
|
||||||
convert_and_validate!(bool, true, "true");
|
convert_and_validate!(bool, true, "true");
|
||||||
convert_and_validate!(u8, 12 as u8, "12");
|
convert_and_validate!(u8, 12 as u8, "12");
|
||||||
convert_and_validate!(i8, 12 as i8, "12");
|
convert_and_validate!(i8, 12 as i8, "12");
|
||||||
@ -60,6 +61,7 @@ fn ser_type() {
|
|||||||
convert_and_validate!(f32, -12.5 as f32, "-12.5");
|
convert_and_validate!(f32, -12.5 as f32, "-12.5");
|
||||||
convert_and_validate!(f64, -12.5 as f64, "-12.5");
|
convert_and_validate!(f64, -12.5 as f64, "-12.5");
|
||||||
|
|
||||||
|
convert_and_validate_as_attribute!(String, "test".to_string(), "test");
|
||||||
convert_and_validate_as_attribute!(bool, true, "true");
|
convert_and_validate_as_attribute!(bool, true, "true");
|
||||||
convert_and_validate_as_attribute!(u8, 12 as u8, "12");
|
convert_and_validate_as_attribute!(u8, 12 as u8, "12");
|
||||||
convert_and_validate_as_attribute!(i8, 12 as i8, "12");
|
convert_and_validate_as_attribute!(i8, 12 as i8, "12");
|
||||||
|
|||||||
@ -66,6 +66,10 @@ pub fn parse(
|
|||||||
#[allow(unused_mut)]
|
#[allow(unused_mut)]
|
||||||
let mut #field_label : #struct_name = #struct_name::default();
|
let mut #field_label : #struct_name = #struct_name::default();
|
||||||
}),
|
}),
|
||||||
|
Some(FieldType::FieldTypeOption { .. }) => Some(quote!{
|
||||||
|
#[allow(unused_mut)]
|
||||||
|
let mut #field_label = None;
|
||||||
|
}),
|
||||||
Some(FieldType::FieldTypeVec { data_type }) => {
|
Some(FieldType::FieldTypeVec { data_type }) => {
|
||||||
let dt = Box::into_raw(data_type);
|
let dt = Box::into_raw(data_type);
|
||||||
match unsafe { dt.as_ref() } {
|
match unsafe { dt.as_ref() } {
|
||||||
@ -109,7 +113,8 @@ pub fn parse(
|
|||||||
#[allow(unused_mut)]
|
#[allow(unused_mut)]
|
||||||
let mut #field_label : Vec<#struct_name> = vec![];
|
let mut #field_label : Vec<#struct_name> = vec![];
|
||||||
}),
|
}),
|
||||||
Some(&FieldType::FieldTypeVec { .. }) => {
|
Some(&FieldType::FieldTypeOption { .. })
|
||||||
|
| Some(&FieldType::FieldTypeVec { .. }) => {
|
||||||
unimplemented!();
|
unimplemented!();
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
|
|||||||
@ -62,6 +62,10 @@ pub fn parse(
|
|||||||
#[allow(unused_mut, non_snake_case, non_camel_case_types)]
|
#[allow(unused_mut, non_snake_case, non_camel_case_types)]
|
||||||
let mut #label : #struct_name = #struct_name::default();
|
let mut #label : #struct_name = #struct_name::default();
|
||||||
}),
|
}),
|
||||||
|
Some(FieldType::FieldTypeOption { .. }) => Some(quote!{
|
||||||
|
#[allow(unused_mut, non_snake_case, non_camel_case_types)]
|
||||||
|
let mut #label = None;
|
||||||
|
}),
|
||||||
Some(FieldType::FieldTypeVec { data_type }) => {
|
Some(FieldType::FieldTypeVec { data_type }) => {
|
||||||
let dt = Box::into_raw(data_type);
|
let dt = Box::into_raw(data_type);
|
||||||
match unsafe { dt.as_ref() } {
|
match unsafe { dt.as_ref() } {
|
||||||
@ -105,7 +109,7 @@ pub fn parse(
|
|||||||
#[allow(unused_mut)]
|
#[allow(unused_mut)]
|
||||||
let mut #label : Vec<#struct_name> = vec![];
|
let mut #label : Vec<#struct_name> = vec![];
|
||||||
}),
|
}),
|
||||||
Some(&FieldType::FieldTypeVec { .. }) => {
|
Some(&FieldType::FieldTypeOption { .. }) | Some(&FieldType::FieldTypeVec { .. }) => {
|
||||||
unimplemented!();
|
unimplemented!();
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
@ -194,6 +198,50 @@ pub fn parse(
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Some(FieldType::FieldTypeOption { data_type }) => {
|
||||||
|
let dt = Box::into_raw(data_type);
|
||||||
|
match unsafe { dt.as_ref() } {
|
||||||
|
Some(&FieldType::FieldTypeString) => {
|
||||||
|
build_declare_visitor("e!{String}, "e!{visit_str}, &visitor_label)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeBool) => {
|
||||||
|
build_declare_visitor("e!{bool}, "e!{visit_bool}, &visitor_label)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeI8) => {
|
||||||
|
build_declare_visitor("e!{i8}, "e!{visit_i8}, &visitor_label)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeU8) => {
|
||||||
|
build_declare_visitor("e!{u8}, "e!{visit_u8}, &visitor_label)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeI16) => {
|
||||||
|
build_declare_visitor("e!{i16}, "e!{visit_i16}, &visitor_label)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeU16) => {
|
||||||
|
build_declare_visitor("e!{u16}, "e!{visit_u16}, &visitor_label)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeI32) => {
|
||||||
|
build_declare_visitor("e!{i32}, "e!{visit_i32}, &visitor_label)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeU32) => {
|
||||||
|
build_declare_visitor("e!{u32}, "e!{visit_u32}, &visitor_label)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeI64) => {
|
||||||
|
build_declare_visitor("e!{i64}, "e!{visit_i64}, &visitor_label)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeU64) => {
|
||||||
|
build_declare_visitor("e!{u64}, "e!{visit_u64}, &visitor_label)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeF32) => {
|
||||||
|
build_declare_visitor("e!{f32}, "e!{visit_f32}, &visitor_label)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeF64) => {
|
||||||
|
build_declare_visitor("e!{f64}, "e!{visit_f64}, &visitor_label)
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
unimplemented!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Some(FieldType::FieldTypeVec { data_type }) => {
|
Some(FieldType::FieldTypeVec { data_type }) => {
|
||||||
let dt = Box::into_raw(data_type);
|
let dt = Box::into_raw(data_type);
|
||||||
match unsafe { dt.as_ref() } {
|
match unsafe { dt.as_ref() } {
|
||||||
@ -422,6 +470,144 @@ pub fn parse(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
Some(FieldType::FieldTypeOption { data_type }) => {
|
||||||
|
let dt = Box::into_raw(data_type);
|
||||||
|
match unsafe { dt.as_ref() } {
|
||||||
|
Some(&FieldType::FieldTypeString) => {
|
||||||
|
let visitor = Ident::new("visit_str", Span::call_site());
|
||||||
|
build_call_visitor(
|
||||||
|
"e!{String},
|
||||||
|
&visitor,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
&visitor_label,
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeBool) => {
|
||||||
|
let visitor = Ident::new("visit_bool", Span::call_site());
|
||||||
|
build_call_visitor(
|
||||||
|
"e!{bool},
|
||||||
|
&visitor,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
&visitor_label,
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeU8) => {
|
||||||
|
let visitor = Ident::new("visit_u8", Span::call_site());
|
||||||
|
build_call_visitor(
|
||||||
|
"e!{u8},
|
||||||
|
&visitor,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
&visitor_label,
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeI8) => {
|
||||||
|
let visitor = Ident::new("visit_i8", Span::call_site());
|
||||||
|
build_call_visitor(
|
||||||
|
"e!{i8},
|
||||||
|
&visitor,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
&visitor_label,
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeU16) => {
|
||||||
|
let visitor = Ident::new("visit_u16", Span::call_site());
|
||||||
|
build_call_visitor(
|
||||||
|
"e!{u16},
|
||||||
|
&visitor,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
&visitor_label,
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeI16) => {
|
||||||
|
let visitor = Ident::new("visit_i16", Span::call_site());
|
||||||
|
build_call_visitor(
|
||||||
|
"e!{i16},
|
||||||
|
&visitor,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
&visitor_label,
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeU32) => {
|
||||||
|
let visitor = Ident::new("visit_u32", Span::call_site());
|
||||||
|
build_call_visitor(
|
||||||
|
"e!{u32},
|
||||||
|
&visitor,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
&visitor_label,
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeI32) => {
|
||||||
|
let visitor = Ident::new("visit_i32", Span::call_site());
|
||||||
|
build_call_visitor(
|
||||||
|
"e!{i32},
|
||||||
|
&visitor,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
&visitor_label,
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeU64) => {
|
||||||
|
let visitor = Ident::new("visit_u64", Span::call_site());
|
||||||
|
build_call_visitor(
|
||||||
|
"e!{u64},
|
||||||
|
&visitor,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
&visitor_label,
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeI64) => {
|
||||||
|
let visitor = Ident::new("visit_i64", Span::call_site());
|
||||||
|
build_call_visitor(
|
||||||
|
"e!{i64},
|
||||||
|
&visitor,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
&visitor_label,
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeF32) => {
|
||||||
|
let visitor = Ident::new("visit_f32", Span::call_site());
|
||||||
|
build_call_visitor(
|
||||||
|
"e!{f32},
|
||||||
|
&visitor,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
&visitor_label,
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
Some(&FieldType::FieldTypeF64) => {
|
||||||
|
let visitor = Ident::new("visit_f64", Span::call_site());
|
||||||
|
build_call_visitor(
|
||||||
|
"e!{f64},
|
||||||
|
&visitor,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
&visitor_label,
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
Some(FieldType::FieldTypeVec { data_type }) => {
|
Some(FieldType::FieldTypeVec { data_type }) => {
|
||||||
let dt = Box::into_raw(data_type);
|
let dt = Box::into_raw(data_type);
|
||||||
match unsafe { dt.as_ref() } {
|
match unsafe { dt.as_ref() } {
|
||||||
@ -613,40 +799,173 @@ pub fn parse(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
Some(FieldType::FieldTypeBool) => {
|
Some(FieldType::FieldTypeBool) => build_call_visitor_for_attribute(
|
||||||
build_call_visitor_for_attribute(label, &label_name, "e!{visit_bool}, &visitor_label)
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= value},
|
||||||
|
"e!{visit_bool},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(FieldType::FieldTypeI8) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= value},
|
||||||
|
"e!{visit_i8},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(FieldType::FieldTypeU8) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= value},
|
||||||
|
"e!{visit_u8},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(FieldType::FieldTypeI16) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= value},
|
||||||
|
"e!{visit_i16},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(FieldType::FieldTypeU16) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= value},
|
||||||
|
"e!{visit_u16},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(FieldType::FieldTypeI32) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= value},
|
||||||
|
"e!{visit_i32},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(FieldType::FieldTypeU32) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= value},
|
||||||
|
"e!{visit_u32},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(FieldType::FieldTypeI64) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= value},
|
||||||
|
"e!{visit_i64},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(FieldType::FieldTypeU64) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= value},
|
||||||
|
"e!{visit_u64},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(FieldType::FieldTypeF32) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= value},
|
||||||
|
"e!{visit_f32},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(FieldType::FieldTypeF64) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= value},
|
||||||
|
"e!{visit_f64},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(FieldType::FieldTypeOption { data_type }) => {
|
||||||
|
let dt = Box::into_raw(data_type);
|
||||||
|
match unsafe { dt.as_ref() } {
|
||||||
|
Some(&FieldType::FieldTypeString) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
"e!{visit_str},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(&FieldType::FieldTypeBool) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
"e!{visit_bool},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(&FieldType::FieldTypeU8) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
"e!{visit_u8},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(&FieldType::FieldTypeI8) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
"e!{visit_i8},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(&FieldType::FieldTypeU16) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
"e!{visit_u16},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(&FieldType::FieldTypeI16) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
"e!{visit_i16},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(&FieldType::FieldTypeU32) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
"e!{visit_u32},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(&FieldType::FieldTypeI32) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
"e!{visit_i32},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(&FieldType::FieldTypeU64) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
"e!{visit_u64},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(&FieldType::FieldTypeI64) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
"e!{visit_i64},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(&FieldType::FieldTypeF32) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
"e!{visit_f32},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
Some(&FieldType::FieldTypeF64) => build_call_visitor_for_attribute(
|
||||||
|
label,
|
||||||
|
&label_name,
|
||||||
|
"e!{= Some(value)},
|
||||||
|
"e!{visit_f64},
|
||||||
|
&visitor_label,
|
||||||
|
),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Some(FieldType::FieldTypeI8) => {
|
|
||||||
build_call_visitor_for_attribute(label, &label_name, "e!{visit_i8}, &visitor_label)
|
|
||||||
}
|
|
||||||
Some(FieldType::FieldTypeU8) => {
|
|
||||||
build_call_visitor_for_attribute(label, &label_name, "e!{visit_u8}, &visitor_label)
|
|
||||||
}
|
|
||||||
Some(FieldType::FieldTypeI16) => {
|
|
||||||
build_call_visitor_for_attribute(label, &label_name, "e!{visit_i16}, &visitor_label)
|
|
||||||
}
|
|
||||||
Some(FieldType::FieldTypeU16) => {
|
|
||||||
build_call_visitor_for_attribute(label, &label_name, "e!{visit_u16}, &visitor_label)
|
|
||||||
}
|
|
||||||
Some(FieldType::FieldTypeI32) => {
|
|
||||||
build_call_visitor_for_attribute(label, &label_name, "e!{visit_i32}, &visitor_label)
|
|
||||||
}
|
|
||||||
Some(FieldType::FieldTypeU32) => {
|
|
||||||
build_call_visitor_for_attribute(label, &label_name, "e!{visit_u32}, &visitor_label)
|
|
||||||
}
|
|
||||||
Some(FieldType::FieldTypeI64) => {
|
|
||||||
build_call_visitor_for_attribute(label, &label_name, "e!{visit_i64}, &visitor_label)
|
|
||||||
}
|
|
||||||
Some(FieldType::FieldTypeU64) => {
|
|
||||||
build_call_visitor_for_attribute(label, &label_name, "e!{visit_u64}, &visitor_label)
|
|
||||||
}
|
|
||||||
Some(FieldType::FieldTypeF32) => {
|
|
||||||
build_call_visitor_for_attribute(label, &label_name, "e!{visit_f32}, &visitor_label)
|
|
||||||
}
|
|
||||||
Some(FieldType::FieldTypeF64) => {
|
|
||||||
build_call_visitor_for_attribute(label, &label_name, "e!{visit_f64}, &visitor_label)
|
|
||||||
}
|
|
||||||
|
|
||||||
Some(FieldType::FieldTypeStruct { struct_name }) => {
|
Some(FieldType::FieldTypeStruct { struct_name }) => {
|
||||||
let struct_ident = Ident::new(
|
let struct_ident = Ident::new(
|
||||||
&format!("__Visitor_{}_{}", label_name, struct_name),
|
&format!("__Visitor_{}_{}", label_name, struct_name),
|
||||||
@ -742,9 +1061,10 @@ pub fn parse(
|
|||||||
"e!{f64::from_str(text_content).unwrap()},
|
"e!{f64::from_str(text_content).unwrap()},
|
||||||
),
|
),
|
||||||
|
|
||||||
Some(FieldType::FieldTypeStruct { .. }) | Some(FieldType::FieldTypeVec { .. }) | None => {
|
Some(FieldType::FieldTypeStruct { .. })
|
||||||
None
|
| Some(FieldType::FieldTypeOption { .. })
|
||||||
}
|
| Some(FieldType::FieldTypeVec { .. })
|
||||||
|
| None => None,
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.filter(|x| x.is_some())
|
.filter(|x| x.is_some())
|
||||||
@ -900,6 +1220,7 @@ fn build_call_visitor(
|
|||||||
fn build_call_visitor_for_attribute(
|
fn build_call_visitor_for_attribute(
|
||||||
label: &Option<Ident>,
|
label: &Option<Ident>,
|
||||||
label_name: &str,
|
label_name: &str,
|
||||||
|
action: &TokenStream,
|
||||||
visitor: &TokenStream,
|
visitor: &TokenStream,
|
||||||
visitor_label: &Ident,
|
visitor_label: &Ident,
|
||||||
) -> Option<TokenStream> {
|
) -> Option<TokenStream> {
|
||||||
@ -908,7 +1229,7 @@ fn build_call_visitor_for_attribute(
|
|||||||
if attr.name.local_name == #label_name {
|
if attr.name.local_name == #label_name {
|
||||||
let visitor = #visitor_label{};
|
let visitor = #visitor_label{};
|
||||||
match visitor.#visitor(&attr.value) {
|
match visitor.#visitor(&attr.value) {
|
||||||
Ok(value) => {#label = value;}
|
Ok(value) => {#label #action;}
|
||||||
Err(msg) => {return Err(msg);}
|
Err(msg) => {return Err(msg);}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -16,6 +16,7 @@ pub enum FieldType {
|
|||||||
FieldTypeU64,
|
FieldTypeU64,
|
||||||
FieldTypeF32,
|
FieldTypeF32,
|
||||||
FieldTypeF64,
|
FieldTypeF64,
|
||||||
|
FieldTypeOption { data_type: Box<FieldType> },
|
||||||
FieldTypeVec { data_type: Box<FieldType> },
|
FieldTypeVec { data_type: Box<FieldType> },
|
||||||
FieldTypeStruct { struct_name: syn::Ident },
|
FieldTypeStruct { struct_name: syn::Ident },
|
||||||
}
|
}
|
||||||
@ -35,7 +36,17 @@ impl FieldType {
|
|||||||
"u64" => Some(FieldType::FieldTypeU64),
|
"u64" => Some(FieldType::FieldTypeU64),
|
||||||
"f32" => Some(FieldType::FieldTypeF32),
|
"f32" => Some(FieldType::FieldTypeF32),
|
||||||
"f64" => Some(FieldType::FieldTypeF64),
|
"f64" => Some(FieldType::FieldTypeF64),
|
||||||
"Vec" => get_vec_type(t).map(|data_type| {
|
"Option" => get_sub_type(t).map(|data_type| {
|
||||||
|
let p = syn::PathSegment {
|
||||||
|
ident: data_type,
|
||||||
|
arguments: syn::PathArguments::None,
|
||||||
|
};
|
||||||
|
|
||||||
|
FieldType::FieldTypeOption {
|
||||||
|
data_type: Box::new(FieldType::from_ident(&p).unwrap()),
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
"Vec" => get_sub_type(t).map(|data_type| {
|
||||||
let p = syn::PathSegment {
|
let p = syn::PathSegment {
|
||||||
ident: data_type,
|
ident: data_type,
|
||||||
arguments: syn::PathArguments::None,
|
arguments: syn::PathArguments::None,
|
||||||
@ -62,7 +73,7 @@ pub fn get_field_type(field: &syn::Field) -> Option<FieldType> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_vec_type(t: &syn::PathSegment) -> Option<syn::Ident> {
|
fn get_sub_type(t: &syn::PathSegment) -> Option<syn::Ident> {
|
||||||
if let syn::PathArguments::AngleBracketed(ref args) = t.arguments {
|
if let syn::PathArguments::AngleBracketed(ref args) = t.arguments {
|
||||||
if let Some(Pair::End(tt)) = args.args.first() {
|
if let Some(Pair::End(tt)) = args.args.first() {
|
||||||
if let syn::GenericArgument::Type(ref argument) = *tt {
|
if let syn::GenericArgument::Type(ref argument) = *tt {
|
||||||
|
|||||||
@ -46,7 +46,7 @@ pub fn serialize(
|
|||||||
| Some(FieldType::FieldTypeU64)
|
| Some(FieldType::FieldTypeU64)
|
||||||
| Some(FieldType::FieldTypeF32)
|
| Some(FieldType::FieldTypeF32)
|
||||||
| Some(FieldType::FieldTypeF64) => Some(quote!{
|
| Some(FieldType::FieldTypeF64) => Some(quote!{
|
||||||
.attr(#label_name, &*{
|
let struct_start_event = struct_start_event.attr(#label_name, &*{
|
||||||
use std::mem;
|
use std::mem;
|
||||||
unsafe {
|
unsafe {
|
||||||
let content = format!("{}", self.#label);
|
let content = format!("{}", self.#label);
|
||||||
@ -54,11 +54,50 @@ pub fn serialize(
|
|||||||
mem::forget(content);
|
mem::forget(content);
|
||||||
ret
|
ret
|
||||||
}
|
}
|
||||||
|
});
|
||||||
})
|
|
||||||
}),
|
}),
|
||||||
|
Some(FieldType::FieldTypeOption { data_type }) => {
|
||||||
|
let dt = Box::into_raw(data_type);
|
||||||
|
match unsafe { dt.as_ref() } {
|
||||||
|
Some(&FieldType::FieldTypeString) => Some(quote!{
|
||||||
|
let struct_start_event =
|
||||||
|
if let Some(ref value) = self.#label {
|
||||||
|
struct_start_event.attr(#label_name, &value)
|
||||||
|
} else {
|
||||||
|
struct_start_event
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
Some(&FieldType::FieldTypeBool)
|
||||||
|
| Some(&FieldType::FieldTypeI8)
|
||||||
|
| Some(&FieldType::FieldTypeU8)
|
||||||
|
| Some(&FieldType::FieldTypeI16)
|
||||||
|
| Some(&FieldType::FieldTypeU16)
|
||||||
|
| Some(&FieldType::FieldTypeI32)
|
||||||
|
| Some(&FieldType::FieldTypeU32)
|
||||||
|
| Some(&FieldType::FieldTypeI64)
|
||||||
|
| Some(&FieldType::FieldTypeU64)
|
||||||
|
| Some(&FieldType::FieldTypeF32)
|
||||||
|
| Some(&FieldType::FieldTypeF64) => Some(quote!{
|
||||||
|
let struct_start_event =
|
||||||
|
if let Some(value) = self.#label {
|
||||||
|
struct_start_event.attr(#label_name, &*{
|
||||||
|
use std::mem;
|
||||||
|
unsafe {
|
||||||
|
let content = format!("{}", value);
|
||||||
|
let ret : &'static str = mem::transmute(&content as &str);
|
||||||
|
mem::forget(content);
|
||||||
|
ret
|
||||||
|
}
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
struct_start_event
|
||||||
|
};
|
||||||
|
}),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
Some(FieldType::FieldTypeStruct { .. }) => Some(quote!{
|
Some(FieldType::FieldTypeStruct { .. }) => Some(quote!{
|
||||||
.attr(#label_name, &*{
|
let struct_start_event = struct_start_event.attr(#label_name, &*{
|
||||||
use std::mem;
|
use std::mem;
|
||||||
match yaserde::ser::to_string_content(&self.#label) {
|
match yaserde::ser::to_string_content(&self.#label) {
|
||||||
Ok(value) => {
|
Ok(value) => {
|
||||||
@ -70,7 +109,7 @@ pub fn serialize(
|
|||||||
},
|
},
|
||||||
Err(msg) => return Err("Unable to serialize content".to_owned()),
|
Err(msg) => return Err("Unable to serialize content".to_owned()),
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
}),
|
}),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
@ -156,6 +195,47 @@ pub fn serialize(
|
|||||||
let end_event = XmlEvent::end_element();
|
let end_event = XmlEvent::end_element();
|
||||||
let _ret = writer.write(end_event);
|
let _ret = writer.write(end_event);
|
||||||
}),
|
}),
|
||||||
|
Some(FieldType::FieldTypeOption { data_type }) => {
|
||||||
|
let dt = Box::into_raw(data_type);
|
||||||
|
match unsafe { dt.as_ref() } {
|
||||||
|
Some(&FieldType::FieldTypeString) => Some(quote!{
|
||||||
|
if let Some(ref item) = self.#label {
|
||||||
|
let start_event = XmlEvent::start_element(#label_name);
|
||||||
|
let _ret = writer.write(start_event);
|
||||||
|
|
||||||
|
let data_event = XmlEvent::characters(&item);
|
||||||
|
let _ret = writer.write(data_event);
|
||||||
|
|
||||||
|
let end_event = XmlEvent::end_element();
|
||||||
|
let _ret = writer.write(end_event);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
Some(&FieldType::FieldTypeBool)
|
||||||
|
| Some(&FieldType::FieldTypeI8)
|
||||||
|
| Some(&FieldType::FieldTypeU8)
|
||||||
|
| Some(&FieldType::FieldTypeI16)
|
||||||
|
| Some(&FieldType::FieldTypeU16)
|
||||||
|
| Some(&FieldType::FieldTypeI32)
|
||||||
|
| Some(&FieldType::FieldTypeU32)
|
||||||
|
| Some(&FieldType::FieldTypeI64)
|
||||||
|
| Some(&FieldType::FieldTypeU64)
|
||||||
|
| Some(&FieldType::FieldTypeF32)
|
||||||
|
| Some(&FieldType::FieldTypeF64) => Some(quote!{
|
||||||
|
if let Some(item) = self.#label {
|
||||||
|
let start_event = XmlEvent::start_element(#label_name);
|
||||||
|
let _ret = writer.write(start_event);
|
||||||
|
|
||||||
|
let content = format!("{}", item);
|
||||||
|
let data_event = XmlEvent::characters(&content);
|
||||||
|
let _ret = writer.write(data_event);
|
||||||
|
|
||||||
|
let end_event = XmlEvent::end_element();
|
||||||
|
let _ret = writer.write(end_event);
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
Some(FieldType::FieldTypeStruct { .. }) => Some(quote!{
|
Some(FieldType::FieldTypeStruct { .. }) => Some(quote!{
|
||||||
writer.set_skip_start_end(false);
|
writer.set_skip_start_end(false);
|
||||||
match self.#label.serialize(writer) {
|
match self.#label.serialize(writer) {
|
||||||
@ -202,6 +282,19 @@ pub fn serialize(
|
|||||||
let _ret = writer.write(end_event);
|
let _ret = writer.write(end_event);
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
|
Some(&FieldType::FieldTypeOption { .. }) => Some(quote!{
|
||||||
|
for item in &self.#label {
|
||||||
|
if let Some(value) = item {
|
||||||
|
writer.set_skip_start_end(false);
|
||||||
|
match value.serialize(writer) {
|
||||||
|
Ok(()) => {},
|
||||||
|
Err(msg) => {
|
||||||
|
return Err(msg);
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}),
|
||||||
Some(&FieldType::FieldTypeStruct { .. }) => Some(quote!{
|
Some(&FieldType::FieldTypeStruct { .. }) => Some(quote!{
|
||||||
for item in &self.#label {
|
for item in &self.#label {
|
||||||
writer.set_skip_start_end(false);
|
writer.set_skip_start_end(false);
|
||||||
@ -241,7 +334,8 @@ pub fn serialize(
|
|||||||
error!("Struct: start to expand {:?}", #root);
|
error!("Struct: start to expand {:?}", #root);
|
||||||
let skip = writer.skip_start_end();
|
let skip = writer.skip_start_end();
|
||||||
if !skip {
|
if !skip {
|
||||||
let struct_start_event = XmlEvent::start_element(#root)#build_attributes#add_namespaces;
|
let struct_start_event = XmlEvent::start_element(#root)#add_namespaces;
|
||||||
|
#build_attributes
|
||||||
let _ret = writer.write(struct_start_event);
|
let _ret = writer.write(struct_start_event);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user