support type de/ser-ialization for attributes too
This commit is contained in:
@@ -27,6 +27,25 @@ macro_rules! convert_and_validate {
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! convert_and_validate_for_attribute {
|
||||
($type: ty, $value: expr, $content: expr) => {{
|
||||
#[derive(YaDeserialize, PartialEq, Debug)]
|
||||
#[yaserde(root = "data")]
|
||||
pub struct Data {
|
||||
#[yaserde(attribute)]
|
||||
item: $type,
|
||||
}
|
||||
|
||||
let model = Data { item: $value };
|
||||
|
||||
let content = String::from("<?xml version=\"1.0\" encoding=\"utf-8\"?><data item=\"") + $content
|
||||
+ "\" />";
|
||||
|
||||
let loaded: Result<Data, String> = from_str(&content);
|
||||
assert_eq!(loaded, Ok(model));
|
||||
}};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn de_type() {
|
||||
convert_and_validate!(bool, true, "true");
|
||||
@@ -42,4 +61,18 @@ fn de_type() {
|
||||
convert_and_validate!(u64, 12 as u64, "12");
|
||||
convert_and_validate!(i64, 12 as i64, "12");
|
||||
convert_and_validate!(i64, -12 as i64, "-12");
|
||||
|
||||
convert_and_validate_for_attribute!(bool, true, "true");
|
||||
convert_and_validate_for_attribute!(u8, 12 as u8, "12");
|
||||
convert_and_validate_for_attribute!(i8, 12 as i8, "12");
|
||||
convert_and_validate_for_attribute!(i8, -12 as i8, "-12");
|
||||
convert_and_validate_for_attribute!(u16, 12 as u16, "12");
|
||||
convert_and_validate_for_attribute!(i16, 12 as i16, "12");
|
||||
convert_and_validate_for_attribute!(i16, -12 as i16, "-12");
|
||||
convert_and_validate_for_attribute!(u32, 12 as u32, "12");
|
||||
convert_and_validate_for_attribute!(i32, 12 as i32, "12");
|
||||
convert_and_validate_for_attribute!(i32, -12 as i32, "-12");
|
||||
convert_and_validate_for_attribute!(u64, 12 as u64, "12");
|
||||
convert_and_validate_for_attribute!(i64, 12 as i64, "12");
|
||||
convert_and_validate_for_attribute!(i64, -12 as i64, "-12");
|
||||
}
|
||||
|
||||
@@ -25,6 +25,23 @@ macro_rules! convert_and_validate {
|
||||
}};
|
||||
}
|
||||
|
||||
macro_rules! convert_and_validate_as_attribute {
|
||||
($type: ty, $value: expr, $content: expr) => {{
|
||||
#[derive(YaSerialize, PartialEq, Debug)]
|
||||
#[yaserde(root = "data")]
|
||||
pub struct Data {
|
||||
#[yaserde(attribute)]
|
||||
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
|
||||
+ "\" />";
|
||||
assert_eq!(data, Ok(content));
|
||||
}};
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ser_type() {
|
||||
convert_and_validate!(bool, true, "true");
|
||||
@@ -40,4 +57,18 @@ fn ser_type() {
|
||||
convert_and_validate!(u64, 12 as u64, "12");
|
||||
convert_and_validate!(i64, 12 as i64, "12");
|
||||
convert_and_validate!(i64, -12 as i64, "-12");
|
||||
|
||||
convert_and_validate_as_attribute!(bool, true, "true");
|
||||
convert_and_validate_as_attribute!(u8, 12 as u8, "12");
|
||||
convert_and_validate_as_attribute!(i8, 12 as i8, "12");
|
||||
convert_and_validate_as_attribute!(i8, -12 as i8, "-12");
|
||||
convert_and_validate_as_attribute!(u16, 12 as u16, "12");
|
||||
convert_and_validate_as_attribute!(i16, 12 as i16, "12");
|
||||
convert_and_validate_as_attribute!(i16, -12 as i16, "-12");
|
||||
convert_and_validate_as_attribute!(u32, 12 as u32, "12");
|
||||
convert_and_validate_as_attribute!(i32, 12 as i32, "12");
|
||||
convert_and_validate_as_attribute!(i32, -12 as i32, "-12");
|
||||
convert_and_validate_as_attribute!(u64, 12 as u64, "12");
|
||||
convert_and_validate_as_attribute!(i64, 12 as i64, "12");
|
||||
convert_and_validate_as_attribute!(i64, -12 as i64, "-12");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user