update code format

This commit is contained in:
Marc-Antoine Arnaud
2018-05-23 17:14:53 +02:00
parent 8b3d98d911
commit d63e9d27d8
18 changed files with 1218 additions and 970 deletions

View File

@@ -1,4 +1,3 @@
use std::io::Read;
use xml::reader::{EventReader, ParserConfig, XmlEvent};
use xml::name::OwnedName;
@@ -56,9 +55,9 @@ impl<'de, R: Read> Deserializer<R> {
loop {
if let Ok(next) = self.reader.next() {
match next {
XmlEvent::StartDocument { .. } |
XmlEvent::ProcessingInstruction { .. } |
XmlEvent::Comment(_) => { /* skip */ },
XmlEvent::StartDocument { .. }
| XmlEvent::ProcessingInstruction { .. }
| XmlEvent::Comment(_) => { /* skip */ }
other => return Ok(other),
}
} else {
@@ -77,11 +76,11 @@ impl<'de, R: Read> Deserializer<R> {
match next {
XmlEvent::StartElement { .. } => {
self.depth += 1;
},
}
XmlEvent::EndElement { .. } => {
self.depth -= 1;
},
_ => {},
}
_ => {}
}
debug!("Fetched {:?}", next);
Ok(next)
@@ -113,14 +112,13 @@ impl<'de, R: Read> Deserializer<R> {
}
pub fn expect_end_element(&mut self, start_name: OwnedName) -> Result<(), String> {
if let XmlEvent::EndElement{ name, .. } = self.next()? {
if let XmlEvent::EndElement { name, .. } = self.next()? {
if name == start_name {
Ok(())
} else {
Err(format!(
"End tag </{}> didn't match the start tag <{}>",
name.local_name,
start_name.local_name
name.local_name, start_name.local_name
))
}
} else {

View File

@@ -1,4 +1,3 @@
#[macro_use]
extern crate log;
extern crate xml;
@@ -13,11 +12,11 @@ use xml::writer::XmlEvent;
pub mod de;
pub mod ser;
pub trait YaDeserialize : Sized {
pub trait YaDeserialize: Sized {
fn deserialize<R: Read>(reader: &mut de::Deserializer<R>) -> Result<Self, String>;
}
pub trait YaSerialize : Sized {
pub trait YaSerialize: Sized {
fn serialize<W: Write>(&self, writer: &mut ser::Serializer<W>) -> Result<(), String>;
}
@@ -25,60 +24,49 @@ pub trait Visitor<'de>: Sized {
/// The value produced by this visitor.
type Value;
fn visit_bool(self, v: &str) -> Result<Self::Value, String>
{
fn visit_bool(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected bool {}", v))
}
fn visit_i8(self, v: &str) -> Result<Self::Value, String>
{
fn visit_i8(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected i8 {}", v))
}
fn visit_u8(self, v: &str) -> Result<Self::Value, String>
{
fn visit_u8(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected u8 {}", v))
}
fn visit_i16(self, v: &str) -> Result<Self::Value, String>
{
fn visit_i16(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected i16 {}", v))
}
fn visit_u16(self, v: &str) -> Result<Self::Value, String>
{
fn visit_u16(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected u16 {}", v))
}
fn visit_i32(self, v: &str) -> Result<Self::Value, String>
{
fn visit_i32(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected i32 {}", v))
}
fn visit_u32(self, v: &str) -> Result<Self::Value, String>
{
fn visit_u32(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected u32 {}", v))
}
fn visit_i64(self, v: &str) -> Result<Self::Value, String>
{
fn visit_i64(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected i64 {}", v))
}
fn visit_u64(self, v: &str) -> Result<Self::Value, String>
{
fn visit_u64(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected u64 {}", v))
}
fn visit_str(self, v: &str) -> Result<Self::Value, String>
{
fn visit_str(self, v: &str) -> Result<Self::Value, String> {
Err(format!("Unexpected str {}", v))
}
#[inline]
#[cfg(any(feature = "std", feature = "alloc"))]
fn visit_string<String>(self, v: String) -> Result<Self::Value, String>
{
fn visit_string<String>(self, v: String) -> Result<Self::Value, String> {
self.visit_str(&v)
}
}

View File

@@ -1,4 +1,3 @@
use std::str;
use std::io::{Cursor, Write};
use xml::{EmitterConfig, EventWriter};
@@ -28,7 +27,10 @@ pub fn to_string_content<T: YaSerialize>(model: &T) -> Result<String, String> {
Ok(String::from(data))
}
pub fn serialize_with_writer_content<W: Write, T: YaSerialize>(model: &T, writer: W) -> Result<W, String> {
pub fn serialize_with_writer_content<W: Write, T: YaSerialize>(
model: &T,
writer: W,
) -> Result<W, String> {
let mut serializer = Serializer::new_for_inner(writer);
serializer.set_skip_start_end(true);
match model.serialize(&mut serializer) {
@@ -39,27 +41,25 @@ pub fn serialize_with_writer_content<W: Write, T: YaSerialize>(model: &T, writer
pub struct Serializer<W: Write> {
writer: EventWriter<W>,
skip_start_end: bool
skip_start_end: bool,
}
impl<'de, W: Write> Serializer<W> {
pub fn new(writer: EventWriter<W>) -> Self {
Serializer {
writer: writer,
skip_start_end: false
skip_start_end: false,
}
}
pub fn new_from_writer(writer: W) -> Self {
let config = EmitterConfig::new()
.cdata_to_characters(true);
let config = EmitterConfig::new().cdata_to_characters(true);
Self::new(EventWriter::new_with_config(writer, config))
}
pub fn new_for_inner(writer: W) -> Self {
let config = EmitterConfig::new()
.write_document_declaration(false);
let config = EmitterConfig::new().write_document_declaration(false);
Self::new(EventWriter::new_with_config(writer, config))
}
@@ -77,7 +77,9 @@ impl<'de, W: Write> Serializer<W> {
}
pub fn write<'a, E>(&mut self, event: E) -> xml::writer::Result<()>
where E: Into<XmlEvent<'a>> {
where
E: Into<XmlEvent<'a>>,
{
self.writer.write(event)
}
}

View File

@@ -1,10 +1,9 @@
#[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
extern crate xml;
#[macro_use]
extern crate log;
use std::io::Read;
use yaserde::YaDeserialize;
@@ -20,32 +19,33 @@ macro_rules! convert_and_validate {
#[test]
fn de_struct_namespace() {
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="book", prefix="ns", namespace="ns: http://www.sample.com/ns/domain")]
#[yaserde(root = "book", prefix = "ns", namespace = "ns: http://www.sample.com/ns/domain")]
pub struct Book {
#[yaserde(prefix="ns")]
author: String,
#[yaserde(prefix="ns")]
title: String,
#[yaserde(prefix = "ns")] author: String,
#[yaserde(prefix = "ns")] title: String,
}
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ns:book xmlns:ns=\"http://www.sample.com/ns/domain\"><ns:author>Antoine de Saint-Exupéry</ns:author><ns:title>Little prince</ns:title></ns:book>";
convert_and_validate!(content, Book, Book{
author: String::from("Antoine de Saint-Exupéry"),
title: String::from("Little prince")
});
convert_and_validate!(
content,
Book,
Book {
author: String::from("Antoine de Saint-Exupéry"),
title: String::from("Little prince"),
}
);
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ns:book xmlns:ns=\"http://www.sample.com/ns/domain2\"><ns:author>Antoine de Saint-Exupéry</ns:author><ns:title>Little prince</ns:title></ns:book>";
let loaded : Result<Book, String> = from_str(content);
let loaded: Result<Book, String> = from_str(content);
assert_eq!(loaded, Err("bad namespace".to_string()));
}
#[test]
fn de_enum_namespace() {
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="root", prefix="ns", namespace="ns: http://www.sample.com/ns/domain")]
#[yaserde(root = "root", prefix = "ns", namespace = "ns: http://www.sample.com/ns/domain")]
pub enum XmlStruct {
#[yaserde(prefix="ns")]
Item
#[yaserde(prefix = "ns")] Item,
}
impl Default for XmlStruct {

View File

@@ -1,10 +1,9 @@
#[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
extern crate xml;
#[macro_use]
extern crate log;
use std::io::Read;
use yaserde::YaDeserialize;

View File

@@ -1,10 +1,9 @@
#[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
extern crate xml;
#[macro_use]
extern crate log;
use std::io::Read;
use yaserde::YaDeserialize;
@@ -20,188 +19,203 @@ macro_rules! convert_and_validate {
#[test]
fn de_basic() {
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="book")]
#[yaserde(root = "book")]
pub struct Book {
author: String,
title: String,
}
let content = "<book><author>Antoine de Saint-Exupéry</author><title>Little prince</title></book>";
convert_and_validate!(content, Book, Book{
author: String::from("Antoine de Saint-Exupéry"),
title: String::from("Little prince")
});
let content =
"<book><author>Antoine de Saint-Exupéry</author><title>Little prince</title></book>";
convert_and_validate!(
content,
Book,
Book {
author: String::from("Antoine de Saint-Exupéry"),
title: String::from("Little prince"),
}
);
let content = "<book><title>Little prince</title><author>Antoine de Saint-Exupéry</author></book>";
convert_and_validate!(content, Book, Book{
author: String::from("Antoine de Saint-Exupéry"),
title: String::from("Little prince")
});
let content =
"<book><title>Little prince</title><author>Antoine de Saint-Exupéry</author></book>";
convert_and_validate!(
content,
Book,
Book {
author: String::from("Antoine de Saint-Exupéry"),
title: String::from("Little prince"),
}
);
}
#[test]
fn de_list_of_items() {
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="library")]
#[yaserde(root = "library")]
pub struct Library {
books: Vec<String>
books: Vec<String>,
}
let content = "<library><books>Little Prince</books><books>Harry Potter</books></library>";
convert_and_validate!(content, Library, Library{
books: vec![
String::from("Little Prince"),
String::from("Harry Potter")
]
});
convert_and_validate!(
content,
Library,
Library {
books: vec![String::from("Little Prince"), String::from("Harry Potter")],
}
);
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="libraries")]
#[yaserde(root = "libraries")]
pub struct Libraries {
library: Vec<Library>
library: Vec<Library>,
}
let content = "<libraries><library><books>Little Prince</books></library><library><books>Harry Potter</books></library></libraries>";
convert_and_validate!(content, Libraries, Libraries{
library: vec![
Library{
books: vec![
String::from("Little Prince")
]
},
Library{
books: vec![
String::from("Harry Potter")
]
}
]
});
convert_and_validate!(
content,
Libraries,
Libraries {
library: vec![
Library {
books: vec![String::from("Little Prince")],
},
Library {
books: vec![String::from("Harry Potter")],
},
],
}
);
}
#[test]
fn de_attributes() {
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(attribute)]
item: String,
sub: SubStruct
#[yaserde(attribute)] item: String,
sub: SubStruct,
}
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="sub")]
#[yaserde(root = "sub")]
pub struct SubStruct {
#[yaserde(attribute)]
subitem: String
#[yaserde(attribute)] subitem: String,
}
impl Default for SubStruct {
fn default() -> SubStruct {
SubStruct{
subitem: "".to_string()
SubStruct {
subitem: "".to_string(),
}
}
}
let content = "<base item=\"something\"><sub subitem=\"sub-something\"></sub></base>";
convert_and_validate!(content, XmlStruct, XmlStruct{
item: "something".to_string(),
sub: SubStruct{
subitem: "sub-something".to_string()
convert_and_validate!(
content,
XmlStruct,
XmlStruct {
item: "something".to_string(),
sub: SubStruct {
subitem: "sub-something".to_string(),
},
}
});
);
}
#[test]
fn de_rename() {
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(attribute, rename="Item")]
item: String,
#[yaserde(rename="sub")]
sub_struct: SubStruct
#[yaserde(attribute, rename = "Item")] item: String,
#[yaserde(rename = "sub")] sub_struct: SubStruct,
}
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="sub")]
#[yaserde(root = "sub")]
pub struct SubStruct {
#[yaserde(attribute, rename="sub_item")]
subitem: String,
#[yaserde(attribute, rename = "sub_item")] subitem: String,
}
impl Default for SubStruct {
fn default() -> SubStruct {
SubStruct{
subitem: "".to_string()
SubStruct {
subitem: "".to_string(),
}
}
}
let content = "<base Item=\"something\"><sub sub_item=\"sub_something\"></sub></base>";
convert_and_validate!(content, XmlStruct, XmlStruct{
item: "something".to_string(),
sub_struct: SubStruct{
subitem: "sub_something".to_string()
convert_and_validate!(
content,
XmlStruct,
XmlStruct {
item: "something".to_string(),
sub_struct: SubStruct {
subitem: "sub_something".to_string(),
},
}
});
);
}
#[test]
fn de_text_content_with_attributes() {
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(attribute, rename="Item")]
item: String,
#[yaserde(rename="sub")]
sub_struct: SubStruct
#[yaserde(attribute, rename = "Item")] item: String,
#[yaserde(rename = "sub")] sub_struct: SubStruct,
}
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="sub")]
#[yaserde(root = "sub")]
pub struct SubStruct {
#[yaserde(attribute, rename="sub_item")]
subitem: String,
#[yaserde(text)]
text: String
#[yaserde(attribute, rename = "sub_item")] subitem: String,
#[yaserde(text)] text: String,
}
impl Default for SubStruct {
fn default() -> SubStruct {
SubStruct{
SubStruct {
subitem: "".to_string(),
text: "".to_string(),
}
}
}
let content = "<base Item=\"something\"><sub sub_item=\"sub_something\">text_content</sub></base>";
convert_and_validate!(content, XmlStruct, XmlStruct{
item: "something".to_string(),
sub_struct: SubStruct{
subitem: "sub_something".to_string(),
text: "text_content".to_string()
let content =
"<base Item=\"something\"><sub sub_item=\"sub_something\">text_content</sub></base>";
convert_and_validate!(
content,
XmlStruct,
XmlStruct {
item: "something".to_string(),
sub_struct: SubStruct {
subitem: "sub_something".to_string(),
text: "text_content".to_string(),
},
}
});
);
}
#[test]
fn de_enum() {
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct XmlStruct {
background: Color
background: Color,
}
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct Colors {
items: Vec<Color>
items: Vec<Color>,
}
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="color")]
#[yaserde(root = "color")]
pub enum Color {
White,
Black,
@@ -222,7 +236,7 @@ fn de_enum() {
impl Default for RGBColor {
fn default() -> RGBColor {
RGBColor{
RGBColor {
red: "0".to_string(),
green: "0".to_string(),
blue: "0".to_string(),
@@ -230,28 +244,36 @@ fn de_enum() {
}
}
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><background>Black</background></base>";
convert_and_validate!(content, XmlStruct, XmlStruct{
background: Color::Black
});
let content =
"<?xml version=\"1.0\" encoding=\"utf-8\"?><base><background>Black</background></base>";
convert_and_validate!(
content,
XmlStruct,
XmlStruct {
background: Color::Black,
}
);
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><items>Black</items><items>White</items></base>";
convert_and_validate!(content, Colors, Colors{
items: vec![Color::Black, Color::White]
});
convert_and_validate!(
content,
Colors,
Colors {
items: vec![Color::Black, Color::White],
}
);
}
#[test]
fn de_attribute_enum() {
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(attribute)]
background: Color
#[yaserde(attribute)] background: Color,
}
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="color")]
#[yaserde(root = "color")]
pub enum Color {
White,
Black,
@@ -264,7 +286,11 @@ fn de_attribute_enum() {
}
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base background=\"Black\" />";
convert_and_validate!(content, XmlStruct, XmlStruct{
background: Color::Black
});
convert_and_validate!(
content,
XmlStruct,
XmlStruct {
background: Color::Black,
}
);
}

View File

@@ -1,10 +1,9 @@
#[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
extern crate xml;
#[macro_use]
extern crate log;
use std::io::Write;
use yaserde::YaSerialize;
@@ -20,14 +19,13 @@ macro_rules! convert_and_validate {
#[test]
fn ser_struct_namespace() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="root", prefix="ns", namespace="ns: http://www.sample.com/ns/domain")]
#[yaserde(root = "root", prefix = "ns", namespace = "ns: http://www.sample.com/ns/domain")]
pub struct XmlStruct {
#[yaserde(prefix="ns")]
item: String
#[yaserde(prefix = "ns")] item: String,
}
let model = XmlStruct {
item: "something".to_string()
item: "something".to_string(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><ns:root xmlns:ns=\"http://www.sample.com/ns/domain\"><ns:item>something</ns:item></ns:root>";
@@ -37,10 +35,9 @@ fn ser_struct_namespace() {
#[test]
fn ser_enum_namespace() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="root", prefix="ns", namespace="ns: http://www.sample.com/ns/domain")]
#[yaserde(root = "root", prefix = "ns", namespace = "ns: http://www.sample.com/ns/domain")]
pub enum XmlStruct {
#[yaserde(prefix="ns")]
Item
#[yaserde(prefix = "ns")] Item,
}
let model = XmlStruct::Item;
@@ -52,12 +49,11 @@ fn ser_enum_namespace() {
#[test]
fn ser_struct_multi_namespace() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="root", namespace="ns1: http://www.sample.com/ns/domain1", namespace="ns2: http://www.sample.com/ns/domain2")]
#[yaserde(root = "root", namespace = "ns1: http://www.sample.com/ns/domain1",
namespace = "ns2: http://www.sample.com/ns/domain2")]
pub struct XmlStruct {
#[yaserde(prefix="ns1")]
item_1: String,
#[yaserde(prefix="ns2")]
item_2: String,
#[yaserde(prefix = "ns1")] item_1: String,
#[yaserde(prefix = "ns2")] item_2: String,
}
let model = XmlStruct {
@@ -69,16 +65,14 @@ fn ser_struct_multi_namespace() {
convert_and_validate!(model, content);
}
#[test]
fn ser_enum_multi_namespace() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="root", namespace="ns1: http://www.sample.com/ns/domain1", namespace="ns2: http://www.sample.com/ns/domain2")]
#[yaserde(root = "root", namespace = "ns1: http://www.sample.com/ns/domain1",
namespace = "ns2: http://www.sample.com/ns/domain2")]
pub enum XmlStruct {
#[yaserde(prefix="ns1")]
Item1,
#[yaserde(prefix="ns2")]
Item2,
#[yaserde(prefix = "ns1")] Item1,
#[yaserde(prefix = "ns2")] Item2,
}
let model1 = XmlStruct::Item1;
@@ -89,16 +83,14 @@ fn ser_enum_multi_namespace() {
convert_and_validate!(model2, content);
}
#[test]
fn ser_struct_attribute_namespace() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="root", namespace="ns1: http://www.sample.com/ns/domain1", namespace="ns2: http://www.sample.com/ns/domain2")]
#[yaserde(root = "root", namespace = "ns1: http://www.sample.com/ns/domain1",
namespace = "ns2: http://www.sample.com/ns/domain2")]
pub struct XmlStruct {
#[yaserde(prefix="ns1")]
item_1: String,
#[yaserde(attribute, prefix="ns2")]
item_2: String,
#[yaserde(prefix = "ns1")] item_1: String,
#[yaserde(attribute, prefix = "ns2")] item_2: String,
}
let model = XmlStruct {
@@ -113,13 +105,14 @@ fn ser_struct_attribute_namespace() {
#[test]
fn ser_struct_default_namespace() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="tt", namespace="http://www.w3.org/ns/ttml", namespace="ttm: http://www.w3.org/ns/ttml#metadata")]
#[yaserde(root = "tt", namespace = "http://www.w3.org/ns/ttml",
namespace = "ttm: http://www.w3.org/ns/ttml#metadata")]
pub struct XmlStruct {
item: String
item: String,
}
let model = XmlStruct {
item: "something".to_string()
item: "something".to_string(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><tt xmlns=\"http://www.w3.org/ns/ttml\" xmlns:ttm=\"http://www.w3.org/ns/ttml#metadata\"><item>something</item></tt>";

View File

@@ -1,10 +1,9 @@
#[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
extern crate xml;
#[macro_use]
extern crate log;
use std::io::Write;
use yaserde::YaSerialize;

View File

@@ -1,10 +1,9 @@
#[macro_use]
extern crate log;
extern crate xml;
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
extern crate xml;
#[macro_use]
extern crate log;
use std::io::Write;
use yaserde::YaSerialize;
@@ -20,13 +19,13 @@ macro_rules! convert_and_validate {
#[test]
fn ser_basic() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct XmlStruct {
item: String
item: String,
}
let model = XmlStruct {
item: "something".to_string()
item: "something".to_string(),
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><item>something</item></base>";
@@ -36,44 +35,39 @@ fn ser_basic() {
#[test]
fn ser_list_of_items() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct XmlStruct {
items: Vec<String>
items: Vec<String>,
}
let model = XmlStruct{
items: vec![
"something1".to_string(),
"something2".to_string()
]
let model = XmlStruct {
items: vec!["something1".to_string(), "something2".to_string()],
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><items>something1</items><items>something2</items></base>";
convert_and_validate!(model, content);
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct XmlStructOfStruct {
items: Vec<SubStruct>
items: Vec<SubStruct>,
}
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="items")]
#[yaserde(root = "items")]
pub struct SubStruct {
field: String
field: String,
}
let model2 = XmlStructOfStruct{
let model2 = XmlStructOfStruct {
items: vec![
SubStruct{
field: "something1".to_string()
SubStruct {
field: "something1".to_string(),
},
SubStruct{
field: "something2".to_string()
}
]
SubStruct {
field: "something2".to_string(),
},
],
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><items><field>something1</field></items><items><field>something2</field></items></base>";
@@ -83,33 +77,31 @@ fn ser_list_of_items() {
#[test]
fn se_attributes() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(attribute)]
item: String,
sub: SubStruct
#[yaserde(attribute)] item: String,
sub: SubStruct,
}
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="sub")]
#[yaserde(root = "sub")]
pub struct SubStruct {
#[yaserde(attribute)]
subitem: String
#[yaserde(attribute)] subitem: String,
}
impl Default for SubStruct {
fn default() -> SubStruct {
SubStruct{
subitem: "".to_string()
SubStruct {
subitem: "".to_string(),
}
}
}
let model = XmlStruct{
let model = XmlStruct {
item: "something".to_string(),
sub: SubStruct{
subitem: "sub-something".to_string()
}
sub: SubStruct {
subitem: "sub-something".to_string(),
},
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base item=\"something\"><sub subitem=\"sub-something\" /></base>";
@@ -119,34 +111,31 @@ fn se_attributes() {
#[test]
fn ser_rename() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(attribute, rename="Item")]
item: String,
#[yaserde(rename="sub")]
sub_struct: SubStruct
#[yaserde(attribute, rename = "Item")] item: String,
#[yaserde(rename = "sub")] sub_struct: SubStruct,
}
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="sub")]
#[yaserde(root = "sub")]
pub struct SubStruct {
#[yaserde(attribute, rename="sub_item")]
subitem: String,
#[yaserde(attribute, rename = "sub_item")] subitem: String,
}
impl Default for SubStruct {
fn default() -> SubStruct {
SubStruct{
subitem: "".to_string()
SubStruct {
subitem: "".to_string(),
}
}
}
let model = XmlStruct{
let model = XmlStruct {
item: "something".to_string(),
sub_struct: SubStruct{
subitem: "sub_something".to_string()
}
sub_struct: SubStruct {
subitem: "sub_something".to_string(),
},
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base Item=\"something\"><sub sub_item=\"sub_something\" /></base>";
@@ -156,38 +145,34 @@ fn ser_rename() {
#[test]
fn ser_text_content_with_attributes() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(attribute, rename="Item")]
item: String,
#[yaserde(rename="sub")]
sub_struct: SubStruct
#[yaserde(attribute, rename = "Item")] item: String,
#[yaserde(rename = "sub")] sub_struct: SubStruct,
}
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="sub")]
#[yaserde(root = "sub")]
pub struct SubStruct {
#[yaserde(attribute, rename="sub_item")]
subitem: String,
#[yaserde(text)]
text: String,
#[yaserde(attribute, rename = "sub_item")] subitem: String,
#[yaserde(text)] text: String,
}
impl Default for SubStruct {
fn default() -> SubStruct {
SubStruct{
SubStruct {
subitem: "".to_string(),
text: "".to_string(),
}
}
}
let model = XmlStruct{
let model = XmlStruct {
item: "something".to_string(),
sub_struct: SubStruct{
sub_struct: SubStruct {
subitem: "sub_something".to_string(),
text: "text_content".to_string()
}
text: "text_content".to_string(),
},
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base Item=\"something\"><sub sub_item=\"sub_something\">text_content</sub></base>";
@@ -197,23 +182,23 @@ fn ser_text_content_with_attributes() {
#[test]
fn ser_enum() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct XmlStruct {
color: Color
color: Color,
}
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="color")]
#[yaserde(root = "color")]
pub enum Color {
White,
Black,
#[yaserde(rename="custom")]
#[yaserde(rename = "custom")]
Custom {
enabled: String,
color: RGBColor,
alpha: Alpha,
alphas: Vec<Alpha>,
}
},
}
impl Default for Color {
@@ -235,50 +220,45 @@ fn ser_enum() {
Opaque,
}
let model = XmlStruct{
color: Color::Black
let model = XmlStruct {
color: Color::Black,
};
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base><color>Black</color></base>";
convert_and_validate!(model, content);
let model = XmlStruct{
color: Color::Custom{
let model = XmlStruct {
color: Color::Custom {
enabled: "true".to_string(),
color: RGBColor{
color: RGBColor {
red: "0".to_string(),
green: "128".to_string(),
blue: "255".to_string(),
},
alpha: Alpha::Opaque,
alphas: vec![Alpha::Opaque, Alpha::Transparent]
}
alphas: vec![Alpha::Opaque, Alpha::Transparent],
},
};
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);
}
#[test]
fn ser_attribute_enum() {
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="base")]
#[yaserde(root = "base")]
pub struct XmlStruct {
#[yaserde(attribute)]
color: Color
#[yaserde(attribute)] color: Color,
}
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="color")]
#[yaserde(root = "color")]
pub enum Color {
#[yaserde(rename="pink")]
Pink,
#[yaserde(rename = "pink")] Pink,
}
let model = XmlStruct{
color: Color::Pink
};
let model = XmlStruct { color: Color::Pink };
let content = "<?xml version=\"1.0\" encoding=\"utf-8\"?><base color=\"pink\" />";
convert_and_validate!(model, content);