2015-03-08 13:05:01 -05:00
|
|
|
#![feature(custom_derive, plugin)]
|
2015-03-09 00:56:33 -05:00
|
|
|
#![plugin(serde_macros)]
|
2015-02-20 10:21:15 -06:00
|
|
|
|
2015-03-09 00:56:33 -05:00
|
|
|
extern crate serde;
|
2015-02-20 10:21:15 -06:00
|
|
|
|
2015-03-06 21:11:47 -06:00
|
|
|
use std::collections::BTreeMap;
|
2015-03-09 00:56:33 -05:00
|
|
|
use serde::json::{self, Value};
|
2015-03-06 21:11:47 -06:00
|
|
|
|
|
|
|
macro_rules! btreemap {
|
|
|
|
() => {
|
|
|
|
BTreeMap::new()
|
|
|
|
};
|
|
|
|
($($key:expr => $value:expr),+) => {
|
|
|
|
{
|
|
|
|
let mut map = BTreeMap::new();
|
|
|
|
$(map.insert($key, $value);)+
|
|
|
|
map
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-25 10:14:20 -06:00
|
|
|
/*
|
2015-02-20 10:21:15 -06:00
|
|
|
trait Trait {
|
|
|
|
type Type;
|
|
|
|
}
|
2015-02-25 10:14:20 -06:00
|
|
|
*/
|
2015-02-20 10:21:15 -06:00
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2015-03-03 22:29:59 -06:00
|
|
|
struct NamedUnit;
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Debug, PartialEq, Serialize)]
|
2015-03-15 19:47:25 -05:00
|
|
|
struct SerNamedTuple<'a, 'b, A: 'a, B: 'b, C>(&'a A, &'b mut B, C);
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Debug, PartialEq, Deserialize)]
|
2015-03-15 19:47:25 -05:00
|
|
|
struct DeNamedTuple<A, B, C>(A, B, C);
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Debug, PartialEq, Serialize)]
|
2015-03-15 19:47:25 -05:00
|
|
|
struct SerNamedMap<'a, 'b, A: 'a, B: 'b, C> {
|
|
|
|
a: &'a A,
|
|
|
|
b: &'b mut B,
|
|
|
|
c: C,
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Debug, PartialEq, Deserialize)]
|
2015-03-15 19:47:25 -05:00
|
|
|
struct DeNamedMap<A, B, C> {
|
|
|
|
a: A,
|
|
|
|
b: B,
|
|
|
|
c: C,
|
|
|
|
}
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Debug, PartialEq, Serialize)]
|
2015-03-08 21:07:23 -05:00
|
|
|
enum SerEnum<'a, B: 'a, C: /* Trait + */ 'a, D> where D: /* Trait + */ 'a {
|
2015-02-20 10:21:15 -06:00
|
|
|
Unit,
|
|
|
|
Seq(
|
|
|
|
i8,
|
2015-03-06 21:11:47 -06:00
|
|
|
B,
|
2015-02-20 10:21:15 -06:00
|
|
|
&'a C,
|
2015-03-08 21:07:23 -05:00
|
|
|
//C::Type,
|
2015-03-06 21:11:47 -06:00
|
|
|
&'a mut D,
|
|
|
|
//<D as Trait>::Type,
|
2015-02-20 10:21:15 -06:00
|
|
|
),
|
|
|
|
Map {
|
|
|
|
a: i8,
|
2015-03-06 21:11:47 -06:00
|
|
|
b: B,
|
|
|
|
c: &'a C,
|
|
|
|
//d: C::Type,
|
|
|
|
e: &'a mut D,
|
|
|
|
//f: <D as Trait>::Type,
|
2015-02-20 10:21:15 -06:00
|
|
|
},
|
2015-03-14 01:44:18 -05:00
|
|
|
|
|
|
|
// Make sure we can support more than one variant.
|
|
|
|
_Unit2,
|
|
|
|
_Seq2(
|
|
|
|
i8,
|
|
|
|
B,
|
|
|
|
&'a C,
|
|
|
|
//C::Type,
|
|
|
|
&'a mut D,
|
|
|
|
//<D as Trait>::Type,
|
|
|
|
),
|
|
|
|
_Map2 {
|
|
|
|
a: i8,
|
|
|
|
b: B,
|
|
|
|
c: &'a C,
|
|
|
|
//d: C::Type,
|
|
|
|
e: &'a mut D,
|
|
|
|
//f: <D as Trait>::Type,
|
|
|
|
},
|
2015-02-20 10:21:15 -06:00
|
|
|
}
|
|
|
|
|
2015-03-19 09:49:42 -05:00
|
|
|
#[derive(Debug, PartialEq, Deserialize)]
|
2015-03-08 21:07:23 -05:00
|
|
|
enum DeEnum<B, C: /* Trait */, D> /* where D: Trait */ {
|
|
|
|
Unit,
|
|
|
|
Seq(
|
|
|
|
i8,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
//C::Type,
|
|
|
|
D,
|
|
|
|
//<D as Trait>::Type,
|
|
|
|
),
|
|
|
|
Map {
|
|
|
|
a: i8,
|
|
|
|
b: B,
|
|
|
|
c: C,
|
|
|
|
//d: C::Type,
|
|
|
|
e: D,
|
|
|
|
//f: <D as Trait>::Type,
|
|
|
|
},
|
2015-03-14 01:44:18 -05:00
|
|
|
|
|
|
|
// Make sure we can support more than one variant.
|
|
|
|
_Unit2,
|
|
|
|
_Seq2(
|
|
|
|
i8,
|
|
|
|
B,
|
|
|
|
C,
|
|
|
|
//C::Type,
|
|
|
|
D,
|
|
|
|
//<D as Trait>::Type,
|
|
|
|
),
|
|
|
|
_Map2 {
|
|
|
|
a: i8,
|
|
|
|
b: B,
|
|
|
|
c: C,
|
|
|
|
//d: C::Type,
|
|
|
|
e: D,
|
|
|
|
//f: <D as Trait>::Type,
|
|
|
|
},
|
2015-03-08 21:07:23 -05:00
|
|
|
}
|
|
|
|
|
2015-03-03 11:50:57 -06:00
|
|
|
#[test]
|
2015-03-06 21:11:47 -06:00
|
|
|
fn test_named_unit() {
|
|
|
|
let named_unit = NamedUnit;
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_string(&named_unit).unwrap(),
|
|
|
|
"null".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_value(&named_unit),
|
|
|
|
Value::Null
|
|
|
|
);
|
|
|
|
|
2015-04-02 21:13:25 -05:00
|
|
|
let v: NamedUnit = json::from_str("null").unwrap();
|
2015-03-06 21:11:47 -06:00
|
|
|
assert_eq!(v, named_unit);
|
|
|
|
|
2015-04-02 21:13:25 -05:00
|
|
|
let v: NamedUnit = json::from_value(Value::Null).unwrap();
|
2015-03-06 21:11:47 -06:00
|
|
|
assert_eq!(v, named_unit);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-03-08 13:39:20 -05:00
|
|
|
fn test_ser_named_tuple() {
|
2015-03-06 21:11:47 -06:00
|
|
|
let a = 5;
|
|
|
|
let mut b = 6;
|
|
|
|
let c = 7;
|
2015-03-15 19:47:25 -05:00
|
|
|
let named_tuple = SerNamedTuple(&a, &mut b, c);
|
2015-03-06 21:11:47 -06:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_string(&named_tuple).unwrap(),
|
|
|
|
"[5,6,7]"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_value(&named_tuple),
|
2015-03-18 22:51:48 -05:00
|
|
|
Value::Array(vec![Value::U64(5), Value::U64(6), Value::U64(7)])
|
2015-03-06 21:11:47 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-08 13:39:20 -05:00
|
|
|
#[test]
|
|
|
|
fn test_de_named_tuple() {
|
2015-04-02 21:13:25 -05:00
|
|
|
let v: DeNamedTuple<i32, i32, i32> = json::from_str("[1,2,3]").unwrap();
|
2015-03-08 13:39:20 -05:00
|
|
|
assert_eq!(
|
2015-04-02 21:13:25 -05:00
|
|
|
v,
|
2015-03-15 19:47:25 -05:00
|
|
|
DeNamedTuple(1, 2, 3)
|
2015-03-08 13:39:20 -05:00
|
|
|
);
|
|
|
|
|
2015-04-02 21:13:25 -05:00
|
|
|
let v: Value = json::from_str("[1,2,3]").unwrap();
|
2015-03-08 13:39:20 -05:00
|
|
|
assert_eq!(
|
2015-04-02 21:13:25 -05:00
|
|
|
v,
|
2015-03-08 13:39:20 -05:00
|
|
|
Value::Array(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
Value::U64(1),
|
|
|
|
Value::U64(2),
|
|
|
|
Value::U64(3),
|
2015-03-08 13:39:20 -05:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-07 00:14:13 -06:00
|
|
|
#[test]
|
2015-03-08 20:46:41 -05:00
|
|
|
fn test_ser_named_map() {
|
2015-03-07 00:14:13 -06:00
|
|
|
let a = 5;
|
|
|
|
let mut b = 6;
|
|
|
|
let c = 7;
|
2015-03-15 19:47:25 -05:00
|
|
|
let named_map = SerNamedMap {
|
2015-03-07 00:14:13 -06:00
|
|
|
a: &a,
|
|
|
|
b: &mut b,
|
|
|
|
c: c,
|
|
|
|
};
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_string(&named_map).unwrap(),
|
|
|
|
"{\"a\":5,\"b\":6,\"c\":7}"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
json::to_value(&named_map),
|
|
|
|
Value::Object(btreemap![
|
2015-03-18 22:51:48 -05:00
|
|
|
"a".to_string() => Value::U64(5),
|
|
|
|
"b".to_string() => Value::U64(6),
|
|
|
|
"c".to_string() => Value::U64(7)
|
2015-03-07 00:14:13 -06:00
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-08 20:46:41 -05:00
|
|
|
#[test]
|
|
|
|
fn test_de_named_map() {
|
2015-03-15 19:47:25 -05:00
|
|
|
let v = DeNamedMap {
|
2015-03-08 20:46:41 -05:00
|
|
|
a: 5,
|
|
|
|
b: 6,
|
|
|
|
c: 7,
|
|
|
|
};
|
|
|
|
|
2015-04-02 21:13:25 -05:00
|
|
|
let v2: DeNamedMap<i32, i32, i32> = json::from_str(
|
|
|
|
"{\"a\":5,\"b\":6,\"c\":7}"
|
|
|
|
).unwrap();
|
|
|
|
assert_eq!(v, v2);
|
|
|
|
|
|
|
|
let v2 = json::from_value(Value::Object(btreemap![
|
|
|
|
"a".to_string() => Value::U64(5),
|
|
|
|
"b".to_string() => Value::U64(6),
|
|
|
|
"c".to_string() => Value::U64(7)
|
|
|
|
])).unwrap();
|
|
|
|
assert_eq!(v, v2);
|
2015-03-08 20:46:41 -05:00
|
|
|
}
|
|
|
|
|
2015-03-06 21:11:47 -06:00
|
|
|
#[test]
|
2015-03-08 21:07:23 -05:00
|
|
|
fn test_ser_enum_unit() {
|
2015-03-06 21:11:47 -06:00
|
|
|
assert_eq!(
|
2015-03-08 21:07:23 -05:00
|
|
|
json::to_string(&SerEnum::Unit::<u32, u32, u32>).unwrap(),
|
2015-03-06 21:11:47 -06:00
|
|
|
"{\"Unit\":[]}"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2015-03-08 21:07:23 -05:00
|
|
|
json::to_value(&SerEnum::Unit::<u32, u32, u32>),
|
2015-03-06 21:11:47 -06:00
|
|
|
Value::Object(btreemap!(
|
|
|
|
"Unit".to_string() => Value::Array(vec![]))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-03-08 21:07:23 -05:00
|
|
|
fn test_ser_enum_seq() {
|
2015-03-06 21:11:47 -06:00
|
|
|
let a = 1;
|
|
|
|
let b = 2;
|
|
|
|
let c = 3;
|
|
|
|
//let d = 4;
|
|
|
|
let mut e = 5;
|
|
|
|
//let f = 6;
|
|
|
|
|
|
|
|
assert_eq!(
|
2015-03-08 21:07:23 -05:00
|
|
|
json::to_string(&SerEnum::Seq(
|
2015-03-06 21:11:47 -06:00
|
|
|
a,
|
|
|
|
b,
|
|
|
|
&c,
|
|
|
|
//d,
|
|
|
|
&mut e,
|
|
|
|
//f,
|
|
|
|
)).unwrap(),
|
|
|
|
"{\"Seq\":[1,2,3,5]}".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2015-03-08 21:07:23 -05:00
|
|
|
json::to_value(&SerEnum::Seq(
|
2015-03-06 21:11:47 -06:00
|
|
|
a,
|
|
|
|
b,
|
|
|
|
&c,
|
|
|
|
//d,
|
|
|
|
&mut e,
|
|
|
|
//e,
|
|
|
|
)),
|
|
|
|
Value::Object(btreemap!(
|
|
|
|
"Seq".to_string() => Value::Array(vec![
|
2015-03-18 22:51:48 -05:00
|
|
|
Value::U64(1),
|
|
|
|
Value::U64(2),
|
|
|
|
Value::U64(3),
|
|
|
|
//Value::U64(4),
|
|
|
|
Value::U64(5),
|
|
|
|
//Value::U64(6),
|
2015-03-06 21:11:47 -06:00
|
|
|
])
|
|
|
|
))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2015-03-08 21:07:23 -05:00
|
|
|
fn test_ser_enum_map() {
|
2015-03-06 21:11:47 -06:00
|
|
|
let a = 1;
|
|
|
|
let b = 2;
|
|
|
|
let c = 3;
|
|
|
|
//let d = 4;
|
|
|
|
let mut e = 5;
|
|
|
|
//let f = 6;
|
|
|
|
|
|
|
|
assert_eq!(
|
2015-03-08 21:07:23 -05:00
|
|
|
json::to_string(&SerEnum::Map {
|
2015-03-06 21:11:47 -06:00
|
|
|
a: a,
|
|
|
|
b: b,
|
|
|
|
c: &c,
|
|
|
|
//d: d,
|
|
|
|
e: &mut e,
|
|
|
|
//f: f,
|
|
|
|
}).unwrap(),
|
|
|
|
"{\"Map\":{\"a\":1,\"b\":2,\"c\":3,\"e\":5}}".to_string()
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2015-03-08 21:07:23 -05:00
|
|
|
json::to_value(&SerEnum::Map {
|
2015-03-06 21:11:47 -06:00
|
|
|
a: a,
|
|
|
|
b: b,
|
|
|
|
c: &c,
|
|
|
|
//d: d,
|
|
|
|
e: &mut e,
|
|
|
|
//f: f,
|
|
|
|
}),
|
|
|
|
Value::Object(btreemap!(
|
|
|
|
"Map".to_string() => Value::Object(btreemap![
|
2015-03-18 22:51:48 -05:00
|
|
|
"a".to_string() => Value::U64(1),
|
|
|
|
"b".to_string() => Value::U64(2),
|
|
|
|
"c".to_string() => Value::U64(3),
|
|
|
|
//"d".to_string() => Value::U64(4)
|
|
|
|
"e".to_string() => Value::U64(5)
|
|
|
|
//"f".to_string() => Value::U64(6)
|
2015-03-06 21:11:47 -06:00
|
|
|
])
|
|
|
|
))
|
|
|
|
);
|
2015-03-03 11:50:57 -06:00
|
|
|
}
|
2015-03-08 21:07:23 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_de_enum_unit() {
|
2015-04-02 21:13:25 -05:00
|
|
|
let v: DeEnum<_, _, _> = json::from_str("{\"Unit\":[]}").unwrap();
|
2015-03-08 21:07:23 -05:00
|
|
|
assert_eq!(
|
2015-04-02 21:13:25 -05:00
|
|
|
v,
|
2015-03-08 21:07:23 -05:00
|
|
|
DeEnum::Unit::<u32, u32, u32>
|
|
|
|
);
|
|
|
|
|
2015-04-02 21:13:25 -05:00
|
|
|
let v: DeEnum<_, _, _> = json::from_value(Value::Object(btreemap!(
|
|
|
|
"Unit".to_string() => Value::Array(vec![]))
|
|
|
|
)).unwrap();
|
2015-03-08 21:07:23 -05:00
|
|
|
assert_eq!(
|
2015-04-02 21:13:25 -05:00
|
|
|
v,
|
2015-03-08 21:07:23 -05:00
|
|
|
DeEnum::Unit::<u32, u32, u32>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_de_enum_seq() {
|
|
|
|
let a = 1;
|
|
|
|
let b = 2;
|
|
|
|
let c = 3;
|
|
|
|
//let d = 4;
|
|
|
|
let e = 5;
|
|
|
|
//let f = 6;
|
|
|
|
|
2015-04-02 21:13:25 -05:00
|
|
|
let v: DeEnum<_, _, _> = json::from_str("{\"Seq\":[1,2,3,5]}").unwrap();
|
2015-03-08 21:07:23 -05:00
|
|
|
assert_eq!(
|
2015-04-02 21:13:25 -05:00
|
|
|
v,
|
2015-03-08 21:07:23 -05:00
|
|
|
DeEnum::Seq(
|
|
|
|
a,
|
|
|
|
b,
|
|
|
|
c,
|
|
|
|
//d,
|
|
|
|
e,
|
|
|
|
//f,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2015-04-02 21:13:25 -05:00
|
|
|
let v: DeEnum<_, _, _> = json::from_value(Value::Object(btreemap!(
|
|
|
|
"Seq".to_string() => Value::Array(vec![
|
|
|
|
Value::U64(1),
|
|
|
|
Value::U64(2),
|
|
|
|
Value::U64(3),
|
|
|
|
//Value::U64(4),
|
|
|
|
Value::U64(5),
|
|
|
|
//Value::U64(6),
|
|
|
|
])
|
|
|
|
))).unwrap();
|
2015-03-08 21:07:23 -05:00
|
|
|
assert_eq!(
|
2015-04-02 21:13:25 -05:00
|
|
|
v,
|
2015-03-08 21:07:23 -05:00
|
|
|
DeEnum::Seq(
|
|
|
|
a,
|
|
|
|
b,
|
|
|
|
c,
|
|
|
|
//d,
|
|
|
|
e,
|
|
|
|
//e,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_de_enum_map() {
|
|
|
|
let a = 1;
|
|
|
|
let b = 2;
|
|
|
|
let c = 3;
|
|
|
|
//let d = 4;
|
|
|
|
let e = 5;
|
|
|
|
//let f = 6;
|
|
|
|
|
2015-04-02 21:13:25 -05:00
|
|
|
let v: DeEnum<_, _, _> = json::from_str(
|
|
|
|
"{\"Map\":{\"a\":1,\"b\":2,\"c\":3,\"e\":5}}"
|
|
|
|
).unwrap();
|
2015-03-08 21:07:23 -05:00
|
|
|
assert_eq!(
|
2015-04-02 21:13:25 -05:00
|
|
|
v,
|
2015-03-08 21:07:23 -05:00
|
|
|
DeEnum::Map {
|
|
|
|
a: a,
|
|
|
|
b: b,
|
|
|
|
c: c,
|
|
|
|
//d: d,
|
|
|
|
e: e,
|
|
|
|
//f: f,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
2015-04-02 21:13:25 -05:00
|
|
|
let v: DeEnum<_, _, _> = json::from_value(Value::Object(btreemap!(
|
|
|
|
"Map".to_string() => Value::Object(btreemap![
|
|
|
|
"a".to_string() => Value::U64(1),
|
|
|
|
"b".to_string() => Value::U64(2),
|
|
|
|
"c".to_string() => Value::U64(3),
|
|
|
|
//"d".to_string() => Value::U64(4)
|
|
|
|
"e".to_string() => Value::U64(5)
|
|
|
|
//"f".to_string() => Value::U64(6)
|
|
|
|
])
|
|
|
|
))).unwrap();
|
|
|
|
|
2015-03-08 21:07:23 -05:00
|
|
|
assert_eq!(
|
2015-04-02 21:13:25 -05:00
|
|
|
v,
|
2015-03-08 21:07:23 -05:00
|
|
|
DeEnum::Map {
|
|
|
|
a: a,
|
|
|
|
b: b,
|
|
|
|
c: c,
|
|
|
|
//d: d,
|
|
|
|
e: e,
|
|
|
|
//f: f,
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|