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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! The JSON half of the trait set.
//!
//! [`Read`] and [`Write`] are implemented for every supported type.
//! [`ReadObject`] and [`WriteObject`] describe a struct's fields against the
//! shared [`Keys`] schema, and are what the [`object!`](crate::object) macro
//! generates. [`ReadArray`] and [`WriteArray`] are their positional
//! counterparts, from [`array!`](crate::array). [`ReadEnum`] is the same idea
//! against the [`Variants`] schema, from
//! [`tagged_enum!`](crate::tagged_enum); it has no writing half, because a
//! variant is written by one call rather than by a callback over its parts.
//!
//! [`ReadAs`] and [`WriteAs`] are the same pair as [`Read`] and [`Write`],
//! moved off the type and onto an *adapter*, which is what lets a field keep a
//! type from a crate you do not own. [`ReadKeyAs`] and [`WriteKeyAs`] are the
//! same idea for a map's keys, which go through
//! [`FromJsonKey`](crate::json::FromJsonKey) rather than through [`Read`].
//!
//! Implementing these by hand is fully supported and is the escape hatch for
//! anything the macro cannot express. The macro exists only to remove the
//! boilerplate.
use cratePResult;
use crateParser;
use crateWriter;
use crateOptions;
use crate;
/// A type that can be parsed from JSON.
///
/// Reading is into an existing value rather than returning a new one, so
/// buffers and allocations already held by the destination get reused. This is
/// the same reason Glaze reads into a reference.
///
/// The `'de` lifetime is the input document's. A type that borrows from the
/// input, such as `&'de str`, ties itself to it; an owning type ignores it.
/// A type that can be serialized to JSON.
///
/// The method is generic over the [write policy](crate::Options) rather than
/// the trait being generic over it, which is what keeps a bound on a container
/// element spelled `T: Write` instead of `T: Write<O>`. An implementation
/// forwards `w` on and never names `O` unless it wants to read a setting.
///
/// Writing returns nothing, so an impl has no way to report a failure, and it
/// cannot take a byte back either: a [`Writer`] does not rewind, for the reason
/// its own documentation gives. An impl that might change its mind about what
/// to emit therefore has to settle the question before it emits its first byte
/// rather than discover it half way through.
/// [`Raw`](crate::json::Raw) is the worked example, deciding with a probe pass
/// whether its span can be laid out before laying out any of it.
///
/// A write recurses once per nesting level and has no depth limit. Reading is
/// bounded by [`MAX_DEPTH`](crate::json::MAX_DEPTH) because a document's depth
/// is its sender's choice; a value being written is the program's own, so its
/// depth is the caller's to bound, as it already is for dropping that value.
/// How a field of type `T` is read when its declaration names this adapter.
///
/// Implemented by the adapter rather than by `T`, which is what lets it
/// describe a type from another crate: the adapter is local to whoever writes
/// the impl, so the orphan rule is satisfied wherever it lives. The field keeps
/// its own type; only the reading of it moves.
///
/// ```
/// # use std::time::Duration;
/// # use structio::{ErrorCode, Options, json};
/// struct Millis;
///
/// impl<'de> json::ReadAs<'de, Duration> for Millis {
/// fn read<O: Options>(
/// value: &mut Duration,
/// p: &mut json::Parser<'de, O>,
/// ) -> Result<(), ErrorCode> {
/// let mut ms = 0u64;
/// json::Read::read(&mut ms, p)?;
/// *value = Duration::from_millis(ms);
/// Ok(())
/// }
/// }
/// ```
///
/// Adapters compose, because an adapter is a type: `Option<Millis>` reads an
/// `Option<Duration>` and `Vec<Millis>` reads a `Vec<Duration>`, each mirroring
/// the container's own [`Read`] impl. [`Same`](crate::Same) is the identity,
/// for a position that wants the type's own impl inside one that does not.
/// How a field of type `T` is written when its declaration names this adapter.
///
/// The writing half of [`ReadAs`], split from it for the reason [`Read`] and
/// [`Write`] are split: `'de` belongs to the read half alone.
/// How a map key of type `T` is read when its declaration names this adapter.
///
/// A key is not a value: it never passes through [`Read`] at all, because a
/// JSON key is always a string and a numeric key is parsed out of the quoted
/// text. So a key position takes its own adapter trait, and
/// `HashMap<KA, VA>` adapts a `HashMap<K, V>` by naming one for each half.
/// How a map key of type `T` is written when its declaration names this
/// adapter.
/// Field-by-field reading for a struct.
/// Field-by-field writing for a struct.
/// Element-by-element reading for a struct written as a JSON array.
///
/// The positional counterpart of [`ReadObject`]. There is no key to confirm,
/// because position *is* the key: element `i` is field `i`, and a document
/// holding some other number of them is an error rather than a struct with
/// defaults in the gaps.
/// Element-by-element writing for a struct written as a JSON array.
/// Variant-by-variant reading for an enum.
///
/// The counterpart of [`ReadObject`] for a type declared with
/// [`unit_enum!`](crate::unit_enum) or [`tagged_enum!`](crate::tagged_enum).
/// There are two methods because there are two forms on the wire, and
/// [`Parser::read_enum`] has already decided which one it is looking at: a
/// bare name reaches [`read_name`](Self::read_name), and the single key of an
/// object reaches [`read_payload`](Self::read_payload).
///
/// `index` is only the candidate the hash proposed, exactly as in
/// [`ReadObject::read_field`], so both must confirm the name with
/// [`Parser::match_key`] before doing anything else.
/// Variant-by-variant reading for an internally tagged enum.
///
/// The counterpart of [`ReadEnum`] for a type declared with
/// [`tagged_enum!`](crate::tagged_enum)`(.. as tag "..")`. There is one method
/// rather than two because there is one wire form: an object whose first
/// member is the tag, and whose remaining members are the variant's own.
///
/// [`Parser::read_internally_tagged`] has already matched the tag and is
/// sitting on the first byte of its value, so what reaches
/// [`read_variant`](Self::read_variant) is the variant name and the rest of an
/// object still to be read.
/// Convenience bound for generic containers: readable from any JSON input, and
/// writable.
///
/// Types that borrow from the input do not satisfy this, exactly as they do
/// not satisfy an "owned" bound elsewhere in the ecosystem. Prefer
/// [`crate::ReadWrite`], which also covers BEVE, unless the type is deliberately
/// JSON only.
/// Convenience bound for a function that parses a `T` out of a document it
/// owns: readable from any JSON input, and constructible.
///
/// The read half is higher-ranked. [`Read`] is parameterised by the document's
/// lifetime because a type is allowed to borrow out of the input, and a caller
/// that holds the buffer itself cannot allow that. Reading from a document of
/// *any* lifetime is how "borrows from no input" is spelled. That excludes `&'de str`,
/// [`Cow`](std::borrow::Cow) and [`Raw`](crate::json::Raw), and any type holding
/// one of them.
///
/// The [`Default`] half is not decoration, and it is why this is two bounds
/// where other libraries have one. [`Read::read`] fills a value that already
/// exists, so anything that hands a `T` back has to build one first. That is
/// also the difference from [`ReadWrite`], which omits `Default` on purpose:
/// reading *into* a value constructs nothing, while producing one does.
///
/// This is the bound to write on a generic that owns its input, which is what
/// a web framework's extractor is:
///
/// ```
/// # struct Body(Vec<u8>);
/// fn parse<T: structio::json::ReadOwned>(body: Body) -> structio::Result<T> {
/// structio::json::from_slice(&body.0)
/// }
/// ```
///
/// [`from_str`](crate::json::from_str()) deliberately does not take this bound.
/// Its `T: Read<'de>` is tied to the input's lifetime so that a borrowing type
/// can be read from text the caller keeps. The owned bound belongs where the
/// buffer does not outlive the call, which is why [`from_reader`] carries it
/// and `from_str` does not.
///
/// One thing to expect from the compiler: a borrowing type is rejected during
/// region inference rather than trait solving, so the error reads
/// "implementation of `json::Read` is not general enough" and names neither
/// this trait nor the type parameter's bound. [`ReadWrite`] has the same edge
/// for the same reason. A missing [`Default`] is an ordinary unsatisfied
/// bound and does name this trait.
///
/// Prefer [`crate::ReadOwned`], which also covers BEVE, unless the type is
/// deliberately JSON only.
///
/// [`from_reader`]: crate::json::from_reader()