Add support for Option<struct> in 'attribute' fields

This commit is contained in:
Dmitry Samoylov
2020-02-12 15:45:30 +07:00
parent 264fa06b8b
commit cad7f88c4e
5 changed files with 226 additions and 101 deletions

View File

@@ -172,6 +172,53 @@ fn de_attributes() {
);
}
#[test]
fn de_attributes_complex() {
mod other_mod {
use super::*;
#[derive(YaDeserialize, PartialEq, Debug)]
pub enum AttrEnum {
#[yaserde(rename = "variant 1")]
Variant1,
#[yaserde(rename = "variant 2")]
Variant2,
}
impl Default for AttrEnum {
fn default() -> AttrEnum {
AttrEnum::Variant1
}
}
}
#[derive(Default, YaDeserialize, PartialEq, Debug)]
pub struct Struct {
#[yaserde(attribute)]
attr_option_string: Option<std::string::String>,
#[yaserde(attribute)]
attr_option_enum: Option<other_mod::AttrEnum>,
}
convert_and_validate!(
r#"<Struct />"#,
Struct,
Struct {
attr_option_string: None,
attr_option_enum: None
}
);
convert_and_validate!(
r#"<Struct attr_option_string="some value" attr_option_enum="variant 2" />"#,
Struct,
Struct {
attr_option_string: Some("some value".to_string()),
attr_option_enum: Some(other_mod::AttrEnum::Variant2)
}
);
}
#[test]
fn de_rename() {
#[derive(YaDeserialize, PartialEq, Debug)]

View File

@@ -124,6 +124,66 @@ fn se_attributes() {
convert_and_validate!(model, content);
}
#[test]
fn se_attributes_complex() {
mod other_mod {
use super::*;
#[derive(YaSerialize, PartialEq, Debug)]
pub enum AttrEnum {
#[yaserde(rename = "variant 1")]
Variant1,
#[yaserde(rename = "variant 2")]
Variant2,
}
impl Default for AttrEnum {
fn default() -> AttrEnum {
AttrEnum::Variant1
}
}
}
#[derive(YaSerialize, PartialEq, Debug)]
pub struct Struct {
#[yaserde(attribute)]
attr_option_string: Option<std::string::String>,
#[yaserde(attribute)]
attr_option_enum: Option<other_mod::AttrEnum>,
}
impl Default for Struct {
fn default() -> Struct {
Struct {
attr_option_string: None,
attr_option_enum: None,
}
}
}
convert_and_validate!(
Struct {
attr_option_string: None,
attr_option_enum: None,
},
r#"
<?xml version="1.0" encoding="utf-8"?>
<Struct />
"#
);
convert_and_validate!(
Struct {
attr_option_string: Some("some value".to_string()),
attr_option_enum: Some(other_mod::AttrEnum::Variant2),
},
r#"
<?xml version="1.0" encoding="utf-8"?>
<Struct attr_option_string="some value" attr_option_enum="variant 2" />
"#
);
}
#[test]
fn ser_rename() {
#[derive(YaSerialize, PartialEq, Debug)]