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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//! Converts values from one data format to another.
//!
//! Every [`Deserializer`] can be transcoded into every [`Serializer`],
//! without types in between and without code that is specific to the
//! formats:
//!
//! ```rust
//! let mut de = deser_json::Deserializer::from_str(r#"{"name": "deser", "tags": ["a", "b"]}"#);
//! let mut ser = deser_yaml::Serializer::new();
//! deser_transcode::transcode(&mut de, &mut ser).unwrap();
//! assert_eq!(ser.finish(), "name: deser\ntags:\n - a\n - b\n");
//! ```
//!
//! # How it Works
//!
//! A deserializer parses a value and pushes its events into a
//! [`DeserializeDriver`], a serializer receives the events of a value
//! from a [`SerializeDriver`]. Both are in control of their loop, so a
//! value is recorded into a [`RecordBuf`] and then serialized from it.
//! Only one value is held at a time, and strings which the deserializer
//! lends from the input (like the strings without escape sequences in
//! JSON) are not copied. Recording also makes the length of every map and
//! sequence known before it's serialized, even if the input format did not
//! say, so formats that write lengths upfront (such as CBOR and
//! MessagePack) do not have to fix them up afterwards.
//!
//! The events are passed on as the deserializer emitted them, together
//! with the data attached to them (such as CBOR tags). Each serializer
//! then does with them what it does with any value, there are no rules
//! specific to transcoding:
//!
//! * values whose type the input format infers from their text (like
//! `true` or `1.0` in YAML) are written as the type they were inferred
//! as, text that is text in the input format (like the values of query
//! strings) stays text.
//! * keys that are not strings are written the way the target format
//! writes such keys (JSON writes `1` as `"1"`), keys that the target
//! format cannot express (such as sequences in JSON) are an error.
//! * values that the target format cannot express use its fallback (bytes
//! are base64 in text formats, TOML skips map entries that are null) or
//! are an error (TOML documents must be tables).
//! * duplicate keys are passed on, it's up to the target format what it
//! does with them.
//!
//! # Streams
//!
//! Every call transcodes a single value. What follows the value in the
//! input depends on the configuration of the deserializer, for a stream of
//! values (like JSON Lines or YAML documents) transcode until the
//! deserializer is at its end. A [`Transcoder`] reuses its buffer for all
//! values:
//!
//! ```rust
//! use deser_json::{DeserializerConfig, Trailing};
//! use deser_transcode::Transcoder;
//!
//! let config = DeserializerConfig::builder().trailing(Trailing::Newline).build();
//! let mut de = deser_json::Deserializer::from_str_with_config(
//! "{\"a\": 1}\n{\"a\": 2}\n",
//! config,
//! );
//! let mut ser = deser_yaml::Serializer::new();
//! let mut transcoder = Transcoder::new();
//! while !de.is_end() {
//! transcoder.transcode(&mut de, &mut ser).unwrap();
//! }
//! assert_eq!(ser.finish(), "a: 1\n---\na: 2\n");
//! ```
//!
//! Readers and writers of `deser::io` are deserializers and serializers
//! too, so values can be transcoded from one stream to another (for
//! instance from a file to standard output) without holding more than one
//! value in memory:
//!
//! ```rust
//! use deser_json::{DeserializerConfig, Trailing};
//! use deser_transcode::Transcoder;
//!
//! let config = DeserializerConfig::builder().trailing(Trailing::Newline).build();
//! let input = &b"{\"a\": 1}\n{\"a\": 2}\n"[..];
//! let mut de = config.reader(input);
//! let mut ser = deser_yaml::SerializerConfig::new().writer(Vec::new());
//! let mut transcoder = Transcoder::new();
//! while !de.is_end().unwrap() {
//! transcoder.transcode(&mut de, &mut ser).unwrap();
//! }
//! assert_eq!(ser.into_inner(), b"a: 1\n---\na: 2\n");
//! ```
//!
//! Values that are read from streams cannot borrow from the input, strings
//! are copied into the buffer of the transcoder.
//!
//! # Layers
//!
//! Layers can be added to both sides (see [`transcode_with`]), for
//! instance to limit what is accepted from the input. Errors of the
//! deserializer carry the location in the input as usual.
use ;
use ;
use ;
/// Transcodes a value from a deserializer into a serializer.
///
/// This reads the next value of the deserializer and serializes it. To
/// transcode many values, [`Transcoder`] reuses the buffer.
/// Transcodes a value with configured drivers.
///
/// The callbacks are invoked with the drivers before the value is
/// deserialized and serialized, for instance to add layers or to give
/// them a context:
///
/// ```rust
/// use deser::Context;
/// use deser::de::Limits;
///
/// let limits = Context::with(Limits::builder().max_depth(2).build());
/// let mut de = deser_json::Deserializer::from_str("[[[1]]]");
/// let mut ser = deser_yaml::Serializer::new();
/// let err = deser_transcode::transcode_with(
/// &mut de,
/// &mut ser,
/// |driver| driver.set_context(limits.clone()),
/// |_driver| {},
/// )
/// .unwrap_err();
/// assert_eq!(
/// err.to_string(),
/// "LimitExceeded: recursion limit exceeded at line 1 column 3"
/// );
/// ```
/// Transcodes values and reuses its buffer.
///
/// This is useful to transcode many values, see [`transcode`] and
/// [`transcode_with`] for what it does. The transcoder can be used for all
/// values of an input, the buffer it holds borrows from it.