4 extern crate serde_derive
;
6 use std
::collections
::{BTreeMap, HashSet}
;
7 use serde
::{Deserialize, Deserializer}
;
10 use toml
::Value
::{Table, Integer, Array, Float}
;
13 ($e
:expr
) => (match $e
{
15 Err(e
) => panic
!("{} failed with {}", stringify
!($e
), e
),
19 macro_rules
! equivalent
{
20 ($literal
:expr
, $toml
:expr
,) => ({
22 let literal
= $literal
;
24 // In/out of Value is equivalent
26 assert_eq
!(t
!(Value
::try_from(literal
.clone())), toml
);
28 assert_eq
!(literal
, t
!(toml
.clone().try_into()));
30 // Through a string equivalent
31 println
!("to_string(literal)");
32 assert_eq
!(t
!(toml
::to_string(&literal
)), toml
.to_string());
33 println
!("to_string(toml)");
34 assert_eq
!(t
!(toml
::to_string(&toml
)), toml
.to_string());
35 println
!("literal, from_str(toml)");
36 assert_eq
!(literal
, t
!(toml
::from_str(&toml
.to_string())));
37 println
!("toml, from_str(toml)");
38 assert_eq
!(toml
, t
!(toml
::from_str(&toml
.to_string())));
43 ($ty
:ty
, $toml
:expr
, $error
:expr
) => ({
44 println
!("attempting parsing");
45 match toml
::from_str
::<$ty
>(&$toml
.to_string()) {
46 Ok(_
) => panic
!("successful"),
48 assert
!(e
.to_string().contains($error
),
53 println
!("attempting toml decoding");
54 match $toml
.try_into
::<$ty
>() {
55 Ok(_
) => panic
!("successful"),
57 assert
!(e
.to_string().contains($error
),
64 macro_rules
! decode( ($t
:expr
) => ({
68 macro_rules
! map( ($
($k
:ident
: $v
:expr
),*) => ({
69 let mut _m
= BTreeMap
::new();
70 $
(_m
.insert(stringify
!($k
).to_string(), $v
);)*
76 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
77 struct Foo { a: isize }
81 Table(map
! { a: Integer(2) }
),
87 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
94 Table(map
! { a_b: Integer(2) }
),
97 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
99 #[serde(rename = "a-b")]
103 let mut m
= BTreeMap
::new();
104 m
.insert("a-b".to_string(), Integer(2));
113 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
114 struct Foo { a: isize, b: Bar }
115 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
116 struct Bar { a: String }
119 Foo { a: 2, b: Bar { a: "test".to_string() }
},
123 a
: Value
::String("test".to_string())
130 fn application_decode_error() {
131 #[derive(PartialEq, Debug)]
132 struct Range10(usize);
133 impl Deserialize
for Range10
{
134 fn deserialize
<D
: Deserializer
>(d
: D
) -> Result
<Range10
, D
::Error
> {
135 let x
: usize = try
!(Deserialize
::deserialize(d
));
137 Err(serde
::de
::Error
::custom("more than 10"))
143 let d_good
= Integer(5);
144 let d_bad1
= Value
::String("not an isize".to_string());
145 let d_bad2
= Integer(11);
147 assert_eq
!(Range10(5), d_good
.try_into().unwrap());
149 let err1
: Result
<Range10
, _
> = d_bad1
.try_into();
150 assert
!(err1
.is_err());
151 let err2
: Result
<Range10
, _
> = d_bad2
.try_into();
152 assert
!(err2
.is_err());
157 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
158 struct Foo { a: Vec<isize> }
161 Foo { a: vec![1, 2, 3, 4] }
,
174 fn inner_structs_with_options() {
175 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
180 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
188 a
: Some(Box
::new(Foo
{
190 b
: Bar { a: "foo".to_string(), b: 4.5 }
,
192 b
: Bar { a: "bar".to_string(), b: 1.0 }
,
197 a
: Value
::String("foo".to_string()),
202 a
: Value
::String("bar".to_string()),
211 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
214 map
: BTreeMap
<String
, isize>,
220 let mut m
= BTreeMap
::new();
221 m
.insert("foo".to_string(), 10);
222 m
.insert("bar".to_string(), 4);
226 let mut s
= HashSet
::new();
236 set
: Array(vec
![Value
::String("a".to_string())])
243 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
244 struct Foo { a: Vec<Bar>, }
245 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
246 struct Bar { a: isize }
249 Foo { a: vec![Bar { a: 1 }
, Bar { a: 2 }
] },
252 Table(map
!{ a: Integer(1) }
),
253 Table(map
!{ a: Integer(2) }
),
261 #[derive(Deserialize)]
263 struct Foo { bar: isize }
268 bar
: Value
::String("a".to_string())
270 "invalid type: string \"a\", expected isize for key `bar`"
273 #[derive(Deserialize)]
275 struct Bar { foo: Foo }
281 bar
: Value
::String("a".to_string())
284 "invalid type: string \"a\", expected isize for key `foo.bar`"
289 fn missing_errors() {
290 #[derive(Serialize, Deserialize, PartialEq, Debug)]
291 struct Foo { bar: isize }
296 "missing field `bar`"
302 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
304 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
311 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
317 Foo { a: E::Bar(10) }
,
318 Table(map
! { a: Integer(10) }
),
322 Foo { a: E::Baz("foo".to_string()) }
,
323 Table(map
! { a: Value::String("foo".to_string()) }
),
327 Foo { a: E::Last(Foo2 { test: "test".to_string() }
) },
328 Table(map
! { a: Table(map! { test: Value::String("test".to_string()) }
) }),
333 // fn unused_fields() {
334 // #[derive(Serialize, Deserialize, PartialEq, Debug)]
335 // struct Foo { a: isize }
337 // let v = Foo { a: 2 };
338 // let mut d = Decoder::new(Table(map! {
342 // assert_eq!(v, t!(Deserialize::deserialize(&mut d)));
344 // assert_eq!(d.toml, Some(Table(map! {
350 // fn unused_fields2() {
351 // #[derive(Serialize, Deserialize, PartialEq, Debug)]
352 // struct Foo { a: Bar }
353 // #[derive(Serialize, Deserialize, PartialEq, Debug)]
354 // struct Bar { a: isize }
356 // let v = Foo { a: Bar { a: 2 } };
357 // let mut d = Decoder::new(Table(map! {
363 // assert_eq!(v, t!(Deserialize::deserialize(&mut d)));
365 // assert_eq!(d.toml, Some(Table(map! {
373 // fn unused_fields3() {
374 // #[derive(Serialize, Deserialize, PartialEq, Debug)]
375 // struct Foo { a: Bar }
376 // #[derive(Serialize, Deserialize, PartialEq, Debug)]
377 // struct Bar { a: isize }
379 // let v = Foo { a: Bar { a: 2 } };
380 // let mut d = Decoder::new(Table(map! {
385 // assert_eq!(v, t!(Deserialize::deserialize(&mut d)));
387 // assert_eq!(d.toml, None);
391 // fn unused_fields4() {
392 // #[derive(Serialize, Deserialize, PartialEq, Debug)]
393 // struct Foo { a: BTreeMap<String, String> }
395 // let v = Foo { a: map! { a, "foo".to_string() } };
396 // let mut d = Decoder::new(Table(map! {
398 // a, Value::String("foo".to_string())
401 // assert_eq!(v, t!(Deserialize::deserialize(&mut d)));
403 // assert_eq!(d.toml, None);
407 // fn unused_fields5() {
408 // #[derive(Serialize, Deserialize, PartialEq, Debug)]
409 // struct Foo { a: Vec<String> }
411 // let v = Foo { a: vec!["a".to_string()] };
412 // let mut d = Decoder::new(Table(map! {
413 // a, Array(vec![Value::String("a".to_string())])
415 // assert_eq!(v, t!(Deserialize::deserialize(&mut d)));
417 // assert_eq!(d.toml, None);
421 // fn unused_fields6() {
422 // #[derive(Serialize, Deserialize, PartialEq, Debug)]
423 // struct Foo { a: Option<Vec<String>> }
425 // let v = Foo { a: Some(vec![]) };
426 // let mut d = Decoder::new(Table(map! {
429 // assert_eq!(v, t!(Deserialize::deserialize(&mut d)));
431 // assert_eq!(d.toml, None);
435 // fn unused_fields7() {
436 // #[derive(Serialize, Deserialize, PartialEq, Debug)]
437 // struct Foo { a: Vec<Bar> }
438 // #[derive(Serialize, Deserialize, PartialEq, Debug)]
439 // struct Bar { a: isize }
441 // let v = Foo { a: vec![Bar { a: 1 }] };
442 // let mut d = Decoder::new(Table(map! {
443 // a, Array(vec![Table(map! {
448 // assert_eq!(v, t!(Deserialize::deserialize(&mut d)));
450 // assert_eq!(d.toml, Some(Table(map! {
451 // a, Array(vec![Table(map! {
459 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
460 struct Foo { a: Vec<Bar> }
461 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
466 Table(map
! {a: Array(Vec::new())}
),
472 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
473 struct Foo { a: Option<Vec<Bar>> }
474 #[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
483 Foo { a: Some(vec![]) }
,
484 Table(map
! { a: Array(vec![]) }
),
490 #[derive(Serialize, Deserialize)]
491 struct Foo { a: isize }
493 let toml
= Table(map
! { a: Integer(2), b: Integer(2) }
);
494 assert
!(toml
.clone().try_into
::<Foo
>().is_ok());
495 assert
!(toml
::from_str
::<Foo
>(&toml
.to_string()).is_ok());