be able to use Option of struct and String
This commit is contained in:
@@ -42,6 +42,7 @@ pub fn serialize_with_writer_content<W: Write, T: YaSerialize>(
|
||||
pub struct Serializer<W: Write> {
|
||||
writer: EventWriter<W>,
|
||||
skip_start_end: bool,
|
||||
start_event_name: Option<String>,
|
||||
}
|
||||
|
||||
impl<'de, W: Write> Serializer<W> {
|
||||
@@ -49,6 +50,7 @@ impl<'de, W: Write> Serializer<W> {
|
||||
Serializer {
|
||||
writer,
|
||||
skip_start_end: false,
|
||||
start_event_name: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,6 +78,14 @@ impl<'de, W: Write> Serializer<W> {
|
||||
self.skip_start_end = state;
|
||||
}
|
||||
|
||||
pub fn get_start_event_name<'a>(&self) -> Option<String> {
|
||||
self.start_event_name.clone()
|
||||
}
|
||||
|
||||
pub fn set_start_event_name<'a>(&mut self, name: Option<String>) {
|
||||
self.start_event_name = name;
|
||||
}
|
||||
|
||||
pub fn write<'a, E>(&mut self, event: E) -> xml::writer::Result<()>
|
||||
where
|
||||
E: Into<XmlEvent<'a>>,
|
||||
|
||||
@@ -116,3 +116,27 @@ fn de_option() {
|
||||
convert_and_validate_for_attribute!(f64, Some(-12.5 as f64), Some("-12.5"));
|
||||
convert_and_validate_for_attribute!(f64, None, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn de_option_struct() {
|
||||
#[derive(YaDeserialize, Debug, PartialEq)]
|
||||
struct Test {
|
||||
field: SubTest
|
||||
}
|
||||
|
||||
#[derive(YaDeserialize, Debug, PartialEq)]
|
||||
struct SubTest {
|
||||
content: Option<String>
|
||||
}
|
||||
|
||||
impl Default for SubTest {
|
||||
fn default() -> Self {
|
||||
SubTest {
|
||||
content: None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
convert_and_validate!(Test, Some(Test{field: SubTest{content: Some("value".to_string())}}), Some("<Test><field><content>value</content></field></Test>"));
|
||||
convert_and_validate!(Test, None, None);
|
||||
}
|
||||
|
||||
@@ -119,3 +119,27 @@ fn ser_option() {
|
||||
convert_and_validate_as_attribute!(f64, Some(-12.5 as f64), Some("-12.5"));
|
||||
convert_and_validate_as_attribute!(f64, None, None);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn de_option_struct() {
|
||||
#[derive(YaSerialize, Debug, PartialEq)]
|
||||
struct Test {
|
||||
field: SubTest
|
||||
}
|
||||
|
||||
#[derive(YaSerialize, Debug, PartialEq)]
|
||||
struct SubTest {
|
||||
content: Option<String>
|
||||
}
|
||||
|
||||
impl Default for SubTest {
|
||||
fn default() -> Self {
|
||||
SubTest {
|
||||
content: None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
convert_and_validate!(Test, Some(Test{field: SubTest{content: Some("value".to_string())}}), Some("<Test><field><content>value</content></field></Test>"));
|
||||
convert_and_validate!(Test, None, None);
|
||||
}
|
||||
|
||||
@@ -273,7 +273,7 @@ fn ser_enum() {
|
||||
},
|
||||
};
|
||||
|
||||
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><color><custom><enabled>true</enabled><color><red>0</red><green>128</green><blue>255</blue></color><alpha>Opaque</alpha><alphas>Opaque</alphas><alphas>Transparent</alphas></custom></color></base>";
|
||||
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><color><enabled>true</enabled><color><red>0</red><green>128</green><blue>255</blue></color><alpha>Opaque</alpha><alphas>Opaque</alphas><alphas>Transparent</alphas></color></base>";
|
||||
convert_and_validate!(model, content);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user