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
158
use crateState;
use crateDriverCore;
use crateError;
use crateEvent;
use Box;
/// A layer between a format and the sinks.
///
/// Layers are added to a [`DeserializeDriver`](crate::de::DeserializeDriver)
/// with [`push_layer`](crate::de::DeserializeDriver::push_layer), usually in
/// the setup callback of
/// [`Deserializer::deserialize_with`](crate::de::Deserializer::deserialize_with). Every
/// event that is emitted into the driver passes through the layers before
/// it's delivered to the sinks. A layer receives the event together with
/// a [`Next`] which passes events on to the next layer (or the sinks). This
/// way a layer can:
///
/// * observe events and track information in the [`State`] (for instance
/// the current path),
/// * reject events by returning an error (for instance to enforce limits),
/// * change events, drop them or emit additional events,
/// * act on the result of the events that it passed on.
///
/// The position of the event is known when a layer is invoked:
/// [`State::is_map_key`] tells if the event is (the start of) a map key
/// and [`State::depth`] is the number of open containers.
///
/// ```
/// use deser::de::{Deserializer, Layer, LayerEvent, Next};
/// use deser::{Atom, Error, Event};
///
/// /// Upper cases all strings that are not map keys.
/// struct Uppercase;
///
/// impl Layer for Uppercase {
/// fn event<'de>(
/// &mut self,
/// event: LayerEvent<'_, 'de>,
/// next: &mut Next<'_, 'de>,
/// ) -> Result<(), Error> {
/// match event.event() {
/// Event::Atom(Atom::Str(s)) if !next.state().is_map_key() => {
/// next.emit(LayerEvent::new(Event::from(s.to_uppercase())))
/// }
/// _ => next.emit(event),
/// }
/// }
/// }
///
/// let words: Vec<String> = deser_json::Deserializer::from_str(r#"["hello", "world"]"#)
/// .deserialize_with(|driver| driver.push_layer(Uppercase))
/// .unwrap();
/// assert_eq!(words, ["HELLO", "WORLD"]);
/// ```
///
/// # Buffered Values
///
/// Some types buffer values and replay them later (see
/// [`Recording`](crate::de::Recording)). Replayed events do not pass
/// through the layers again as they already did when they were recorded,
/// this means that changes made by layers are retained and not applied
/// twice. Information that layers keep in the state and that is needed
/// for replayed values should be kept in replayable extensions (see
/// [`State::set_replayable`]) which are captured with the events.
/// An event that passes through the [`Layer`]s.
///
/// Events emitted with
/// [`emit_borrowed`](crate::de::DeserializeDriver::emit_borrowed) borrow
/// from the data that is deserialized for `'de`, other events are only
/// valid for `'e`. Layers that pass on the event they received retain
/// this.
;
/// Passes events on to the next [`Layer`].
///
/// The last layer passes the events on to the sinks.