Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add flatten support for root elements (Enum and Structs)
- Loading branch information
1 parent
38e5d32
commit 7b53515
Showing
4 changed files
with
89 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#[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_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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters