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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use crateState;
use crateError;
use crateEvent;
use crateSerializeRef;
use Box;
/// The function that receives the events of a [`SerializeDriver`](crate::ser::SerializeDriver).
pub type EventFn<'f> =
dyn FnMut + 'f;
/// A layer between the serialization and a format.
///
/// Layers are added to a [`SerializeDriver`](crate::ser::SerializeDriver)
/// with [`push_layer`](crate::ser::SerializeDriver::push_layer) and see
/// every event produced by the serialized values before the format receives
/// it. A layer receives the event together with a [`Next`] which passes
/// events on to the next layer (or the format). This way a layer can
/// observe events and track information in the [`State`] (which the
/// [`Serialize`](crate::ser::Serialize) implementations of the values that
/// follow can access), reject, change or drop events or emit additional
/// events.
///
/// Layers are only applied by [`drive`](crate::ser::SerializeDriver::drive).
///
/// ```
/// use deser::ser::{Layer, Next, SerializeDriver};
/// use deser::{Atom, Error, Event};
///
/// /// Upper cases all map keys.
/// struct UppercaseKeys;
///
/// impl Layer for UppercaseKeys {
/// fn event(
/// &mut self,
/// event: Event<'_>,
/// next: &mut Next<'_>,
/// ) -> Result<(), Error> {
/// match event {
/// Event::Atom(Atom::Str(key)) if next.state().is_map_key() => {
/// next.emit(Event::from(key.to_uppercase()))
/// }
/// event => next.emit(event),
/// }
/// }
/// }
///
/// let mut map = std::collections::BTreeMap::new();
/// map.insert("key", "value");
/// let mut events = Vec::new();
/// let mut driver = SerializeDriver::new(&map);
/// driver.push_layer(UppercaseKeys);
/// driver.drive(|event, _| {
/// events.push(event.to_static());
/// Ok(())
/// }).unwrap();
/// assert_eq!(events.len(), 4);
/// assert_eq!(events[1], "KEY".into());
/// ```
///
/// # Changing the Events
///
/// Layers that emit events other than the one they received should take
/// care of the [`State`]: the next layers and the format see the state as
/// it is when the event is emitted. For instance a layer which delays an
/// event emits it with the [event data](State::event) of the event that is
/// current when it's emitted. Map keys that are emitted at another time
/// have to be emitted with [`Next::emit_key`] so that they are recognized
/// as map keys (see [`State::is_map_key`]).
///
/// All events a layer emits are passed on with the value of the event the
/// layer received (see [`Next::value`]), so that formats that
/// [describe](crate::ser::Describe) values see the description of the
/// original value.
/// Passes events on to the next [`Layer`].
///
/// The last layer passes the events on to the format.