• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 use std::fmt::{self, Debug, Display};
2 
3 #[derive(Copy, Clone, PartialEq, Debug)]
4 pub enum Token {
5     /// A serialized `bool`.
6     ///
7     /// ```edition2018
8     /// # use serde_test::{assert_tokens, Token};
9     /// #
10     /// assert_tokens(&true, &[Token::Bool(true)]);
11     /// ```
12     Bool(bool),
13 
14     /// A serialized `i8`.
15     ///
16     /// ```edition2018
17     /// # use serde_test::{assert_tokens, Token};
18     /// #
19     /// assert_tokens(&0i8, &[Token::I8(0)]);
20     /// ```
21     I8(i8),
22 
23     /// A serialized `i16`.
24     ///
25     /// ```edition2018
26     /// # use serde_test::{assert_tokens, Token};
27     /// #
28     /// assert_tokens(&0i16, &[Token::I16(0)]);
29     /// ```
30     I16(i16),
31 
32     /// A serialized `i32`.
33     ///
34     /// ```edition2018
35     /// # use serde_test::{assert_tokens, Token};
36     /// #
37     /// assert_tokens(&0i32, &[Token::I32(0)]);
38     /// ```
39     I32(i32),
40 
41     /// A serialized `i64`.
42     ///
43     /// ```edition2018
44     /// # use serde_test::{assert_tokens, Token};
45     /// #
46     /// assert_tokens(&0i64, &[Token::I64(0)]);
47     /// ```
48     I64(i64),
49 
50     /// A serialized `u8`.
51     ///
52     /// ```edition2018
53     /// # use serde_test::{assert_tokens, Token};
54     /// #
55     /// assert_tokens(&0u8, &[Token::U8(0)]);
56     /// ```
57     U8(u8),
58 
59     /// A serialized `u16`.
60     ///
61     /// ```edition2018
62     /// # use serde_test::{assert_tokens, Token};
63     /// #
64     /// assert_tokens(&0u16, &[Token::U16(0)]);
65     /// ```
66     U16(u16),
67 
68     /// A serialized `u32`.
69     ///
70     /// ```edition2018
71     /// # use serde_test::{assert_tokens, Token};
72     /// #
73     /// assert_tokens(&0u32, &[Token::U32(0)]);
74     /// ```
75     U32(u32),
76 
77     /// A serialized `u64`.
78     ///
79     /// ```edition2018
80     /// # use serde_test::{assert_tokens, Token};
81     /// #
82     /// assert_tokens(&0u64, &[Token::U64(0)]);
83     /// ```
84     U64(u64),
85 
86     /// A serialized `f32`.
87     ///
88     /// ```edition2018
89     /// # use serde_test::{assert_tokens, Token};
90     /// #
91     /// assert_tokens(&0f32, &[Token::F32(0.0)]);
92     /// ```
93     F32(f32),
94 
95     /// A serialized `f64`.
96     ///
97     /// ```edition2018
98     /// # use serde_test::{assert_tokens, Token};
99     /// #
100     /// assert_tokens(&0f64, &[Token::F64(0.0)]);
101     /// ```
102     F64(f64),
103 
104     /// A serialized `char`.
105     ///
106     /// ```edition2018
107     /// # use serde_test::{assert_tokens, Token};
108     /// #
109     /// assert_tokens(&'\n', &[Token::Char('\n')]);
110     /// ```
111     Char(char),
112 
113     /// A serialized `str`.
114     ///
115     /// ```edition2018
116     /// # use serde_test::{assert_tokens, Token};
117     /// #
118     /// let s = String::from("transient");
119     /// assert_tokens(&s, &[Token::Str("transient")]);
120     /// ```
121     Str(&'static str),
122 
123     /// A borrowed `str`.
124     ///
125     /// ```edition2018
126     /// # use serde_test::{assert_tokens, Token};
127     /// #
128     /// let s: &str = "borrowed";
129     /// assert_tokens(&s, &[Token::BorrowedStr("borrowed")]);
130     /// ```
131     BorrowedStr(&'static str),
132 
133     /// A serialized `String`.
134     ///
135     /// ```edition2018
136     /// # use serde_test::{assert_tokens, Token};
137     /// #
138     /// let s = String::from("owned");
139     /// assert_tokens(&s, &[Token::String("owned")]);
140     /// ```
141     String(&'static str),
142 
143     /// A serialized `[u8]`
144     Bytes(&'static [u8]),
145 
146     /// A borrowed `[u8]`.
147     BorrowedBytes(&'static [u8]),
148 
149     /// A serialized `ByteBuf`
150     ByteBuf(&'static [u8]),
151 
152     /// A serialized `Option<T>` containing none.
153     ///
154     /// ```edition2018
155     /// # use serde_test::{assert_tokens, Token};
156     /// #
157     /// let opt = None::<char>;
158     /// assert_tokens(&opt, &[Token::None]);
159     /// ```
160     None,
161 
162     /// The header to a serialized `Option<T>` containing some value.
163     ///
164     /// The tokens of the value follow after this header.
165     ///
166     /// ```edition2018
167     /// # use serde_test::{assert_tokens, Token};
168     /// #
169     /// let opt = Some('c');
170     /// assert_tokens(&opt, &[
171     ///     Token::Some,
172     ///     Token::Char('c'),
173     /// ]);
174     /// ```
175     Some,
176 
177     /// A serialized `()`.
178     ///
179     /// ```edition2018
180     /// # use serde_test::{assert_tokens, Token};
181     /// #
182     /// assert_tokens(&(), &[Token::Unit]);
183     /// ```
184     Unit,
185 
186     /// A serialized unit struct of the given name.
187     ///
188     /// ```edition2018
189     /// # use serde::{Serialize, Deserialize};
190     /// # use serde_test::{assert_tokens, Token};
191     /// #
192     /// # fn main() {
193     /// #[derive(Serialize, Deserialize, PartialEq, Debug)]
194     /// struct X;
195     ///
196     /// assert_tokens(&X, &[Token::UnitStruct { name: "X" }]);
197     /// # }
198     /// ```
199     UnitStruct { name: &'static str },
200 
201     /// A unit variant of an enum.
202     ///
203     /// ```edition2018
204     /// # use serde::{Serialize, Deserialize};
205     /// # use serde_test::{assert_tokens, Token};
206     /// #
207     /// # fn main() {
208     /// #[derive(Serialize, Deserialize, PartialEq, Debug)]
209     /// enum E {
210     ///     A,
211     /// }
212     ///
213     /// let a = E::A;
214     /// assert_tokens(&a, &[Token::UnitVariant { name: "E", variant: "A" }]);
215     /// # }
216     /// ```
217     UnitVariant {
218         name: &'static str,
219         variant: &'static str,
220     },
221 
222     /// The header to a serialized newtype struct of the given name.
223     ///
224     /// After this header is the value contained in the newtype struct.
225     ///
226     /// ```edition2018
227     /// # use serde::{Serialize, Deserialize};
228     /// # use serde_test::{assert_tokens, Token};
229     /// #
230     /// # fn main() {
231     /// #[derive(Serialize, Deserialize, PartialEq, Debug)]
232     /// struct N(String);
233     ///
234     /// let n = N("newtype".to_owned());
235     /// assert_tokens(&n, &[
236     ///     Token::NewtypeStruct { name: "N" },
237     ///     Token::String("newtype"),
238     /// ]);
239     /// # }
240     /// ```
241     NewtypeStruct { name: &'static str },
242 
243     /// The header to a newtype variant of an enum.
244     ///
245     /// After this header is the value contained in the newtype variant.
246     ///
247     /// ```edition2018
248     /// # use serde::{Serialize, Deserialize};
249     /// # use serde_test::{assert_tokens, Token};
250     /// #
251     /// # fn main() {
252     /// #[derive(Serialize, Deserialize, PartialEq, Debug)]
253     /// enum E {
254     ///     B(u8),
255     /// }
256     ///
257     /// let b = E::B(0);
258     /// assert_tokens(&b, &[
259     ///     Token::NewtypeVariant { name: "E", variant: "B" },
260     ///     Token::U8(0),
261     /// ]);
262     /// # }
263     /// ```
264     NewtypeVariant {
265         name: &'static str,
266         variant: &'static str,
267     },
268 
269     /// The header to a sequence.
270     ///
271     /// After this header are the elements of the sequence, followed by
272     /// `SeqEnd`.
273     ///
274     /// ```edition2018
275     /// # use serde_test::{assert_tokens, Token};
276     /// #
277     /// let vec = vec!['a', 'b', 'c'];
278     /// assert_tokens(&vec, &[
279     ///     Token::Seq { len: Some(3) },
280     ///     Token::Char('a'),
281     ///     Token::Char('b'),
282     ///     Token::Char('c'),
283     ///     Token::SeqEnd,
284     /// ]);
285     /// ```
286     Seq { len: Option<usize> },
287 
288     /// An indicator of the end of a sequence.
289     SeqEnd,
290 
291     /// The header to a tuple.
292     ///
293     /// After this header are the elements of the tuple, followed by `TupleEnd`.
294     ///
295     /// ```edition2018
296     /// # use serde_test::{assert_tokens, Token};
297     /// #
298     /// let tuple = ('a', 100);
299     /// assert_tokens(&tuple, &[
300     ///     Token::Tuple { len: 2 },
301     ///     Token::Char('a'),
302     ///     Token::I32(100),
303     ///     Token::TupleEnd,
304     /// ]);
305     /// ```
306     Tuple { len: usize },
307 
308     /// An indicator of the end of a tuple.
309     TupleEnd,
310 
311     /// The header to a tuple struct.
312     ///
313     /// After this header are the fields of the tuple struct, followed by
314     /// `TupleStructEnd`.
315     ///
316     /// ```edition2018
317     /// # use serde::{Serialize, Deserialize};
318     /// # use serde_test::{assert_tokens, Token};
319     /// #
320     /// # fn main() {
321     /// #[derive(Serialize, Deserialize, PartialEq, Debug)]
322     /// struct T(u8, u8);
323     ///
324     /// let t = T(0, 0);
325     /// assert_tokens(&t, &[
326     ///     Token::TupleStruct { name: "T", len: 2 },
327     ///     Token::U8(0),
328     ///     Token::U8(0),
329     ///     Token::TupleStructEnd,
330     /// ]);
331     /// # }
332     /// ```
333     TupleStruct { name: &'static str, len: usize },
334 
335     /// An indicator of the end of a tuple struct.
336     TupleStructEnd,
337 
338     /// The header to a tuple variant of an enum.
339     ///
340     /// After this header are the fields of the tuple variant, followed by
341     /// `TupleVariantEnd`.
342     ///
343     /// ```edition2018
344     /// # use serde::{Serialize, Deserialize};
345     /// # use serde_test::{assert_tokens, Token};
346     /// #
347     /// # fn main() {
348     /// #[derive(Serialize, Deserialize, PartialEq, Debug)]
349     /// enum E {
350     ///     C(u8, u8),
351     /// }
352     ///
353     /// let c = E::C(0, 0);
354     /// assert_tokens(&c, &[
355     ///     Token::TupleVariant { name: "E", variant: "C", len: 2 },
356     ///     Token::U8(0),
357     ///     Token::U8(0),
358     ///     Token::TupleVariantEnd,
359     /// ]);
360     /// # }
361     /// ```
362     TupleVariant {
363         name: &'static str,
364         variant: &'static str,
365         len: usize,
366     },
367 
368     /// An indicator of the end of a tuple variant.
369     TupleVariantEnd,
370 
371     /// The header to a map.
372     ///
373     /// After this header are the entries of the map, followed by `MapEnd`.
374     ///
375     /// ```edition2018
376     /// # use serde_test::{assert_tokens, Token};
377     /// #
378     /// use std::collections::BTreeMap;
379     ///
380     /// let mut map = BTreeMap::new();
381     /// map.insert('A', 65);
382     /// map.insert('Z', 90);
383     ///
384     /// assert_tokens(&map, &[
385     ///     Token::Map { len: Some(2) },
386     ///     Token::Char('A'),
387     ///     Token::I32(65),
388     ///     Token::Char('Z'),
389     ///     Token::I32(90),
390     ///     Token::MapEnd,
391     /// ]);
392     /// ```
393     Map { len: Option<usize> },
394 
395     /// An indicator of the end of a map.
396     MapEnd,
397 
398     /// The header of a struct.
399     ///
400     /// After this header are the fields of the struct, followed by `StructEnd`.
401     ///
402     /// ```edition2018
403     /// # use serde::{Serialize, Deserialize};
404     /// # use serde_test::{assert_tokens, Token};
405     /// #
406     /// # fn main() {
407     /// #[derive(Serialize, Deserialize, PartialEq, Debug)]
408     /// struct S {
409     ///     a: u8,
410     ///     b: u8,
411     /// }
412     ///
413     /// let s = S { a: 0, b: 0 };
414     /// assert_tokens(&s, &[
415     ///     Token::Struct { name: "S", len: 2 },
416     ///     Token::Str("a"),
417     ///     Token::U8(0),
418     ///     Token::Str("b"),
419     ///     Token::U8(0),
420     ///     Token::StructEnd,
421     /// ]);
422     /// # }
423     /// ```
424     Struct { name: &'static str, len: usize },
425 
426     /// An indicator of the end of a struct.
427     StructEnd,
428 
429     /// The header of a struct variant of an enum.
430     ///
431     /// After this header are the fields of the struct variant, followed by
432     /// `StructVariantEnd`.
433     ///
434     /// ```edition2018
435     /// # use serde::{Serialize, Deserialize};
436     /// # use serde_test::{assert_tokens, Token};
437     /// #
438     /// # fn main() {
439     /// #[derive(Serialize, Deserialize, PartialEq, Debug)]
440     /// enum E {
441     ///     D { d: u8 },
442     /// }
443     ///
444     /// let d = E::D { d: 0 };
445     /// assert_tokens(&d, &[
446     ///     Token::StructVariant { name: "E", variant: "D", len: 1 },
447     ///     Token::Str("d"),
448     ///     Token::U8(0),
449     ///     Token::StructVariantEnd,
450     /// ]);
451     /// # }
452     /// ```
453     StructVariant {
454         name: &'static str,
455         variant: &'static str,
456         len: usize,
457     },
458 
459     /// An indicator of the end of a struct variant.
460     StructVariantEnd,
461 
462     /// The header to an enum of the given name.
463     ///
464     /// ```edition2018
465     /// # use serde::{Serialize, Deserialize};
466     /// # use serde_test::{assert_tokens, Token};
467     /// #
468     /// # fn main() {
469     /// #[derive(Serialize, Deserialize, PartialEq, Debug)]
470     /// enum E {
471     ///     A,
472     ///     B(u8),
473     ///     C(u8, u8),
474     ///     D { d: u8 },
475     /// }
476     ///
477     /// let a = E::A;
478     /// assert_tokens(&a, &[
479     ///     Token::Enum { name: "E" },
480     ///     Token::Str("A"),
481     ///     Token::Unit,
482     /// ]);
483     ///
484     /// let b = E::B(0);
485     /// assert_tokens(&b, &[
486     ///     Token::Enum { name: "E" },
487     ///     Token::Str("B"),
488     ///     Token::U8(0),
489     /// ]);
490     ///
491     /// let c = E::C(0, 0);
492     /// assert_tokens(&c, &[
493     ///     Token::Enum { name: "E" },
494     ///     Token::Str("C"),
495     ///     Token::Seq { len: Some(2) },
496     ///     Token::U8(0),
497     ///     Token::U8(0),
498     ///     Token::SeqEnd,
499     /// ]);
500     ///
501     /// let d = E::D { d: 0 };
502     /// assert_tokens(&d, &[
503     ///     Token::Enum { name: "E" },
504     ///     Token::Str("D"),
505     ///     Token::Map { len: Some(1) },
506     ///     Token::Str("d"),
507     ///     Token::U8(0),
508     ///     Token::MapEnd,
509     /// ]);
510     /// # }
511     /// ```
512     Enum { name: &'static str },
513 }
514 
515 impl Display for Token {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result516     fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
517         Debug::fmt(self, formatter)
518     }
519 }
520