1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
use crateError;
use crate;
/// Serializes values into an output.
///
/// This is implemented by the serializers of the data formats (for instance
/// `deser_json::Serializer`) and by other destinations of values (like the
/// value type of `deser-value`). A serializer holds its output, every call
/// serializes a value into it. Serializers implement
/// [`drive`](Self::drive) which receives the events of a value from a
/// driver. The provided methods create the driver:
///
/// * [`serialize`](Self::serialize) serializes a value.
/// * [`serialize_with`](Self::serialize_with) serializes a value and allows
/// configuring the driver first, for instance to add
/// [`Layer`](crate::ser::Layer)s.
///
/// ```
/// use deser::ser::{SerializeDriver, Serializer};
/// use deser::{Atom, Error, ErrorKind, Event};
///
/// /// A format which writes sequences of numbers comma separated.
/// struct Numbers(String);
///
/// impl Serializer for Numbers {
/// fn drive(
/// &mut self,
/// driver: &mut SerializeDriver<'_>,
/// ) -> Result<(), Error> {
/// driver.drive(|event, _state| {
/// match event {
/// Event::Atom(Atom::U64(value)) => {
/// if !self.0.is_empty() {
/// self.0.push_str(", ");
/// }
/// self.0.push_str(&value.to_string());
/// }
/// Event::SeqStart(_) | Event::SeqEnd => {}
/// _ => {
/// return Err(Error::new(
/// ErrorKind::UnsupportedType,
/// "not a number",
/// ));
/// }
/// }
/// Ok(())
/// })
/// }
/// }
///
/// let mut numbers = Numbers(String::new());
/// numbers.serialize(&vec![1u64, 2, 3]).unwrap();
/// assert_eq!(numbers.0, "1, 2, 3");
/// ```