start to deserialize enum

This commit is contained in:
Marc-Antoine Arnaud
2018-04-29 15:16:28 +02:00
parent 5c3910c420
commit 6adbd71ebb
9 changed files with 395 additions and 28 deletions

View File

@@ -14,5 +14,5 @@ pub trait YaDeserialize : Sized {
}
pub trait YaSerialize : Sized {
fn derive_serialize<W: Write>(&self, read: &mut EventWriter<W>) -> Result<(), String>;
fn derive_serialize<W: Write>(&self, read: &mut EventWriter<W>, skip_start_end: bool) -> Result<(), String>;
}

View File

@@ -180,3 +180,59 @@ fn de_text_content_with_attributes() {
}
});
}
#[test]
fn de_enum() {
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="base")]
pub struct XmlStruct {
background: Color
}
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="color")]
pub enum Color {
White,
Black,
}
impl Default for Color {
fn default() -> Color {
Color::White
}
}
#[derive(YaDeserialize, PartialEq, Debug)]
pub struct RGBColor {
red: String,
green: String,
blue: String,
}
impl Default for RGBColor {
fn default() -> RGBColor {
RGBColor{
red: "0".to_string(),
green: "0".to_string(),
blue: "0".to_string(),
}
}
}
#[derive(YaDeserialize, PartialEq, Debug)]
pub enum Alpha {
Transparent,
Opaque,
}
impl Default for Alpha {
fn default() -> Alpha {
Alpha::Transparent
}
}
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><background>Black</background></base>";
convert_and_validate!(content, XmlStruct, XmlStruct{
background: Color::Black
});
}

View File

@@ -14,7 +14,7 @@ macro_rules! convert_and_validate {
($model:expr, $content:expr) => (
let mut buf = Cursor::new(Vec::new());
let mut writer = EventWriter::new(&mut buf);
let _status = $model.derive_serialize(&mut writer);
let _status = $model.derive_serialize(&mut writer, false);
let buffer = writer.into_inner();
let cursor = buffer.get_ref();
@@ -215,7 +215,7 @@ fn ser_enum() {
White,
Black,
#[yaserde(rename="custom")]
Custom{
Custom {
enabled: String,
color: RGBColor,
alpha: Alpha,
@@ -262,6 +262,6 @@ fn ser_enum() {
}
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><color><custom><enabled>true</enabled><RGBColor><red>0</red><green>128</green><blue>255</blue></RGBColor><Alpha>Opaque</Alpha><Alpha>Opaque</Alpha><Alpha>Transparent</Alpha></custom></color></base>";
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>";
convert_and_validate!(model, content);
}