support type de/ser-rialization

This commit is contained in:
Marc-Antoine Arnaud
2018-05-23 16:59:31 +02:00
parent 7a6ae6e6fe
commit a88f6535f4
7 changed files with 551 additions and 139 deletions

View File

@@ -25,6 +25,51 @@ pub trait Visitor<'de>: Sized {
/// The value produced by this visitor.
type Value;
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>
{
Err(format!("Unexpected i8 {}", v))
}
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>
{
Err(format!("Unexpected i16 {}", v))
}
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>
{
Err(format!("Unexpected i32 {}", v))
}
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>
{
Err(format!("Unexpected i64 {}", v))
}
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>
{
Err(format!("Unexpected str {}", v))
@@ -54,11 +99,13 @@ macro_rules! serialize_type {
serialize_type!(bool);
serialize_type!(char);
serialize_type!(usize);
serialize_type!(u8);
serialize_type!(u16);
serialize_type!(u32);
serialize_type!(u64);
serialize_type!(isize);
serialize_type!(i8);
serialize_type!(i16);
serialize_type!(i32);
@@ -66,4 +113,3 @@ serialize_type!(i64);
serialize_type!(f32);
serialize_type!(f64);

48
yaserde/tests/der_type.rs Normal file
View File

@@ -0,0 +1,48 @@
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
extern crate xml;
#[macro_use]
extern crate log;
use std::io::Read;
use yaserde::YaDeserialize;
use yaserde::de::from_str;
macro_rules! convert_and_validate {
($type:ty, $value:expr, $content:expr) => ({
#[derive(YaDeserialize, PartialEq, Debug)]
#[yaserde(root="data")]
pub struct Data {
item: $type
}
let model = Data {
item: $value
};
let content = String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data><item>") + $content + "</item></data>";
let loaded : Result<Data, String> = from_str(&content);
assert_eq!(loaded, Ok(model));
})
}
#[test]
fn de_type() {
convert_and_validate!(bool, true, "true");
convert_and_validate!(u8, 12 as u8, "12");
convert_and_validate!(i8, 12 as i8, "12");
convert_and_validate!(i8, -12 as i8, "-12");
convert_and_validate!(u16, 12 as u16, "12");
convert_and_validate!(i16, 12 as i16, "12");
convert_and_validate!(i16, -12 as i16, "-12");
convert_and_validate!(u32, 12 as u32, "12");
convert_and_validate!(i32, 12 as i32, "12");
convert_and_validate!(i32, -12 as i32, "-12");
convert_and_validate!(u64, 12 as u64, "12");
convert_and_validate!(i64, 12 as i64, "12");
convert_and_validate!(i64, -12 as i64, "-12");
}

46
yaserde/tests/se_type.rs Normal file
View File

@@ -0,0 +1,46 @@
extern crate yaserde;
#[macro_use]
extern crate yaserde_derive;
extern crate xml;
#[macro_use]
extern crate log;
use std::io::Write;
use yaserde::YaSerialize;
use yaserde::ser::to_string;
macro_rules! convert_and_validate {
($type:ty, $value:expr, $content:expr) => ({
#[derive(YaSerialize, PartialEq, Debug)]
#[yaserde(root="data")]
pub struct Data {
item: $type
}
let model = Data {
item: $value
};
let data : Result<String, String> = to_string(&model);
let content = String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data><item>") + $content + "</item></data>";
assert_eq!(data, Ok(content));
})
}
#[test]
fn ser_type() {
convert_and_validate!(bool, true, "true");
convert_and_validate!(u8, 12 as u8, "12");
convert_and_validate!(i8, 12 as i8, "12");
convert_and_validate!(i8, -12 as i8, "-12");
convert_and_validate!(u16, 12 as u16, "12");
convert_and_validate!(i16, 12 as i16, "12");
convert_and_validate!(i16, -12 as i16, "-12");
convert_and_validate!(u32, 12 as u32, "12");
convert_and_validate!(i32, 12 as i32, "12");
convert_and_validate!(i32, -12 as i32, "-12");
convert_and_validate!(u64, 12 as u64, "12");
convert_and_validate!(i64, 12 as i64, "12");
convert_and_validate!(i64, -12 as i64, "-12");
}