2017-09-09 14:37:00 -05:00
|
|
|
#![deny(trivial_numeric_casts)]
|
2018-12-31 20:58:16 -06:00
|
|
|
#![allow(clippy::redundant_field_names)]
|
2018-01-13 16:28:12 -06:00
|
|
|
|
2018-04-19 11:52:27 -05:00
|
|
|
mod bytes;
|
|
|
|
|
2018-12-31 20:44:24 -06:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_test::{
|
2018-05-05 02:56:12 -05:00
|
|
|
assert_de_tokens, assert_de_tokens_error, assert_ser_tokens, assert_tokens, Token,
|
|
|
|
};
|
2015-03-06 21:11:47 -06:00
|
|
|
|
2017-02-02 14:10:07 -06:00
|
|
|
use std::collections::BTreeMap;
|
2016-06-28 23:47:54 -05:00
|
|
|
use std::marker::PhantomData;
|
2015-02-20 10:21:15 -06:00
|
|
|
|
2016-03-30 08:33:02 -05:00
|
|
|
// That tests that the derived Serialize implementation doesn't trigger
|
|
|
|
// any warning about `serializer` not being used, in case of empty enums.
|
|
|
|
#[derive(Serialize)]
|
|
|
|
#[allow(dead_code)]
|
|
|
|
#[deny(unused_variables)]
|
|
|
|
enum Void {}
|
|
|
|
|
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)]
|
2017-04-13 14:28:23 -05:00
|
|
|
enum SerEnum<'a, B: 'a, C: 'a, D>
|
|
|
|
where
|
|
|
|
D: 'a,
|
|
|
|
{
|
2015-02-20 10:21:15 -06:00
|
|
|
Unit,
|
2017-04-13 14:28:23 -05:00
|
|
|
Seq(i8, B, &'a C, &'a mut D),
|
|
|
|
Map { a: i8, b: B, c: &'a C, d: &'a mut D },
|
2015-03-14 01:44:18 -05:00
|
|
|
|
|
|
|
// Make sure we can support more than one variant.
|
|
|
|
_Unit2,
|
2017-04-13 14:28:23 -05:00
|
|
|
_Seq2(i8, B, &'a C, &'a mut D),
|
|
|
|
_Map2 { a: i8, b: B, c: &'a C, d: &'a mut D },
|
2015-02-20 10:21:15 -06:00
|
|
|
}
|
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2016-07-17 23:09:44 -05:00
|
|
|
enum DeEnum<B, C, D> {
|
2015-03-08 21:07:23 -05:00
|
|
|
Unit,
|
2017-04-13 14:28:23 -05:00
|
|
|
Seq(i8, B, C, D),
|
|
|
|
Map { a: i8, b: B, c: C, d: D },
|
2015-03-14 01:44:18 -05:00
|
|
|
|
|
|
|
// Make sure we can support more than one variant.
|
|
|
|
_Unit2,
|
2017-04-13 14:28:23 -05:00
|
|
|
_Seq2(i8, B, C, D),
|
|
|
|
_Map2 { a: i8, b: B, c: C, d: D },
|
2015-03-08 21:07:23 -05:00
|
|
|
}
|
|
|
|
|
2015-06-18 10:45:03 -05:00
|
|
|
#[derive(Serialize)]
|
|
|
|
enum Lifetimes<'a> {
|
|
|
|
LifetimeSeq(&'a i32),
|
|
|
|
NoLifetimeSeq(i32),
|
|
|
|
LifetimeMap { a: &'a i32 },
|
|
|
|
NoLifetimeMap { a: i32 },
|
|
|
|
}
|
|
|
|
|
2015-07-23 09:25:27 -05:00
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub struct GenericStruct<T> {
|
|
|
|
x: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2016-02-13 00:05:02 -06:00
|
|
|
pub struct GenericNewTypeStruct<T>(T);
|
2015-07-23 09:25:27 -05:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2015-07-30 11:38:09 -05:00
|
|
|
pub struct GenericTupleStruct<T, U>(T, U);
|
2015-07-23 09:25:27 -05:00
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2015-07-30 11:38:09 -05:00
|
|
|
pub enum GenericEnum<T, U> {
|
|
|
|
Unit,
|
2016-02-13 00:05:02 -06:00
|
|
|
NewType(T),
|
2015-07-30 11:38:09 -05:00
|
|
|
Seq(T, U),
|
|
|
|
Map { x: T, y: U },
|
2015-07-23 09:25:27 -05:00
|
|
|
}
|
|
|
|
|
2016-04-14 01:51:04 -05:00
|
|
|
trait AssociatedType {
|
|
|
|
type X;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AssociatedType for i32 {
|
|
|
|
type X = i32;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2017-04-13 14:28:23 -05:00
|
|
|
struct DefaultTyParam<T: AssociatedType<X = i32> = i32> {
|
|
|
|
phantom: PhantomData<T>,
|
2016-04-14 01:51:04 -05:00
|
|
|
}
|
|
|
|
|
2015-03-03 11:50:57 -06:00
|
|
|
#[test]
|
2015-03-06 21:11:47 -06:00
|
|
|
fn test_named_unit() {
|
2017-04-19 15:06:31 -05:00
|
|
|
assert_tokens(&NamedUnit, &[Token::UnitStruct { name: "NamedUnit" }]);
|
2015-03-06 21:11:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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-08-27 22:01:09 -05:00
|
|
|
assert_ser_tokens(
|
|
|
|
&SerNamedTuple(&a, &mut b, c),
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::TupleStruct {
|
|
|
|
name: "SerNamedTuple",
|
|
|
|
len: 3,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::I32(5),
|
|
|
|
Token::I32(6),
|
|
|
|
Token::I32(7),
|
2016-02-23 07:15:44 -06:00
|
|
|
Token::TupleStructEnd,
|
2015-08-27 22:01:09 -05:00
|
|
|
],
|
2015-03-06 21:11:47 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-08 13:39:20 -05:00
|
|
|
#[test]
|
|
|
|
fn test_de_named_tuple() {
|
2015-08-27 22:01:09 -05:00
|
|
|
assert_de_tokens(
|
|
|
|
&DeNamedTuple(5, 6, 7),
|
2016-06-29 01:50:19 -05:00
|
|
|
&[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Seq { len: Some(3) },
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::I32(5),
|
|
|
|
Token::I32(6),
|
|
|
|
Token::I32(7),
|
|
|
|
Token::SeqEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2015-03-08 13:39:20 -05:00
|
|
|
);
|
2016-02-13 00:05:02 -06:00
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&DeNamedTuple(5, 6, 7),
|
2016-06-29 01:50:19 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::TupleStruct {
|
|
|
|
name: "DeNamedTuple",
|
|
|
|
len: 3,
|
|
|
|
},
|
2016-02-13 00:05:02 -06:00
|
|
|
Token::I32(5),
|
|
|
|
Token::I32(6),
|
|
|
|
Token::I32(7),
|
2016-02-23 07:15:44 -06:00
|
|
|
Token::TupleStructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2016-02-13 00:05:02 -06:00
|
|
|
);
|
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-08-27 22:01:09 -05:00
|
|
|
assert_ser_tokens(
|
|
|
|
&SerNamedMap {
|
2017-12-23 22:13:08 -06:00
|
|
|
a: &a,
|
|
|
|
b: &mut b,
|
|
|
|
c: c,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "SerNamedMap",
|
|
|
|
len: 3,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::I32(5),
|
|
|
|
Token::Str("b"),
|
|
|
|
Token::I32(6),
|
|
|
|
Token::Str("c"),
|
|
|
|
Token::I32(7),
|
2016-02-13 00:05:02 -06:00
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2015-03-07 00:14:13 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-03-08 20:46:41 -05:00
|
|
|
#[test]
|
|
|
|
fn test_de_named_map() {
|
2015-08-27 22:01:09 -05:00
|
|
|
assert_de_tokens(
|
2017-04-13 14:28:23 -05:00
|
|
|
&DeNamedMap { a: 5, b: 6, c: 7 },
|
2016-06-29 01:50:19 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "DeNamedMap",
|
|
|
|
len: 3,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::I32(5),
|
|
|
|
Token::Str("b"),
|
|
|
|
Token::I32(6),
|
|
|
|
Token::Str("c"),
|
|
|
|
Token::I32(7),
|
2016-02-13 00:05:02 -06:00
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
);
|
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-08-27 22:01:09 -05:00
|
|
|
assert_ser_tokens(
|
|
|
|
&SerEnum::Unit::<u32, u32, u32>,
|
2018-04-30 03:41:22 -05:00
|
|
|
&[Token::UnitVariant {
|
|
|
|
name: "SerEnum",
|
|
|
|
variant: "Unit",
|
|
|
|
}],
|
2015-03-06 21:11:47 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[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;
|
2016-07-17 23:09:44 -05:00
|
|
|
let mut d = 4;
|
2015-03-06 21:11:47 -06:00
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
assert_ser_tokens(
|
2017-04-13 14:28:23 -05:00
|
|
|
&SerEnum::Seq(a, b, &c, &mut d),
|
2015-08-27 22:01:09 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::TupleVariant {
|
|
|
|
name: "SerEnum",
|
|
|
|
variant: "Seq",
|
|
|
|
len: 4,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::I8(1),
|
|
|
|
Token::I32(2),
|
|
|
|
Token::I32(3),
|
2016-07-17 23:09:44 -05:00
|
|
|
Token::I32(4),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::TupleVariantEnd,
|
2015-08-27 22:01:09 -05:00
|
|
|
],
|
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;
|
2016-07-17 23:09:44 -05:00
|
|
|
let mut d = 4;
|
2015-03-06 21:11:47 -06:00
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
assert_ser_tokens(
|
|
|
|
&SerEnum::Map {
|
2017-12-23 22:13:08 -06:00
|
|
|
a: a,
|
|
|
|
b: b,
|
|
|
|
c: &c,
|
|
|
|
d: &mut d,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::StructVariant {
|
|
|
|
name: "SerEnum",
|
|
|
|
variant: "Map",
|
|
|
|
len: 4,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(1),
|
|
|
|
Token::Str("b"),
|
|
|
|
Token::I32(2),
|
|
|
|
Token::Str("c"),
|
|
|
|
Token::I32(3),
|
2016-07-17 23:09:44 -05:00
|
|
|
Token::Str("d"),
|
|
|
|
Token::I32(4),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::StructVariantEnd,
|
2015-08-27 22:01:09 -05:00
|
|
|
],
|
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-08-27 22:01:09 -05:00
|
|
|
assert_tokens(
|
|
|
|
&DeEnum::Unit::<u32, u32, u32>,
|
2018-04-30 03:41:22 -05:00
|
|
|
&[Token::UnitVariant {
|
|
|
|
name: "DeEnum",
|
|
|
|
variant: "Unit",
|
|
|
|
}],
|
2015-03-08 21:07:23 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_de_enum_seq() {
|
|
|
|
let a = 1;
|
|
|
|
let b = 2;
|
|
|
|
let c = 3;
|
2016-07-17 23:09:44 -05:00
|
|
|
let d = 4;
|
2015-03-08 21:07:23 -05:00
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
assert_tokens(
|
2017-04-13 14:28:23 -05:00
|
|
|
&DeEnum::Seq(a, b, c, d),
|
2016-06-29 01:50:19 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::TupleVariant {
|
|
|
|
name: "DeEnum",
|
|
|
|
variant: "Seq",
|
|
|
|
len: 4,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::I8(1),
|
|
|
|
Token::I32(2),
|
|
|
|
Token::I32(3),
|
2016-07-17 23:09:44 -05:00
|
|
|
Token::I32(4),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::TupleVariantEnd,
|
2015-08-27 22:01:09 -05:00
|
|
|
],
|
2015-03-08 21:07:23 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_de_enum_map() {
|
|
|
|
let a = 1;
|
|
|
|
let b = 2;
|
|
|
|
let c = 3;
|
2016-07-17 23:09:44 -05:00
|
|
|
let d = 4;
|
2015-03-08 21:07:23 -05:00
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
assert_tokens(
|
|
|
|
&DeEnum::Map {
|
2017-12-23 22:13:08 -06:00
|
|
|
a: a,
|
|
|
|
b: b,
|
|
|
|
c: c,
|
|
|
|
d: d,
|
|
|
|
},
|
2016-06-29 01:50:19 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::StructVariant {
|
|
|
|
name: "DeEnum",
|
|
|
|
variant: "Map",
|
|
|
|
len: 4,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::I8(1),
|
|
|
|
Token::Str("b"),
|
|
|
|
Token::I32(2),
|
|
|
|
Token::Str("c"),
|
|
|
|
Token::I32(3),
|
2016-07-17 23:09:44 -05:00
|
|
|
Token::Str("d"),
|
|
|
|
Token::I32(4),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::StructVariantEnd,
|
2015-08-27 22:01:09 -05:00
|
|
|
],
|
2015-03-08 21:07:23 -05:00
|
|
|
);
|
|
|
|
}
|
2015-06-18 10:45:03 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_lifetimes() {
|
|
|
|
let value = 5;
|
2015-08-27 22:01:09 -05:00
|
|
|
|
|
|
|
assert_ser_tokens(
|
|
|
|
&Lifetimes::LifetimeSeq(&value),
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::NewtypeVariant {
|
|
|
|
name: "Lifetimes",
|
|
|
|
variant: "LifetimeSeq",
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::I32(5),
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2015-06-18 10:45:03 -05:00
|
|
|
);
|
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
assert_ser_tokens(
|
|
|
|
&Lifetimes::NoLifetimeSeq(5),
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::NewtypeVariant {
|
|
|
|
name: "Lifetimes",
|
|
|
|
variant: "NoLifetimeSeq",
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::I32(5),
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2015-06-18 10:45:03 -05:00
|
|
|
);
|
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
assert_ser_tokens(
|
|
|
|
&Lifetimes::LifetimeMap { a: &value },
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::StructVariant {
|
|
|
|
name: "Lifetimes",
|
|
|
|
variant: "LifetimeMap",
|
|
|
|
len: 1,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::I32(5),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::StructVariantEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2015-06-18 10:45:03 -05:00
|
|
|
);
|
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
assert_ser_tokens(
|
|
|
|
&Lifetimes::NoLifetimeMap { a: 5 },
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::StructVariant {
|
|
|
|
name: "Lifetimes",
|
|
|
|
variant: "NoLifetimeMap",
|
|
|
|
len: 1,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::I32(5),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::StructVariantEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2015-06-18 10:45:03 -05:00
|
|
|
);
|
|
|
|
}
|
2015-07-23 09:25:27 -05:00
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
#[test]
|
|
|
|
fn test_generic_struct() {
|
|
|
|
assert_tokens(
|
|
|
|
&GenericStruct { x: 5u32 },
|
2016-06-29 01:50:19 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "GenericStruct",
|
|
|
|
len: 1,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::Str("x"),
|
|
|
|
Token::U32(5),
|
2016-02-13 00:05:02 -06:00
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
);
|
2015-07-30 11:38:09 -05:00
|
|
|
}
|
2015-07-23 09:25:27 -05:00
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
#[test]
|
|
|
|
fn test_generic_newtype_struct() {
|
|
|
|
assert_tokens(
|
2016-02-13 00:05:02 -06:00
|
|
|
&GenericNewTypeStruct(5u32),
|
2017-12-23 22:13:08 -06:00
|
|
|
&[
|
|
|
|
Token::NewtypeStruct {
|
|
|
|
name: "GenericNewTypeStruct",
|
|
|
|
},
|
|
|
|
Token::U32(5),
|
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-04-23 12:51:49 -05:00
|
|
|
#[test]
|
|
|
|
fn test_untagged_newtype_struct() {
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum E {
|
|
|
|
Newtype(GenericNewTypeStruct<u32>),
|
|
|
|
Null,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&E::Newtype(GenericNewTypeStruct(5u32)),
|
|
|
|
&[
|
|
|
|
Token::NewtypeStruct {
|
|
|
|
name: "GenericNewTypeStruct",
|
|
|
|
},
|
|
|
|
Token::U32(5),
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_adjacently_tagged_newtype_struct() {
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(tag = "t", content = "c")]
|
|
|
|
enum E {
|
|
|
|
Newtype(GenericNewTypeStruct<u32>),
|
|
|
|
Null,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&E::Newtype(GenericNewTypeStruct(5u32)),
|
|
|
|
&[
|
2018-04-30 03:41:22 -05:00
|
|
|
Token::Struct { name: "E", len: 2 },
|
2018-04-23 12:51:49 -05:00
|
|
|
Token::Str("c"),
|
|
|
|
Token::NewtypeStruct {
|
|
|
|
name: "GenericNewTypeStruct",
|
|
|
|
},
|
|
|
|
Token::U32(5),
|
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Newtype"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-08-27 22:01:09 -05:00
|
|
|
#[test]
|
|
|
|
fn test_generic_tuple_struct() {
|
|
|
|
assert_tokens(
|
|
|
|
&GenericTupleStruct(5u32, 6u32),
|
2016-06-29 01:50:19 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::TupleStruct {
|
|
|
|
name: "GenericTupleStruct",
|
|
|
|
len: 2,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::U32(5),
|
|
|
|
Token::U32(6),
|
2016-02-23 07:15:44 -06:00
|
|
|
Token::TupleStructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generic_enum_unit() {
|
|
|
|
assert_tokens(
|
|
|
|
&GenericEnum::Unit::<u32, u32>,
|
2018-04-30 03:41:22 -05:00
|
|
|
&[Token::UnitVariant {
|
|
|
|
name: "GenericEnum",
|
|
|
|
variant: "Unit",
|
|
|
|
}],
|
2015-08-27 22:01:09 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generic_enum_newtype() {
|
|
|
|
assert_tokens(
|
2016-02-13 00:05:02 -06:00
|
|
|
&GenericEnum::NewType::<u32, u32>(5),
|
2016-06-29 01:50:19 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::NewtypeVariant {
|
|
|
|
name: "GenericEnum",
|
|
|
|
variant: "NewType",
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::U32(5),
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generic_enum_seq() {
|
|
|
|
assert_tokens(
|
|
|
|
&GenericEnum::Seq::<u32, u32>(5, 6),
|
2016-06-29 01:50:19 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::TupleVariant {
|
|
|
|
name: "GenericEnum",
|
|
|
|
variant: "Seq",
|
|
|
|
len: 2,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::U32(5),
|
|
|
|
Token::U32(6),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::TupleVariantEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_generic_enum_map() {
|
|
|
|
assert_tokens(
|
|
|
|
&GenericEnum::Map::<u32, u32> { x: 5, y: 6 },
|
2016-06-29 01:50:19 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::StructVariant {
|
|
|
|
name: "GenericEnum",
|
|
|
|
variant: "Map",
|
|
|
|
len: 2,
|
|
|
|
},
|
2015-08-27 22:01:09 -05:00
|
|
|
Token::Str("x"),
|
|
|
|
Token::U32(5),
|
|
|
|
Token::Str("y"),
|
|
|
|
Token::U32(6),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::StructVariantEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2015-08-27 22:01:09 -05:00
|
|
|
);
|
2015-07-23 09:25:27 -05:00
|
|
|
}
|
2016-04-14 01:51:04 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_default_ty_param() {
|
|
|
|
assert_tokens(
|
2017-12-23 22:13:08 -06:00
|
|
|
&DefaultTyParam::<i32> {
|
|
|
|
phantom: PhantomData,
|
|
|
|
},
|
2016-06-29 01:50:19 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "DefaultTyParam",
|
|
|
|
len: 1,
|
|
|
|
},
|
2016-04-14 01:51:04 -05:00
|
|
|
Token::Str("phantom"),
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::UnitStruct {
|
|
|
|
name: "PhantomData",
|
|
|
|
},
|
2016-04-14 01:51:04 -05:00
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2016-04-14 01:51:04 -05:00
|
|
|
);
|
|
|
|
}
|
2016-09-26 23:09:13 -05:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_enum_state_field() {
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
enum SomeEnum {
|
|
|
|
Key { key: char, state: bool },
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_tokens(
|
2017-04-13 14:28:23 -05:00
|
|
|
&SomeEnum::Key {
|
2017-12-23 22:13:08 -06:00
|
|
|
key: 'a',
|
|
|
|
state: true,
|
|
|
|
},
|
2016-09-26 23:09:13 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::StructVariant {
|
|
|
|
name: "SomeEnum",
|
|
|
|
variant: "Key",
|
|
|
|
len: 2,
|
|
|
|
},
|
2016-09-26 23:09:13 -05:00
|
|
|
Token::Str("key"),
|
|
|
|
Token::Char('a'),
|
|
|
|
Token::Str("state"),
|
|
|
|
Token::Bool(true),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::StructVariantEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2016-09-26 23:09:13 -05:00
|
|
|
);
|
|
|
|
}
|
2017-02-02 14:10:07 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_untagged_enum() {
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum Untagged {
|
2017-04-13 14:28:23 -05:00
|
|
|
A { a: u8 },
|
|
|
|
B { b: u8 },
|
2017-02-02 14:10:07 -06:00
|
|
|
C,
|
|
|
|
D(u8),
|
|
|
|
E(String),
|
|
|
|
F(u8, u8),
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Untagged::A { a: 1 },
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "Untagged",
|
|
|
|
len: 1,
|
|
|
|
},
|
2017-02-02 14:10:07 -06:00
|
|
|
Token::Str("a"),
|
|
|
|
Token::U8(1),
|
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-02 14:10:07 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Untagged::B { b: 2 },
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "Untagged",
|
|
|
|
len: 1,
|
|
|
|
},
|
2017-02-02 14:10:07 -06:00
|
|
|
Token::Str("b"),
|
|
|
|
Token::U8(2),
|
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-02 14:10:07 -06:00
|
|
|
);
|
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
assert_tokens(&Untagged::C, &[Token::Unit]);
|
2017-02-02 14:10:07 -06:00
|
|
|
|
2017-04-13 14:28:23 -05:00
|
|
|
assert_tokens(&Untagged::D(4), &[Token::U8(4)]);
|
|
|
|
assert_tokens(&Untagged::E("e".to_owned()), &[Token::Str("e")]);
|
2017-02-02 14:10:07 -06:00
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Untagged::F(1, 2),
|
2017-12-23 22:13:08 -06:00
|
|
|
&[
|
|
|
|
Token::Tuple { len: 2 },
|
|
|
|
Token::U8(1),
|
|
|
|
Token::U8(2),
|
|
|
|
Token::TupleEnd,
|
|
|
|
],
|
2017-02-02 14:10:07 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens_error::<Untagged>(
|
2017-04-13 14:28:23 -05:00
|
|
|
&[Token::None],
|
2017-04-19 13:38:57 -05:00
|
|
|
"data did not match any variant of untagged enum Untagged",
|
2017-02-02 14:10:07 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens_error::<Untagged>(
|
2017-04-19 15:06:31 -05:00
|
|
|
&[Token::Tuple { len: 1 }, Token::U8(1), Token::TupleEnd],
|
2017-04-19 13:38:57 -05:00
|
|
|
"data did not match any variant of untagged enum Untagged",
|
2017-02-02 14:10:07 -06:00
|
|
|
);
|
2017-02-03 00:12:07 -06:00
|
|
|
|
|
|
|
assert_de_tokens_error::<Untagged>(
|
|
|
|
&[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Tuple { len: 3 },
|
2017-02-03 00:12:07 -06:00
|
|
|
Token::U8(1),
|
|
|
|
Token::U8(2),
|
|
|
|
Token::U8(3),
|
|
|
|
Token::TupleEnd,
|
|
|
|
],
|
2017-04-19 13:38:57 -05:00
|
|
|
"data did not match any variant of untagged enum Untagged",
|
2017-02-03 00:12:07 -06:00
|
|
|
);
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_internally_tagged_enum() {
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
struct Newtype(BTreeMap<String, String>);
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
struct Struct {
|
|
|
|
f: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(tag = "type")]
|
|
|
|
enum InternallyTagged {
|
2017-04-13 14:28:23 -05:00
|
|
|
A { a: u8 },
|
2017-09-17 15:45:07 -05:00
|
|
|
B,
|
|
|
|
C(BTreeMap<String, String>),
|
|
|
|
D(Newtype),
|
|
|
|
E(Struct),
|
2017-02-02 14:10:07 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&InternallyTagged::A { a: 1 },
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "InternallyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-02 14:10:07 -06:00
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("A"),
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::U8(1),
|
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-02 14:10:07 -06:00
|
|
|
);
|
|
|
|
|
2017-09-17 15:45:07 -05:00
|
|
|
assert_de_tokens(
|
|
|
|
&InternallyTagged::A { a: 1 },
|
2017-02-02 14:10:07 -06:00
|
|
|
&[
|
2017-09-17 15:45:07 -05:00
|
|
|
Token::Seq { len: Some(2) },
|
|
|
|
Token::Str("A"),
|
|
|
|
Token::U8(1),
|
|
|
|
Token::SeqEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-02 14:10:07 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
2017-09-17 15:45:07 -05:00
|
|
|
&InternallyTagged::B,
|
2017-02-02 14:10:07 -06:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "InternallyTagged",
|
|
|
|
len: 1,
|
|
|
|
},
|
2017-02-02 14:10:07 -06:00
|
|
|
Token::Str("type"),
|
2017-09-17 15:45:07 -05:00
|
|
|
Token::Str("B"),
|
2017-02-02 14:10:07 -06:00
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-02 14:10:07 -06:00
|
|
|
);
|
|
|
|
|
2017-09-17 15:45:07 -05:00
|
|
|
assert_de_tokens(
|
|
|
|
&InternallyTagged::B,
|
2017-12-23 22:13:08 -06:00
|
|
|
&[Token::Seq { len: Some(1) }, Token::Str("B"), Token::SeqEnd],
|
2017-09-17 15:45:07 -05:00
|
|
|
);
|
|
|
|
|
2017-02-02 14:10:07 -06:00
|
|
|
assert_tokens(
|
2017-09-17 15:45:07 -05:00
|
|
|
&InternallyTagged::C(BTreeMap::new()),
|
2017-02-02 14:10:07 -06:00
|
|
|
&[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(1) },
|
2017-02-02 14:10:07 -06:00
|
|
|
Token::Str("type"),
|
2017-09-17 15:45:07 -05:00
|
|
|
Token::Str("C"),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
);
|
2017-02-02 14:10:07 -06:00
|
|
|
|
2017-09-17 15:45:07 -05:00
|
|
|
assert_de_tokens_error::<InternallyTagged>(
|
|
|
|
&[
|
|
|
|
Token::Seq { len: Some(2) },
|
|
|
|
Token::Str("C"),
|
|
|
|
Token::Map { len: Some(0) },
|
2017-02-02 14:10:07 -06:00
|
|
|
Token::MapEnd,
|
2017-09-17 15:45:07 -05:00
|
|
|
Token::SeqEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-09-17 15:45:07 -05:00
|
|
|
"invalid type: sequence, expected a map",
|
2017-02-02 14:10:07 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
2017-09-17 15:45:07 -05:00
|
|
|
&InternallyTagged::D(Newtype(BTreeMap::new())),
|
2017-02-02 14:10:07 -06:00
|
|
|
&[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(1) },
|
2017-02-02 14:10:07 -06:00
|
|
|
Token::Str("type"),
|
2017-09-17 15:45:07 -05:00
|
|
|
Token::Str("D"),
|
2017-02-02 14:10:07 -06:00
|
|
|
Token::MapEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-02 14:10:07 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
2017-09-17 15:45:07 -05:00
|
|
|
&InternallyTagged::E(Struct { f: 6 }),
|
2017-02-02 14:10:07 -06:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "Struct",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-02 14:10:07 -06:00
|
|
|
Token::Str("type"),
|
2017-09-17 15:45:07 -05:00
|
|
|
Token::Str("E"),
|
2017-02-02 14:10:07 -06:00
|
|
|
Token::Str("f"),
|
|
|
|
Token::U8(6),
|
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-02 14:10:07 -06:00
|
|
|
);
|
|
|
|
|
2017-09-17 15:45:07 -05:00
|
|
|
assert_de_tokens(
|
|
|
|
&InternallyTagged::E(Struct { f: 6 }),
|
|
|
|
&[
|
|
|
|
Token::Seq { len: Some(2) },
|
|
|
|
Token::Str("E"),
|
|
|
|
Token::U8(6),
|
|
|
|
Token::SeqEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2017-02-02 14:10:07 -06:00
|
|
|
assert_de_tokens_error::<InternallyTagged>(
|
2017-04-19 15:06:31 -05:00
|
|
|
&[Token::Map { len: Some(0) }, Token::MapEnd],
|
2017-04-19 13:38:57 -05:00
|
|
|
"missing field `type`",
|
2017-02-02 14:10:07 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens_error::<InternallyTagged>(
|
|
|
|
&[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(1) },
|
2017-02-02 14:10:07 -06:00
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("Z"),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
2017-09-17 15:45:07 -05:00
|
|
|
"unknown variant `Z`, expected one of `A`, `B`, `C`, `D`, `E`",
|
2017-02-02 14:10:07 -06:00
|
|
|
);
|
|
|
|
}
|
2017-02-17 23:44:15 -06:00
|
|
|
|
2018-04-19 11:52:27 -05:00
|
|
|
#[test]
|
|
|
|
fn test_internally_tagged_bytes() {
|
|
|
|
#[derive(Debug, PartialEq, Deserialize)]
|
|
|
|
#[serde(tag = "type")]
|
|
|
|
enum InternallyTagged {
|
|
|
|
String {
|
|
|
|
string: String,
|
|
|
|
},
|
|
|
|
Bytes {
|
|
|
|
#[serde(with = "bytes")]
|
|
|
|
bytes: Vec<u8>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_de_tokens(
|
2018-04-30 03:41:22 -05:00
|
|
|
&InternallyTagged::String {
|
|
|
|
string: "\0".to_owned(),
|
|
|
|
},
|
2018-04-19 11:52:27 -05:00
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "String",
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("String"),
|
|
|
|
Token::Str("string"),
|
|
|
|
Token::Str("\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
2018-04-30 03:41:22 -05:00
|
|
|
&InternallyTagged::String {
|
|
|
|
string: "\0".to_owned(),
|
|
|
|
},
|
2018-04-19 11:52:27 -05:00
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "String",
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("String"),
|
|
|
|
Token::Str("string"),
|
|
|
|
Token::String("\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
2018-04-30 03:41:22 -05:00
|
|
|
&InternallyTagged::String {
|
|
|
|
string: "\0".to_owned(),
|
|
|
|
},
|
2018-04-19 11:52:27 -05:00
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "String",
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("String"),
|
|
|
|
Token::Str("string"),
|
|
|
|
Token::Bytes(b"\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
2018-04-30 03:41:22 -05:00
|
|
|
&InternallyTagged::String {
|
|
|
|
string: "\0".to_owned(),
|
|
|
|
},
|
2018-04-19 11:52:27 -05:00
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "String",
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("String"),
|
|
|
|
Token::Str("string"),
|
|
|
|
Token::ByteBuf(b"\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&InternallyTagged::Bytes { bytes: vec![0] },
|
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Bytes",
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("Bytes"),
|
|
|
|
Token::Str("bytes"),
|
|
|
|
Token::Str("\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&InternallyTagged::Bytes { bytes: vec![0] },
|
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Bytes",
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("Bytes"),
|
|
|
|
Token::Str("bytes"),
|
|
|
|
Token::String("\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&InternallyTagged::Bytes { bytes: vec![0] },
|
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Bytes",
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("Bytes"),
|
|
|
|
Token::Str("bytes"),
|
|
|
|
Token::Bytes(b"\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&InternallyTagged::Bytes { bytes: vec![0] },
|
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Bytes",
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("Bytes"),
|
|
|
|
Token::Str("bytes"),
|
|
|
|
Token::ByteBuf(b"\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&InternallyTagged::Bytes { bytes: vec![0] },
|
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Bytes",
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("Bytes"),
|
|
|
|
Token::Str("bytes"),
|
|
|
|
Token::Seq { len: Some(1) },
|
|
|
|
Token::U8(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-14 14:21:00 -05:00
|
|
|
#[test]
|
|
|
|
fn test_internally_tagged_struct_variant_containing_unit_variant() {
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
pub enum Level {
|
|
|
|
Info,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(tag = "action")]
|
|
|
|
pub enum Message {
|
|
|
|
Log { level: Level },
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&Message::Log { level: Level::Info },
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "Message",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-05-14 14:21:00 -05:00
|
|
|
Token::Str("action"),
|
|
|
|
Token::Str("Log"),
|
|
|
|
Token::Str("level"),
|
|
|
|
Token::BorrowedStr("Info"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-05-10 21:56:05 -05:00
|
|
|
#[test]
|
|
|
|
fn test_internally_tagged_borrow() {
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(tag = "type")]
|
|
|
|
pub enum Input<'a> {
|
|
|
|
Package { name: &'a str },
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Input::Package { name: "borrowed" },
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "Input",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-05-10 21:56:05 -05:00
|
|
|
Token::BorrowedStr("type"),
|
|
|
|
Token::BorrowedStr("Package"),
|
|
|
|
Token::BorrowedStr("name"),
|
|
|
|
Token::BorrowedStr("borrowed"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-17 23:44:15 -06:00
|
|
|
#[test]
|
|
|
|
fn test_adjacently_tagged_enum() {
|
2017-02-19 18:04:50 -06:00
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2017-02-19 18:04:39 -06:00
|
|
|
#[serde(tag = "t", content = "c")]
|
2017-02-19 18:04:50 -06:00
|
|
|
enum AdjacentlyTagged<T> {
|
2017-02-19 18:04:39 -06:00
|
|
|
Unit,
|
2017-02-19 18:04:50 -06:00
|
|
|
Newtype(T),
|
2017-02-19 18:04:39 -06:00
|
|
|
Tuple(u8, u8),
|
|
|
|
Struct { f: u8 },
|
2017-02-17 23:44:15 -06:00
|
|
|
}
|
|
|
|
|
2017-02-19 18:04:50 -06:00
|
|
|
// unit with no content
|
2017-11-12 11:52:39 -06:00
|
|
|
assert_ser_tokens(
|
2017-02-19 18:04:50 -06:00
|
|
|
&AdjacentlyTagged::Unit::<u8>,
|
2017-02-17 23:44:15 -06:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 1,
|
|
|
|
},
|
2017-02-19 18:04:39 -06:00
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Unit"),
|
2017-02-17 23:44:15 -06:00
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-17 23:44:15 -06:00
|
|
|
);
|
|
|
|
|
2017-11-12 11:52:39 -06:00
|
|
|
// unit with no content
|
|
|
|
assert_de_tokens(
|
|
|
|
&AdjacentlyTagged::Unit::<u8>,
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-11-12 11:52:39 -06:00
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Unit"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2017-02-19 18:04:50 -06:00
|
|
|
// unit with tag first
|
|
|
|
assert_de_tokens(
|
|
|
|
&AdjacentlyTagged::Unit::<u8>,
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-19 18:04:50 -06:00
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Unit"),
|
|
|
|
Token::Str("c"),
|
|
|
|
Token::Unit,
|
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-19 18:04:50 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
// unit with content first
|
|
|
|
assert_de_tokens(
|
|
|
|
&AdjacentlyTagged::Unit::<u8>,
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-19 18:04:50 -06:00
|
|
|
Token::Str("c"),
|
|
|
|
Token::Unit,
|
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Unit"),
|
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-19 18:04:50 -06:00
|
|
|
);
|
|
|
|
|
2017-04-27 13:24:09 -05:00
|
|
|
// unit with excess content (f, g, h)
|
|
|
|
assert_de_tokens(
|
|
|
|
&AdjacentlyTagged::Unit::<u8>,
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-04-27 13:24:09 -05:00
|
|
|
Token::Str("f"),
|
|
|
|
Token::Unit,
|
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Unit"),
|
|
|
|
Token::Str("g"),
|
|
|
|
Token::Unit,
|
|
|
|
Token::Str("c"),
|
|
|
|
Token::Unit,
|
|
|
|
Token::Str("h"),
|
|
|
|
Token::Unit,
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
2017-02-19 18:04:50 -06:00
|
|
|
// newtype with tag first
|
|
|
|
assert_tokens(
|
|
|
|
&AdjacentlyTagged::Newtype::<u8>(1),
|
2017-02-17 23:44:15 -06:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-19 18:04:39 -06:00
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Newtype"),
|
|
|
|
Token::Str("c"),
|
2017-02-17 23:44:15 -06:00
|
|
|
Token::U8(1),
|
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-17 23:44:15 -06:00
|
|
|
);
|
|
|
|
|
2017-02-19 18:04:50 -06:00
|
|
|
// newtype with content first
|
|
|
|
assert_de_tokens(
|
|
|
|
&AdjacentlyTagged::Newtype::<u8>(1),
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-19 18:04:50 -06:00
|
|
|
Token::Str("c"),
|
|
|
|
Token::U8(1),
|
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Newtype"),
|
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-19 18:04:50 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
// tuple with tag first
|
|
|
|
assert_tokens(
|
|
|
|
&AdjacentlyTagged::Tuple::<u8>(1, 1),
|
2017-02-17 23:44:15 -06:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-19 18:04:39 -06:00
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Tuple"),
|
|
|
|
Token::Str("c"),
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Tuple { len: 2 },
|
2017-02-19 18:04:39 -06:00
|
|
|
Token::U8(1),
|
|
|
|
Token::U8(1),
|
|
|
|
Token::TupleEnd,
|
2017-02-17 23:44:15 -06:00
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-17 23:44:15 -06:00
|
|
|
);
|
|
|
|
|
2017-02-19 18:04:50 -06:00
|
|
|
// tuple with content first
|
|
|
|
assert_de_tokens(
|
|
|
|
&AdjacentlyTagged::Tuple::<u8>(1, 1),
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-19 18:04:50 -06:00
|
|
|
Token::Str("c"),
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Tuple { len: 2 },
|
2017-02-19 18:04:50 -06:00
|
|
|
Token::U8(1),
|
|
|
|
Token::U8(1),
|
|
|
|
Token::TupleEnd,
|
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Tuple"),
|
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-19 18:04:50 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
// struct with tag first
|
|
|
|
assert_tokens(
|
|
|
|
&AdjacentlyTagged::Struct::<u8> { f: 1 },
|
2017-02-17 23:44:15 -06:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-19 18:04:39 -06:00
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Struct"),
|
|
|
|
Token::Str("c"),
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "Struct",
|
|
|
|
len: 1,
|
|
|
|
},
|
2017-02-17 23:44:15 -06:00
|
|
|
Token::Str("f"),
|
2017-02-19 18:04:39 -06:00
|
|
|
Token::U8(1),
|
2017-02-17 23:44:15 -06:00
|
|
|
Token::StructEnd,
|
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-17 23:44:15 -06:00
|
|
|
);
|
2017-02-19 18:04:50 -06:00
|
|
|
|
|
|
|
// struct with content first
|
|
|
|
assert_de_tokens(
|
|
|
|
&AdjacentlyTagged::Struct::<u8> { f: 1 },
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-19 18:04:50 -06:00
|
|
|
Token::Str("c"),
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "Struct",
|
|
|
|
len: 1,
|
|
|
|
},
|
2017-02-19 18:04:50 -06:00
|
|
|
Token::Str("f"),
|
|
|
|
Token::U8(1),
|
|
|
|
Token::StructEnd,
|
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Struct"),
|
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-19 18:04:50 -06:00
|
|
|
);
|
2017-02-17 23:44:15 -06:00
|
|
|
}
|
2017-02-24 15:48:03 -06:00
|
|
|
|
2017-04-27 14:21:32 -05:00
|
|
|
#[test]
|
|
|
|
fn test_adjacently_tagged_enum_deny_unknown_fields() {
|
|
|
|
#[derive(Debug, PartialEq, Deserialize)]
|
|
|
|
#[serde(tag = "t", content = "c", deny_unknown_fields)]
|
|
|
|
enum AdjacentlyTagged {
|
|
|
|
Unit,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&AdjacentlyTagged::Unit,
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-04-27 14:21:32 -05:00
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Unit"),
|
|
|
|
Token::Str("c"),
|
|
|
|
Token::Unit,
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens_error::<AdjacentlyTagged>(
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-04-27 14:21:32 -05:00
|
|
|
Token::Str("t"),
|
|
|
|
Token::Str("Unit"),
|
|
|
|
Token::Str("c"),
|
|
|
|
Token::Unit,
|
|
|
|
Token::Str("h"),
|
|
|
|
],
|
2017-12-23 22:13:08 -06:00
|
|
|
r#"invalid value: string "h", expected "t" or "c""#,
|
2017-04-27 14:21:32 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens_error::<AdjacentlyTagged>(
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-04-27 14:21:32 -05:00
|
|
|
Token::Str("h"),
|
|
|
|
],
|
2017-12-23 22:13:08 -06:00
|
|
|
r#"invalid value: string "h", expected "t" or "c""#,
|
2017-04-27 14:21:32 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens_error::<AdjacentlyTagged>(
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "AdjacentlyTagged",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-04-27 14:21:32 -05:00
|
|
|
Token::Str("c"),
|
|
|
|
Token::Unit,
|
|
|
|
Token::Str("h"),
|
|
|
|
],
|
2017-12-23 22:13:08 -06:00
|
|
|
r#"invalid value: string "h", expected "t" or "c""#,
|
2017-04-27 14:21:32 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-24 15:48:03 -06:00
|
|
|
#[test]
|
|
|
|
fn test_enum_in_internally_tagged_enum() {
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(tag = "type")]
|
|
|
|
enum Outer {
|
|
|
|
Inner(Inner),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
enum Inner {
|
|
|
|
Unit,
|
|
|
|
Newtype(u8),
|
|
|
|
Tuple(u8, u8),
|
|
|
|
Struct { f: u8 },
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Outer::Inner(Inner::Unit),
|
|
|
|
&[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(2) },
|
2017-02-24 15:48:03 -06:00
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("Inner"),
|
|
|
|
Token::Str("Unit"),
|
|
|
|
Token::Unit,
|
|
|
|
Token::MapEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-24 15:48:03 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Outer::Inner(Inner::Newtype(1)),
|
|
|
|
&[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(2) },
|
2017-02-24 15:48:03 -06:00
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("Inner"),
|
|
|
|
Token::Str("Newtype"),
|
|
|
|
Token::U8(1),
|
|
|
|
Token::MapEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-24 15:48:03 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Outer::Inner(Inner::Tuple(1, 1)),
|
|
|
|
&[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(2) },
|
2017-02-24 15:48:03 -06:00
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("Inner"),
|
|
|
|
Token::Str("Tuple"),
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::TupleStruct {
|
|
|
|
name: "Tuple",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-24 15:48:03 -06:00
|
|
|
Token::U8(1),
|
|
|
|
Token::U8(1),
|
|
|
|
Token::TupleStructEnd,
|
|
|
|
Token::MapEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-24 15:48:03 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Outer::Inner(Inner::Struct { f: 1 }),
|
|
|
|
&[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Map { len: Some(2) },
|
2017-02-24 15:48:03 -06:00
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("Inner"),
|
|
|
|
Token::Str("Struct"),
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "Struct",
|
|
|
|
len: 1,
|
|
|
|
},
|
2017-02-24 15:48:03 -06:00
|
|
|
Token::Str("f"),
|
|
|
|
Token::U8(1),
|
|
|
|
Token::StructEnd,
|
|
|
|
Token::MapEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-24 15:48:03 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-27 10:57:29 -06:00
|
|
|
#[test]
|
|
|
|
fn test_internally_tagged_struct() {
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
2018-12-27 18:49:08 -06:00
|
|
|
#[serde(tag = "type")]
|
2018-12-27 10:57:29 -06:00
|
|
|
pub struct Struct {
|
|
|
|
a: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_tokens(
|
2018-12-27 18:49:08 -06:00
|
|
|
&Struct { a: 1 },
|
2018-12-27 10:57:29 -06:00
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Struct",
|
|
|
|
len: 2,
|
|
|
|
},
|
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("Struct"),
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::U8(1),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&Struct { a: 1 },
|
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Struct",
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Token::Str("a"),
|
|
|
|
Token::U8(1),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-12-27 19:48:47 -06:00
|
|
|
#[test]
|
|
|
|
fn test_internally_tagged_braced_struct_with_zero_fields() {
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(tag = "type")]
|
|
|
|
struct S {}
|
|
|
|
|
|
|
|
assert_tokens(
|
2018-12-28 11:19:32 -06:00
|
|
|
&S {},
|
2018-12-27 19:48:47 -06:00
|
|
|
&[
|
2018-12-28 11:19:32 -06:00
|
|
|
Token::Struct { name: "S", len: 1 },
|
2018-12-27 19:48:47 -06:00
|
|
|
Token::Str("type"),
|
|
|
|
Token::Str("S"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-24 15:48:03 -06:00
|
|
|
#[test]
|
|
|
|
fn test_enum_in_untagged_enum() {
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum Outer {
|
|
|
|
Inner(Inner),
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
enum Inner {
|
|
|
|
Unit,
|
|
|
|
Newtype(u8),
|
|
|
|
Tuple(u8, u8),
|
|
|
|
Struct { f: u8 },
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Outer::Inner(Inner::Unit),
|
2018-04-30 03:41:22 -05:00
|
|
|
&[Token::UnitVariant {
|
|
|
|
name: "Inner",
|
|
|
|
variant: "Unit",
|
|
|
|
}],
|
2017-02-24 15:48:03 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Outer::Inner(Inner::Newtype(1)),
|
2017-12-23 22:13:08 -06:00
|
|
|
&[
|
|
|
|
Token::NewtypeVariant {
|
|
|
|
name: "Inner",
|
|
|
|
variant: "Newtype",
|
|
|
|
},
|
|
|
|
Token::U8(1),
|
|
|
|
],
|
2017-02-24 15:48:03 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Outer::Inner(Inner::Tuple(1, 1)),
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::TupleVariant {
|
|
|
|
name: "Inner",
|
|
|
|
variant: "Tuple",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-24 15:48:03 -06:00
|
|
|
Token::U8(1),
|
|
|
|
Token::U8(1),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::TupleVariantEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-24 15:48:03 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Outer::Inner(Inner::Struct { f: 1 }),
|
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::StructVariant {
|
|
|
|
name: "Inner",
|
|
|
|
variant: "Struct",
|
|
|
|
len: 1,
|
|
|
|
},
|
2017-02-24 15:48:03 -06:00
|
|
|
Token::Str("f"),
|
|
|
|
Token::U8(1),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::StructVariantEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-24 15:48:03 -06:00
|
|
|
);
|
|
|
|
}
|
2017-02-25 13:58:34 -06:00
|
|
|
|
2018-04-19 11:52:27 -05:00
|
|
|
#[test]
|
|
|
|
fn test_untagged_bytes() {
|
|
|
|
#[derive(Debug, PartialEq, Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum Untagged {
|
|
|
|
String {
|
|
|
|
string: String,
|
|
|
|
},
|
|
|
|
Bytes {
|
|
|
|
#[serde(with = "bytes")]
|
|
|
|
bytes: Vec<u8>,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_de_tokens(
|
2018-04-30 03:41:22 -05:00
|
|
|
&Untagged::String {
|
|
|
|
string: "\0".to_owned(),
|
|
|
|
},
|
2018-04-19 11:52:27 -05:00
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Untagged",
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Token::Str("string"),
|
|
|
|
Token::Str("\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
2018-04-30 03:41:22 -05:00
|
|
|
&Untagged::String {
|
|
|
|
string: "\0".to_owned(),
|
|
|
|
},
|
2018-04-19 11:52:27 -05:00
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Untagged",
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Token::Str("string"),
|
|
|
|
Token::String("\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
2018-04-30 03:41:22 -05:00
|
|
|
&Untagged::String {
|
|
|
|
string: "\0".to_owned(),
|
|
|
|
},
|
2018-04-19 11:52:27 -05:00
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Untagged",
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Token::Str("string"),
|
|
|
|
Token::Bytes(b"\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
2018-04-30 03:41:22 -05:00
|
|
|
&Untagged::String {
|
|
|
|
string: "\0".to_owned(),
|
|
|
|
},
|
2018-04-19 11:52:27 -05:00
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Untagged",
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Token::Str("string"),
|
|
|
|
Token::ByteBuf(b"\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&Untagged::Bytes { bytes: vec![0] },
|
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Untagged",
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Token::Str("bytes"),
|
|
|
|
Token::Str("\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&Untagged::Bytes { bytes: vec![0] },
|
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Untagged",
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Token::Str("bytes"),
|
|
|
|
Token::String("\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&Untagged::Bytes { bytes: vec![0] },
|
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Untagged",
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Token::Str("bytes"),
|
|
|
|
Token::Bytes(b"\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&Untagged::Bytes { bytes: vec![0] },
|
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Untagged",
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Token::Str("bytes"),
|
|
|
|
Token::ByteBuf(b"\0"),
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_de_tokens(
|
|
|
|
&Untagged::Bytes { bytes: vec![0] },
|
|
|
|
&[
|
|
|
|
Token::Struct {
|
|
|
|
name: "Untagged",
|
|
|
|
len: 1,
|
|
|
|
},
|
|
|
|
Token::Str("bytes"),
|
|
|
|
Token::Seq { len: Some(1) },
|
|
|
|
Token::U8(0),
|
|
|
|
Token::SeqEnd,
|
|
|
|
Token::StructEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-25 13:58:34 -06:00
|
|
|
#[test]
|
|
|
|
fn test_rename_all() {
|
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
|
|
|
#[serde(rename_all = "snake_case")]
|
|
|
|
enum E {
|
|
|
|
#[serde(rename_all = "camelCase")]
|
|
|
|
Serialize {
|
|
|
|
serialize: bool,
|
|
|
|
serialize_seq: bool,
|
|
|
|
},
|
|
|
|
#[serde(rename_all = "kebab-case")]
|
|
|
|
SerializeSeq {
|
|
|
|
serialize: bool,
|
|
|
|
serialize_seq: bool,
|
|
|
|
},
|
|
|
|
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
|
|
|
|
SerializeMap {
|
|
|
|
serialize: bool,
|
|
|
|
serialize_seq: bool,
|
2017-12-23 22:13:08 -06:00
|
|
|
},
|
2017-02-25 13:58:34 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
|
|
|
#[serde(rename_all = "PascalCase")]
|
|
|
|
struct S {
|
|
|
|
serialize: bool,
|
|
|
|
serialize_seq: bool,
|
|
|
|
}
|
|
|
|
|
2017-09-05 13:23:38 -05:00
|
|
|
#[derive(Serialize, Deserialize, Debug, PartialEq)]
|
|
|
|
#[serde(rename_all = "SCREAMING-KEBAB-CASE")]
|
|
|
|
struct ScreamingKebab {
|
|
|
|
serialize: bool,
|
|
|
|
serialize_seq: bool,
|
|
|
|
}
|
|
|
|
|
2017-02-25 13:58:34 -06:00
|
|
|
assert_tokens(
|
2017-04-13 14:28:23 -05:00
|
|
|
&E::Serialize {
|
2017-12-23 22:13:08 -06:00
|
|
|
serialize: true,
|
|
|
|
serialize_seq: true,
|
|
|
|
},
|
2017-02-25 13:58:34 -06:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::StructVariant {
|
|
|
|
name: "E",
|
|
|
|
variant: "serialize",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-25 13:58:34 -06:00
|
|
|
Token::Str("serialize"),
|
|
|
|
Token::Bool(true),
|
|
|
|
Token::Str("serializeSeq"),
|
|
|
|
Token::Bool(true),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::StructVariantEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-25 13:58:34 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
2017-04-13 14:28:23 -05:00
|
|
|
&E::SerializeSeq {
|
2017-12-23 22:13:08 -06:00
|
|
|
serialize: true,
|
|
|
|
serialize_seq: true,
|
|
|
|
},
|
2017-02-25 13:58:34 -06:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::StructVariant {
|
|
|
|
name: "E",
|
|
|
|
variant: "serialize_seq",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-25 13:58:34 -06:00
|
|
|
Token::Str("serialize"),
|
|
|
|
Token::Bool(true),
|
|
|
|
Token::Str("serialize-seq"),
|
|
|
|
Token::Bool(true),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::StructVariantEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-25 13:58:34 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
2017-04-13 14:28:23 -05:00
|
|
|
&E::SerializeMap {
|
2017-12-23 22:13:08 -06:00
|
|
|
serialize: true,
|
|
|
|
serialize_seq: true,
|
|
|
|
},
|
2017-02-25 13:58:34 -06:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::StructVariant {
|
|
|
|
name: "E",
|
|
|
|
variant: "serialize_map",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-02-25 13:58:34 -06:00
|
|
|
Token::Str("SERIALIZE"),
|
|
|
|
Token::Bool(true),
|
|
|
|
Token::Str("SERIALIZE_SEQ"),
|
|
|
|
Token::Bool(true),
|
2017-04-05 12:40:14 -05:00
|
|
|
Token::StructVariantEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-25 13:58:34 -06:00
|
|
|
);
|
|
|
|
|
|
|
|
assert_tokens(
|
2017-04-13 14:28:23 -05:00
|
|
|
&S {
|
2017-12-23 22:13:08 -06:00
|
|
|
serialize: true,
|
|
|
|
serialize_seq: true,
|
|
|
|
},
|
2017-02-25 13:58:34 -06:00
|
|
|
&[
|
2017-04-19 15:06:31 -05:00
|
|
|
Token::Struct { name: "S", len: 2 },
|
2017-02-25 13:58:34 -06:00
|
|
|
Token::Str("Serialize"),
|
|
|
|
Token::Bool(true),
|
|
|
|
Token::Str("SerializeSeq"),
|
|
|
|
Token::Bool(true),
|
|
|
|
Token::StructEnd,
|
2017-04-13 14:28:23 -05:00
|
|
|
],
|
2017-02-25 13:58:34 -06:00
|
|
|
);
|
2017-09-05 13:23:38 -05:00
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&ScreamingKebab {
|
2017-12-23 22:13:08 -06:00
|
|
|
serialize: true,
|
|
|
|
serialize_seq: true,
|
|
|
|
},
|
2017-09-05 13:23:38 -05:00
|
|
|
&[
|
2017-12-23 22:13:08 -06:00
|
|
|
Token::Struct {
|
|
|
|
name: "ScreamingKebab",
|
|
|
|
len: 2,
|
|
|
|
},
|
2017-09-05 13:23:38 -05:00
|
|
|
Token::Str("SERIALIZE"),
|
|
|
|
Token::Bool(true),
|
|
|
|
Token::Str("SERIALIZE-SEQ"),
|
|
|
|
Token::Bool(true),
|
|
|
|
Token::StructEnd,
|
2017-12-23 22:13:08 -06:00
|
|
|
],
|
2017-09-05 13:23:38 -05:00
|
|
|
);
|
2017-02-25 13:58:34 -06:00
|
|
|
}
|
2017-11-07 01:32:26 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_untagged_newtype_variant_containing_unit_struct_not_map() {
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
struct Unit;
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(untagged)]
|
|
|
|
enum Message {
|
|
|
|
Unit(Unit),
|
|
|
|
Map(BTreeMap<String, String>),
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Message::Map(BTreeMap::new()),
|
2017-12-23 22:13:08 -06:00
|
|
|
&[Token::Map { len: Some(0) }, Token::MapEnd],
|
2017-11-07 01:32:26 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_internally_tagged_newtype_variant_containing_unit_struct() {
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
struct Info;
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Serialize, Deserialize)]
|
|
|
|
#[serde(tag = "topic")]
|
|
|
|
enum Message {
|
|
|
|
Info(Info),
|
|
|
|
}
|
|
|
|
|
|
|
|
assert_tokens(
|
|
|
|
&Message::Info(Info),
|
|
|
|
&[
|
|
|
|
Token::Map { len: Some(1) },
|
|
|
|
Token::Str("topic"),
|
|
|
|
Token::Str("Info"),
|
|
|
|
Token::MapEnd,
|
|
|
|
],
|
|
|
|
);
|
|
|
|
}
|