mirror of
https://github.com/pezkuwichain/serde.git
synced 2026-04-27 23:57:56 +00:00
Examples of every token
This commit is contained in:
+64
-28
@@ -24,7 +24,7 @@ impl<'a> Serializer<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Pulls the next token off of the serializer, ignoring it.
|
/// Pulls the next token off of the serializer, ignoring it.
|
||||||
pub fn next_token(&mut self) -> Option<Token> {
|
fn next_token(&mut self) -> Option<Token> {
|
||||||
if let Some((&first, rest)) = self.tokens.split_first() {
|
if let Some((&first, rest)) = self.tokens.split_first() {
|
||||||
self.tokens = rest;
|
self.tokens = rest;
|
||||||
Some(first)
|
Some(first)
|
||||||
@@ -39,27 +39,26 @@ impl<'a> Serializer<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! assert_next_token {
|
macro_rules! assert_next_token {
|
||||||
($ser:ident, $expected:ident) => {
|
($ser:expr, $expected:ident) => {
|
||||||
assert_next_token!($ser, Token::$expected, true);
|
assert_next_token!($ser, $expected, Token::$expected, true);
|
||||||
};
|
};
|
||||||
($ser:ident, $expected:ident($v:expr)) => {
|
($ser:expr, $expected:ident($v:expr)) => {
|
||||||
assert_next_token!($ser, Token::$expected(v), v == $v);
|
assert_next_token!($ser, $expected, Token::$expected(v), v == $v);
|
||||||
};
|
};
|
||||||
($ser:ident, $expected:ident { $($k:ident),* }) => {
|
($ser:expr, $expected:ident { $($k:ident),* }) => {
|
||||||
let compare = ($($k,)*);
|
let compare = ($($k,)*);
|
||||||
assert_next_token!($ser, Token::$expected { $($k),* }, ($($k,)*) == compare);
|
assert_next_token!($ser, $expected, Token::$expected { $($k),* }, ($($k,)*) == compare);
|
||||||
};
|
};
|
||||||
($ser:ident, $pat:pat, $guard:expr) => {
|
($ser:expr, $expected:ident, $pat:pat, $guard:expr) => {
|
||||||
match $ser.next_token() {
|
match $ser.next_token() {
|
||||||
Some($pat) if $guard => {}
|
Some($pat) if $guard => {}
|
||||||
//Some(Token::$expected $(($($n),*))*) $(if $($n == $v)&&*)* => {}
|
|
||||||
Some(other) => {
|
Some(other) => {
|
||||||
panic!("expected Token::{} but serialized as {:?}",
|
panic!("expected Token::{} but serialized as {}",
|
||||||
stringify!($pat), other);
|
stringify!($expected), other);
|
||||||
}
|
}
|
||||||
None => {
|
None => {
|
||||||
panic!("expected Token::{} after end of serialized tokens",
|
panic!("expected Token::{} after end of serialized tokens",
|
||||||
stringify!($pat));
|
stringify!($expected));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -72,10 +71,10 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
|||||||
type SerializeSeq = Self;
|
type SerializeSeq = Self;
|
||||||
type SerializeTuple = Self;
|
type SerializeTuple = Self;
|
||||||
type SerializeTupleStruct = Self;
|
type SerializeTupleStruct = Self;
|
||||||
type SerializeTupleVariant = Self;
|
type SerializeTupleVariant = Variant<'s, 'a>;
|
||||||
type SerializeMap = Self;
|
type SerializeMap = Self;
|
||||||
type SerializeStruct = Self;
|
type SerializeStruct = Self;
|
||||||
type SerializeStructVariant = Self;
|
type SerializeStructVariant = Variant<'s, 'a>;
|
||||||
|
|
||||||
fn serialize_bool(self, v: bool) -> Result<(), Error> {
|
fn serialize_bool(self, v: bool) -> Result<(), Error> {
|
||||||
assert_next_token!(self, Bool(v));
|
assert_next_token!(self, Bool(v));
|
||||||
@@ -138,12 +137,20 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_str(self, v: &str) -> Result<(), Error> {
|
fn serialize_str(self, v: &str) -> Result<(), Error> {
|
||||||
assert_next_token!(self, Str(v));
|
match self.tokens.first() {
|
||||||
|
Some(&Token::BorrowedStr(_)) => assert_next_token!(self, BorrowedStr(v)),
|
||||||
|
Some(&Token::String(_)) => assert_next_token!(self, String(v)),
|
||||||
|
_ => assert_next_token!(self, Str(v)),
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_bytes(self, value: &[u8]) -> Result<(), Self::Error> {
|
fn serialize_bytes(self, v: &[u8]) -> Result<(), Self::Error> {
|
||||||
assert_next_token!(self, Bytes(value));
|
match self.tokens.first() {
|
||||||
|
Some(&Token::BorrowedBytes(_)) => assert_next_token!(self, BorrowedBytes(v)),
|
||||||
|
Some(&Token::ByteBuf(_)) => assert_next_token!(self, ByteBuf(v)),
|
||||||
|
_ => assert_next_token!(self, Bytes(v)),
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -234,9 +241,17 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
|||||||
_variant_index: u32,
|
_variant_index: u32,
|
||||||
variant: &'static str,
|
variant: &'static str,
|
||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self, Error> {
|
) -> Result<Self::SerializeTupleVariant, Error> {
|
||||||
|
if self.tokens.first() == Some(&Token::Enum { name }) {
|
||||||
|
self.next_token();
|
||||||
|
assert_next_token!(self, Str(variant));
|
||||||
|
let len = Some(len);
|
||||||
|
assert_next_token!(self, Seq { len });
|
||||||
|
Ok(Variant { ser: self, end: Token::SeqEnd })
|
||||||
|
} else {
|
||||||
assert_next_token!(self, TupleVariant { name, variant, len });
|
assert_next_token!(self, TupleVariant { name, variant, len });
|
||||||
Ok(self)
|
Ok(Variant { ser: self, end: Token::TupleVariantEnd })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn serialize_map(self, len: Option<usize>) -> Result<Self, Error> {
|
fn serialize_map(self, len: Option<usize>) -> Result<Self, Error> {
|
||||||
@@ -255,11 +270,24 @@ impl<'s, 'a> ser::Serializer for &'s mut Serializer<'a> {
|
|||||||
_variant_index: u32,
|
_variant_index: u32,
|
||||||
variant: &'static str,
|
variant: &'static str,
|
||||||
len: usize,
|
len: usize,
|
||||||
) -> Result<Self, Error> {
|
) -> Result<Self::SerializeStructVariant, Error> {
|
||||||
|
if self.tokens.first() == Some(&Token::Enum { name }) {
|
||||||
|
self.next_token();
|
||||||
|
assert_next_token!(self, Str(variant));
|
||||||
|
let len = Some(len);
|
||||||
|
assert_next_token!(self, Map { len });
|
||||||
|
Ok(Variant { ser: self, end: Token::MapEnd })
|
||||||
|
} else {
|
||||||
assert_next_token!(self, StructVariant { name, variant, len });
|
assert_next_token!(self, StructVariant { name, variant, len });
|
||||||
Ok(self)
|
Ok(Variant { ser: self, end: Token::StructVariantEnd })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub struct Variant<'s, 'a: 's> {
|
||||||
|
ser: &'s mut Serializer<'a>,
|
||||||
|
end: Token,
|
||||||
|
}
|
||||||
|
|
||||||
impl<'s, 'a> ser::SerializeSeq for &'s mut Serializer<'a> {
|
impl<'s, 'a> ser::SerializeSeq for &'s mut Serializer<'a> {
|
||||||
type Ok = ();
|
type Ok = ();
|
||||||
@@ -312,7 +340,7 @@ impl<'s, 'a> ser::SerializeTupleStruct for &'s mut Serializer<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, 'a> ser::SerializeTupleVariant for &'s mut Serializer<'a> {
|
impl<'s, 'a> ser::SerializeTupleVariant for Variant<'s, 'a> {
|
||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
@@ -320,11 +348,15 @@ impl<'s, 'a> ser::SerializeTupleVariant for &'s mut Serializer<'a> {
|
|||||||
where
|
where
|
||||||
T: Serialize,
|
T: Serialize,
|
||||||
{
|
{
|
||||||
value.serialize(&mut **self)
|
value.serialize(&mut *self.ser)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn end(self) -> Result<(), Error> {
|
fn end(self) -> Result<(), Error> {
|
||||||
assert_next_token!(self, TupleVariantEnd);
|
match self.end {
|
||||||
|
Token::TupleVariantEnd => assert_next_token!(self.ser, TupleVariantEnd),
|
||||||
|
Token::SeqEnd => assert_next_token!(self.ser, SeqEnd),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -375,7 +407,7 @@ impl<'s, 'a> ser::SerializeStruct for &'s mut Serializer<'a> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s, 'a> ser::SerializeStructVariant for &'s mut Serializer<'a> {
|
impl<'s, 'a> ser::SerializeStructVariant for Variant<'s, 'a> {
|
||||||
type Ok = ();
|
type Ok = ();
|
||||||
type Error = Error;
|
type Error = Error;
|
||||||
|
|
||||||
@@ -387,12 +419,16 @@ impl<'s, 'a> ser::SerializeStructVariant for &'s mut Serializer<'a> {
|
|||||||
where
|
where
|
||||||
T: Serialize,
|
T: Serialize,
|
||||||
{
|
{
|
||||||
try!(key.serialize(&mut **self));
|
try!(key.serialize(&mut *self.ser));
|
||||||
value.serialize(&mut **self)
|
value.serialize(&mut *self.ser)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn end(self) -> Result<(), Self::Error> {
|
fn end(self) -> Result<(), Self::Error> {
|
||||||
assert_next_token!(self, StructVariantEnd);
|
match self.end {
|
||||||
|
Token::StructVariantEnd => assert_next_token!(self.ser, StructVariantEnd),
|
||||||
|
Token::MapEnd => assert_next_token!(self.ser, MapEnd),
|
||||||
|
_ => unreachable!(),
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+427
-25
@@ -11,48 +11,141 @@ use std::fmt::{self, Debug, Display};
|
|||||||
#[derive(Copy, Clone, PartialEq, Debug)]
|
#[derive(Copy, Clone, PartialEq, Debug)]
|
||||||
pub enum Token {
|
pub enum Token {
|
||||||
/// A serialized `bool`.
|
/// A serialized `bool`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// assert_tokens(&true, &[Token::Bool(true)]);
|
||||||
|
/// ```
|
||||||
Bool(bool),
|
Bool(bool),
|
||||||
|
|
||||||
/// A serialized `i8`.
|
/// A serialized `i8`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// assert_tokens(&0i8, &[Token::I8(0)]);
|
||||||
|
/// ```
|
||||||
I8(i8),
|
I8(i8),
|
||||||
|
|
||||||
/// A serialized `i16`.
|
/// A serialized `i16`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// assert_tokens(&0i16, &[Token::I16(0)]);
|
||||||
|
/// ```
|
||||||
I16(i16),
|
I16(i16),
|
||||||
|
|
||||||
/// A serialized `i32`.
|
/// A serialized `i32`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// assert_tokens(&0i32, &[Token::I32(0)]);
|
||||||
|
/// ```
|
||||||
I32(i32),
|
I32(i32),
|
||||||
|
|
||||||
/// A serialized `i64`.
|
/// A serialized `i64`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// assert_tokens(&0i64, &[Token::I64(0)]);
|
||||||
|
/// ```
|
||||||
I64(i64),
|
I64(i64),
|
||||||
|
|
||||||
/// A serialized `u8`.
|
/// A serialized `u8`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// assert_tokens(&0u8, &[Token::U8(0)]);
|
||||||
|
/// ```
|
||||||
U8(u8),
|
U8(u8),
|
||||||
|
|
||||||
/// A serialized `u16`.
|
/// A serialized `u16`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// assert_tokens(&0u16, &[Token::U16(0)]);
|
||||||
|
/// ```
|
||||||
U16(u16),
|
U16(u16),
|
||||||
|
|
||||||
/// A serialized `u32`.
|
/// A serialized `u32`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// assert_tokens(&0u32, &[Token::U32(0)]);
|
||||||
|
/// ```
|
||||||
U32(u32),
|
U32(u32),
|
||||||
|
|
||||||
/// A serialized `u64`.
|
/// A serialized `u64`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// assert_tokens(&0u64, &[Token::U64(0)]);
|
||||||
|
/// ```
|
||||||
U64(u64),
|
U64(u64),
|
||||||
|
|
||||||
/// A serialized `f32`.
|
/// A serialized `f32`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// assert_tokens(&0f32, &[Token::F32(0.0)]);
|
||||||
|
/// ```
|
||||||
F32(f32),
|
F32(f32),
|
||||||
|
|
||||||
/// A serialized `f64`.
|
/// A serialized `f64`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// assert_tokens(&0f64, &[Token::F64(0.0)]);
|
||||||
|
/// ```
|
||||||
F64(f64),
|
F64(f64),
|
||||||
|
|
||||||
/// A serialized `char`.
|
/// A serialized `char`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// assert_tokens(&'\n', &[Token::Char('\n')]);
|
||||||
|
/// ```
|
||||||
Char(char),
|
Char(char),
|
||||||
|
|
||||||
/// A serialized `str`.
|
/// A serialized `str`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// let s = String::from("transient");
|
||||||
|
/// assert_tokens(&s, &[Token::Str("transient")]);
|
||||||
|
/// ```
|
||||||
Str(&'static str),
|
Str(&'static str),
|
||||||
|
|
||||||
/// A borrowed `str`.
|
/// A borrowed `str`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// let s: &str = "borrowed";
|
||||||
|
/// assert_tokens(&s, &[Token::BorrowedStr("borrowed")]);
|
||||||
|
/// ```
|
||||||
BorrowedStr(&'static str),
|
BorrowedStr(&'static str),
|
||||||
|
|
||||||
/// A serialized `String`.
|
/// A serialized `String`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// let s = String::from("owned");
|
||||||
|
/// assert_tokens(&s, &[Token::String("owned")]);
|
||||||
|
/// ```
|
||||||
String(&'static str),
|
String(&'static str),
|
||||||
|
|
||||||
/// A serialized `[u8]`
|
/// A serialized `[u8]`
|
||||||
@@ -67,85 +160,394 @@ pub enum Token {
|
|||||||
/// The header to a serialized `Option<T>` containing some value.
|
/// The header to a serialized `Option<T>` containing some value.
|
||||||
///
|
///
|
||||||
/// The tokens of the value follow after this header.
|
/// The tokens of the value follow after this header.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// let opt = Some('c');
|
||||||
|
/// assert_tokens(&opt, &[
|
||||||
|
/// Token::Some,
|
||||||
|
/// Token::Char('c'),
|
||||||
|
/// ]);
|
||||||
|
/// ```
|
||||||
Some,
|
Some,
|
||||||
|
|
||||||
/// A serialized `Option<T>` containing none.
|
/// A serialized `Option<T>` containing none.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// let opt = None::<char>;
|
||||||
|
/// assert_tokens(&opt, &[Token::None]);
|
||||||
|
/// ```
|
||||||
None,
|
None,
|
||||||
|
|
||||||
/// A serialized `()`.
|
/// A serialized `()`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// assert_tokens(&(), &[Token::Unit]);
|
||||||
|
/// ```
|
||||||
Unit,
|
Unit,
|
||||||
|
|
||||||
/// A serialized unit struct of the given name.
|
/// A serialized unit struct of the given name.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # #[macro_use]
|
||||||
|
/// # extern crate serde_derive;
|
||||||
|
/// #
|
||||||
|
/// # extern crate serde;
|
||||||
|
/// # extern crate serde_test;
|
||||||
|
/// #
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// # fn main() {
|
||||||
|
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
/// struct X;
|
||||||
|
///
|
||||||
|
/// assert_tokens(&X, &[Token::UnitStruct { name: "X" }]);
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
UnitStruct { name: &'static str },
|
UnitStruct { name: &'static str },
|
||||||
|
|
||||||
/// The header to a serialized newtype struct of the given name.
|
/// The header to a serialized newtype struct of the given name.
|
||||||
///
|
///
|
||||||
/// Newtype structs are serialized with this header, followed by the value contained in the
|
/// After this header is the value contained in the newtype struct.
|
||||||
/// newtype struct.
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # #[macro_use]
|
||||||
|
/// # extern crate serde_derive;
|
||||||
|
/// #
|
||||||
|
/// # extern crate serde;
|
||||||
|
/// # extern crate serde_test;
|
||||||
|
/// #
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// # fn main() {
|
||||||
|
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
/// struct N(String);
|
||||||
|
///
|
||||||
|
/// let n = N("newtype".to_owned());
|
||||||
|
/// assert_tokens(&n, &[
|
||||||
|
/// Token::NewtypeStruct { name: "N" },
|
||||||
|
/// Token::String("newtype"),
|
||||||
|
/// ]);
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
NewtypeStruct { name: &'static str },
|
NewtypeStruct { name: &'static str },
|
||||||
|
|
||||||
/// The header to an enum of the given name.
|
/// The header to an enum of the given name.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # #[macro_use]
|
||||||
|
/// # extern crate serde_derive;
|
||||||
|
/// #
|
||||||
|
/// # extern crate serde;
|
||||||
|
/// # extern crate serde_test;
|
||||||
|
/// #
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// # fn main() {
|
||||||
|
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
/// enum E {
|
||||||
|
/// A,
|
||||||
|
/// B(u8),
|
||||||
|
/// C(u8, u8),
|
||||||
|
/// D { d: u8 },
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let a = E::A;
|
||||||
|
/// assert_tokens(&a, &[
|
||||||
|
/// Token::Enum { name: "E" },
|
||||||
|
/// Token::Str("A"),
|
||||||
|
/// Token::Unit,
|
||||||
|
/// ]);
|
||||||
|
///
|
||||||
|
/// let b = E::B(0);
|
||||||
|
/// assert_tokens(&b, &[
|
||||||
|
/// Token::Enum { name: "E" },
|
||||||
|
/// Token::Str("B"),
|
||||||
|
/// Token::U8(0),
|
||||||
|
/// ]);
|
||||||
|
///
|
||||||
|
/// let c = E::C(0, 0);
|
||||||
|
/// assert_tokens(&c, &[
|
||||||
|
/// Token::Enum { name: "E" },
|
||||||
|
/// Token::Str("C"),
|
||||||
|
/// Token::Seq { len: Some(2) },
|
||||||
|
/// Token::U8(0),
|
||||||
|
/// Token::U8(0),
|
||||||
|
/// Token::SeqEnd,
|
||||||
|
/// ]);
|
||||||
|
///
|
||||||
|
/// let d = E::D { d: 0 };
|
||||||
|
/// assert_tokens(&d, &[
|
||||||
|
/// Token::Enum { name: "E" },
|
||||||
|
/// Token::Str("D"),
|
||||||
|
/// Token::Map { len: Some(1) },
|
||||||
|
/// Token::Str("d"),
|
||||||
|
/// Token::U8(0),
|
||||||
|
/// Token::MapEnd,
|
||||||
|
/// ]);
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
Enum { name: &'static str },
|
Enum { name: &'static str },
|
||||||
|
|
||||||
/// A unit variant of an enum of the given name, of the given name.
|
/// A unit variant of an enum.
|
||||||
///
|
///
|
||||||
/// The first string represents the name of the enum, and the second represents the name of the
|
/// ```rust
|
||||||
/// variant.
|
/// # #[macro_use]
|
||||||
|
/// # extern crate serde_derive;
|
||||||
|
/// #
|
||||||
|
/// # extern crate serde;
|
||||||
|
/// # extern crate serde_test;
|
||||||
|
/// #
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// # fn main() {
|
||||||
|
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
/// enum E {
|
||||||
|
/// A,
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let a = E::A;
|
||||||
|
/// assert_tokens(&a, &[Token::UnitVariant { name: "E", variant: "A" }]);
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
UnitVariant { name: &'static str, variant: &'static str },
|
UnitVariant { name: &'static str, variant: &'static str },
|
||||||
|
|
||||||
/// The header to a newtype variant of an enum of the given name, of the given name.
|
/// The header to a newtype variant of an enum.
|
||||||
///
|
///
|
||||||
/// The first string represents the name of the enum, and the second represents the name of the
|
/// After this header is the value contained in the newtype variant.
|
||||||
/// variant. The value contained within this enum works the same as `StructNewType`.
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # #[macro_use]
|
||||||
|
/// # extern crate serde_derive;
|
||||||
|
/// #
|
||||||
|
/// # extern crate serde;
|
||||||
|
/// # extern crate serde_test;
|
||||||
|
/// #
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// # fn main() {
|
||||||
|
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
/// enum E {
|
||||||
|
/// B(u8),
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let b = E::B(0);
|
||||||
|
/// assert_tokens(&b, &[
|
||||||
|
/// Token::NewtypeVariant { name: "E", variant: "B" },
|
||||||
|
/// Token::U8(0),
|
||||||
|
/// ]);
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
NewtypeVariant { name: &'static str, variant: &'static str },
|
NewtypeVariant { name: &'static str, variant: &'static str },
|
||||||
|
|
||||||
/// The header to a sequence of the given length.
|
/// The header to a sequence.
|
||||||
///
|
///
|
||||||
/// These are serialized via `serialize_seq`, which takes an optional length. After this
|
/// After this header are the elements of the sequence, followed by
|
||||||
/// header is a list of elements, followed by `SeqEnd`.
|
/// `SeqEnd`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// let vec = vec!['a', 'b', 'c'];
|
||||||
|
/// assert_tokens(&vec, &[
|
||||||
|
/// Token::Seq { len: Some(3) },
|
||||||
|
/// Token::Char('a'),
|
||||||
|
/// Token::Char('b'),
|
||||||
|
/// Token::Char('c'),
|
||||||
|
/// Token::SeqEnd,
|
||||||
|
/// ]);
|
||||||
|
/// ```
|
||||||
Seq { len: Option<usize> },
|
Seq { len: Option<usize> },
|
||||||
|
|
||||||
/// An indicator of the end of a sequence.
|
/// An indicator of the end of a sequence.
|
||||||
SeqEnd,
|
SeqEnd,
|
||||||
|
|
||||||
/// The header to a tuple of the given length, similar to `SeqFixedSize`.
|
/// The header to a tuple.
|
||||||
|
///
|
||||||
|
/// After this header are the elements of the tuple, followed by `TupleEnd`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// let tuple = ('a', 100);
|
||||||
|
/// assert_tokens(&tuple, &[
|
||||||
|
/// Token::Tuple { len: 2 },
|
||||||
|
/// Token::Char('a'),
|
||||||
|
/// Token::I32(100),
|
||||||
|
/// Token::TupleEnd,
|
||||||
|
/// ]);
|
||||||
|
/// ```
|
||||||
Tuple { len: usize },
|
Tuple { len: usize },
|
||||||
|
|
||||||
/// An indicator of the end of a tuple, similar to `SeqEnd`.
|
/// An indicator of the end of a tuple.
|
||||||
TupleEnd,
|
TupleEnd,
|
||||||
|
|
||||||
/// The header to a tuple struct of the given name and length.
|
/// The header to a tuple struct.
|
||||||
|
///
|
||||||
|
/// After this header are the fields of the tuple struct, followed by
|
||||||
|
/// `TupleStructEnd`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # #[macro_use]
|
||||||
|
/// # extern crate serde_derive;
|
||||||
|
/// #
|
||||||
|
/// # extern crate serde;
|
||||||
|
/// # extern crate serde_test;
|
||||||
|
/// #
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// # fn main() {
|
||||||
|
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
/// struct T(u8, u8);
|
||||||
|
///
|
||||||
|
/// let t = T(0, 0);
|
||||||
|
/// assert_tokens(&t, &[
|
||||||
|
/// Token::TupleStruct { name: "T", len: 2 },
|
||||||
|
/// Token::U8(0),
|
||||||
|
/// Token::U8(0),
|
||||||
|
/// Token::TupleStructEnd,
|
||||||
|
/// ]);
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
TupleStruct { name: &'static str, len: usize },
|
TupleStruct { name: &'static str, len: usize },
|
||||||
|
|
||||||
/// An indicator of the end of a tuple struct, similar to `TupleEnd`.
|
/// An indicator of the end of a tuple struct.
|
||||||
TupleStructEnd,
|
TupleStructEnd,
|
||||||
|
|
||||||
/// The header to a map of the given length.
|
/// The header to a map.
|
||||||
///
|
///
|
||||||
/// These are serialized via `serialize_map`, which takes an optional length. After this header
|
/// After this header are the entries of the map, followed by `MapEnd`.
|
||||||
/// is a list of key-value pairs, followed by `MapEnd`.
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// use std::collections::BTreeMap;
|
||||||
|
///
|
||||||
|
/// let mut map = BTreeMap::new();
|
||||||
|
/// map.insert('A', 65);
|
||||||
|
/// map.insert('Z', 90);
|
||||||
|
///
|
||||||
|
/// assert_tokens(&map, &[
|
||||||
|
/// Token::Map { len: Some(2) },
|
||||||
|
/// Token::Char('A'),
|
||||||
|
/// Token::I32(65),
|
||||||
|
/// Token::Char('Z'),
|
||||||
|
/// Token::I32(90),
|
||||||
|
/// Token::MapEnd,
|
||||||
|
/// ]);
|
||||||
|
/// ```
|
||||||
Map { len: Option<usize> },
|
Map { len: Option<usize> },
|
||||||
|
|
||||||
/// An indicator of the end of a map.
|
/// An indicator of the end of a map.
|
||||||
MapEnd,
|
MapEnd,
|
||||||
|
|
||||||
/// The header of a struct of the given name and length, similar to `Map`.
|
/// The header of a struct.
|
||||||
|
///
|
||||||
|
/// After this header are the fields of the struct, followed by `StructEnd`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # #[macro_use]
|
||||||
|
/// # extern crate serde_derive;
|
||||||
|
/// #
|
||||||
|
/// # extern crate serde;
|
||||||
|
/// # extern crate serde_test;
|
||||||
|
/// #
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// # fn main() {
|
||||||
|
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
/// struct S {
|
||||||
|
/// a: u8,
|
||||||
|
/// b: u8,
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let s = S { a: 0, b: 0 };
|
||||||
|
/// assert_tokens(&s, &[
|
||||||
|
/// Token::Struct { name: "S", len: 2 },
|
||||||
|
/// Token::Str("a"),
|
||||||
|
/// Token::U8(0),
|
||||||
|
/// Token::Str("b"),
|
||||||
|
/// Token::U8(0),
|
||||||
|
/// Token::StructEnd,
|
||||||
|
/// ]);
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
Struct { name: &'static str, len: usize },
|
Struct { name: &'static str, len: usize },
|
||||||
|
|
||||||
/// An indicator of the end of a struct, similar to `MapEnd`.
|
/// An indicator of the end of a struct.
|
||||||
StructEnd,
|
StructEnd,
|
||||||
|
|
||||||
/// The header to a tuple variant of an enum of the given name, of the given name and length.
|
/// The header to a tuple variant of an enum.
|
||||||
|
///
|
||||||
|
/// After this header are the fields of the tuple variant, followed by
|
||||||
|
/// `TupleVariantEnd`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # #[macro_use]
|
||||||
|
/// # extern crate serde_derive;
|
||||||
|
/// #
|
||||||
|
/// # extern crate serde;
|
||||||
|
/// # extern crate serde_test;
|
||||||
|
/// #
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// # fn main() {
|
||||||
|
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
/// enum E {
|
||||||
|
/// C(u8, u8),
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let c = E::C(0, 0);
|
||||||
|
/// assert_tokens(&c, &[
|
||||||
|
/// Token::TupleVariant { name: "E", variant: "C", len: 2 },
|
||||||
|
/// Token::U8(0),
|
||||||
|
/// Token::U8(0),
|
||||||
|
/// Token::TupleVariantEnd,
|
||||||
|
/// ]);
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
TupleVariant { name: &'static str, variant: &'static str, len: usize },
|
TupleVariant { name: &'static str, variant: &'static str, len: usize },
|
||||||
|
|
||||||
/// An indicator of the end of a tuple variant, similar to `TupleEnd`.
|
/// An indicator of the end of a tuple variant.
|
||||||
TupleVariantEnd,
|
TupleVariantEnd,
|
||||||
|
|
||||||
/// The header of a struct variant of an enum of the given name, of the given name and length,
|
/// The header of a struct variant of an enum.
|
||||||
/// similar to `Struct`.
|
///
|
||||||
|
/// After this header are the fields of the struct variant, followed by
|
||||||
|
/// `StructVariantEnd`.
|
||||||
|
///
|
||||||
|
/// ```rust
|
||||||
|
/// # #[macro_use]
|
||||||
|
/// # extern crate serde_derive;
|
||||||
|
/// #
|
||||||
|
/// # extern crate serde;
|
||||||
|
/// # extern crate serde_test;
|
||||||
|
/// #
|
||||||
|
/// # use serde_test::{assert_tokens, Token};
|
||||||
|
/// #
|
||||||
|
/// # fn main() {
|
||||||
|
/// #[derive(Serialize, Deserialize, PartialEq, Debug)]
|
||||||
|
/// enum E {
|
||||||
|
/// D { d: u8 },
|
||||||
|
/// }
|
||||||
|
///
|
||||||
|
/// let d = E::D { d: 0 };
|
||||||
|
/// assert_tokens(&d, &[
|
||||||
|
/// Token::StructVariant { name: "E", variant: "D", len: 1 },
|
||||||
|
/// Token::Str("d"),
|
||||||
|
/// Token::U8(0),
|
||||||
|
/// Token::StructVariantEnd,
|
||||||
|
/// ]);
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
StructVariant { name: &'static str, variant: &'static str, len: usize },
|
StructVariant { name: &'static str, variant: &'static str, len: usize },
|
||||||
|
|
||||||
/// An indicator of the end of a struct, similar to `StructEnd`.
|
/// An indicator of the end of a struct variant.
|
||||||
StructVariantEnd,
|
StructVariantEnd,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user