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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
//! The traits and constants that do not belong to any one format.
//!
//! A struct's *schema* is format independent: the same field list, in the same
//! order, under the same keys, whether it is going out as JSON text or BEVE
//! binary. That schema is [`Keys`], and the [`object!`](crate::object) macro
//! generates it once per type. A struct declared positionally has no keys to
//! share, only a length: that is [`Elements`], from [`array!`](crate::array).
//! An enum shares its variant names, which is [`Variants`], from
//! [`unit_enum!`](crate::unit_enum) and [`tagged_enum!`](crate::tagged_enum).
//!
//! Everything downstream of the schema is format specific and lives in
//! [`json`](crate::json) and [`beve`](crate::beve): each has its own `Read`,
//! `Write`, `ReadObject`, and `WriteObject`.
use PhantomData;
use crateKeyMap;
use crateOptions;
/// The key schema of a struct, and its compile-time perfect hash.
///
/// Shared by every format, because the keys are a property of the type rather
/// than of the encoding. JSON looks a key up out of a quoted run of document
/// bytes and BEVE out of a length-prefixed one, but both land in the same
/// table and yield the same field index.
/// The variant names of an enum, and their compile-time perfect hash.
///
/// The enum counterpart of [`Keys`], and shared by every format for the same
/// reason: which variants there are and what they are called is a property of
/// the type, not of the encoding. A name goes out as a JSON string or as a
/// BEVE one, but both land in the same table and yield the same variant index.
///
/// Generated by [`unit_enum!`](crate::unit_enum) and
/// [`tagged_enum!`](crate::tagged_enum).
/// Put a key index back on the field it fills.
///
/// The hash indexes [`Keys::KEYS`], which holds the fields and then the
/// aliases, so a member written under an alias comes back with an index past
/// the last field. Every reader resolves it here, once, before the generated
/// dispatch sees it. Two things fall out of doing it in one place: the
/// dispatch has an arm per field rather than per key, and the seen mask that
/// [`Keys::REQUIRED`] is checked against gets the field's bit, so a required
/// member supplied under an alias is a member supplied.
///
/// [`ALIASES`](Keys::ALIASES) is empty wherever a schema declares no alias,
/// which is a constant, so this is gone from the reader entirely.
pub
/// [`resolve_key`] for an enum's variant names.
pub
/// The bookkeeping behind [`Options::ERROR_ON_MISSING_KEYS`] and
/// [`Keys::REQUIRED`], and the one place a struct too wide for the first is
/// refused.
///
/// An object reader sets bit `i` when it fills field `i`; an object that ends
/// holding anything less than [`MASK`](Fields::MASK) left a member out that
/// either the policy or the type insisted on.
pub >);
/// Convenience bound for generic containers: readable and writable in every
/// format this crate supports, from any input.
///
/// [`object!`](crate::object) generates impls for all formats at once, so a
/// generic struct's type parameter needs all of them. This is the bound to
/// write:
///
/// ```
/// #[derive(Default)]
/// struct Page<T> {
/// items: Vec<T>,
/// cursor: Option<String>,
/// }
/// structio::object!([T: structio::ReadWrite + Default] Page<T> { items, cursor });
/// ```
///
/// `Default` is separate because [`ReadWrite`] does not imply it: reading
/// *into* a `Page<T>` constructs nothing, but reading one builds a `T` per
/// element of `items`. Where a bound has to produce the value rather than fill
/// it, [`ReadOwned`] folds the two together.
///
/// Types that borrow from the input do not satisfy this, exactly as they do
/// not satisfy an "owned" bound elsewhere in the ecosystem. For a struct that
/// is only ever used with one format, the narrower [`json::ReadWrite`] or
/// [`beve::ReadWrite`] will do.
///
/// [`json::ReadWrite`]: crate::json::ReadWrite
/// [`beve::ReadWrite`]: crate::beve::ReadWrite
/// Convenience bound for generic containers that are only ever written:
/// writable in every format this crate supports.
///
/// The write-only counterpart of [`ReadWrite`]. A declaration that leads with
/// `write_only` generates no read impls, so a generic one bounds its type
/// parameters by this instead, and a type parameter of such a declaration
/// needs no `Default` either: nothing constructs a value it would have to fill.
///
/// ```
/// struct Sample<T> {
/// value: T,
/// }
/// structio::object!(write_only [T: structio::Write] Sample<T> { value });
/// ```
///
/// For a type that is only ever written in one format, the narrower
/// [`json::Write`] or [`beve::Write`] will do.
///
/// Unlike [`ReadWrite`] this does not require `Sized`: `str` and `[u8]` are
/// writable, and it is reading that has to have somewhere to put the value.
///
/// There is no bare `Read` counterpart at this level. The direction axis
/// narrows only to the write half, because a read bound has to say which
/// lifetime it reads at; [`ReadOwned`] is the read-side bound, for the case
/// where the answer is "any".
///
/// [`json::Write`]: crate::json::Write
/// [`beve::Write`]: crate::beve::Write
/// Convenience bound for a function that parses a `T` out of a document it
/// owns: readable in every format this crate supports, from any input, and
/// constructible.
///
/// The read-side counterpart of [`ReadWrite`], and the bound for a generic that
/// does not learn until run time which format it was handed:
///
/// ```
/// fn decode<T: structio::ReadOwned>(json: bool, body: &[u8]) -> structio::Result<T> {
/// if json {
/// structio::json::from_slice(body)
/// } else {
/// structio::beve::from_slice(body)
/// }
/// }
/// ```
///
/// That is the shape worth spending both halves on. A generic that reads one
/// format should take the narrower [`json::ReadOwned`] or [`beve::ReadOwned`]
/// rather than demand an impl it never uses.
///
/// [`json::ReadOwned`] carries the full account of why the read half is
/// higher-ranked and why [`Default`] is part of the bound.
///
/// [`json::ReadOwned`]: crate::json::ReadOwned
/// [`beve::ReadOwned`]: crate::beve::ReadOwned
/// The length of a struct encoded as a positional array.
///
/// The array counterpart of [`Keys`], and shared by every format for the same
/// reason: which fields there are and what order they come in is a property of
/// the type, not of the encoding. JSON writes them between brackets and BEVE
/// behind a generic-array header, but both write the same values in the same
/// order, and both refuse a document that holds a different number of them.
///
/// Generated by [`array!`](crate::array), which unlike [`object!`](crate::object)
/// emits no key list and no hash table: an element is found by counting, so
/// there is nothing to look up.
// A tuple is an array-encoded struct whose fields happen to have no names, so
// it reaches the same drivers through the same trait.
impl_tuple_elements!;
impl_tuple_elements!;
impl_tuple_elements!;
impl_tuple_elements!;
impl_tuple_elements!;
impl_tuple_elements!;
impl_tuple_elements!;
impl_tuple_elements!;
impl_tuple_elements!;
impl_tuple_elements!;
impl_tuple_elements!;
impl_tuple_elements!;
/// The identity adapter: read and write this position the way the type itself
/// would.
///
/// Adapters compose as types do, so a container adapter needs something to
/// name at a position that wants no adapting. `Vec<Same>` reads a `Vec<T>`
/// element for element as `Vec<T>`'s own impl does, and
/// `HashMap<Same, Millis>` adapts only a map's values, leaving its keys to
/// [`FromJsonKey`](crate::json::FromJsonKey) and
/// [`FromBeveKey`](crate::beve::FromBeveKey).
///
/// One type rather than one per format, because a declaration names a single
/// adapter and the macro emits it against [`json::ReadAs`](crate::json::ReadAs),
/// [`json::WriteAs`](crate::json::WriteAs),
/// [`beve::ReadAs`](crate::beve::ReadAs) and
/// [`beve::WriteAs`](crate::beve::WriteAs) alike. A per-format `Same` could
/// not be written at a field at all.
///
/// It is an identity on the bytes too, not only on the values, and in both
/// directions: `Same` forwards BEVE's
/// [`Write::ARRAY`](crate::beve::Write::ARRAY), so a `Vec<Same>` over a
/// `Vec<f64>` is still one typed array rather than a value per element, and it
/// forwards [`Read::read_bulk`](crate::beve::Read::read_bulk), so reading that
/// array back is still the single `memcpy` the unadapted field would have got.
///
/// Neither is true of an adapter in general, and neither should be. An adapter
/// with a conversion to do has no block to copy, so it leaves both alone and
/// gets a generic array and an element-by-element read. What `Same` shows is
/// that the ceiling is the adapter's, not the mechanism's: an adapter over a
/// type whose memory is already a payload can reach the same two paths, which
/// is what [`NumericBytes`](crate::beve::NumericBytes) is implementable for.
;
/// Refuse an internally tagged declaration whose tag is also a field of the
/// variant's payload.
///
/// The two share one object, so a collision writes the name twice:
/// `{"kind":"Config","kind":"debug"}`. This crate reads that back, taking the
/// first member as the tag, and a last-wins parser does not: it resolves the
/// name to the payload's value and the variant is gone. The field is
/// unreadable here as well, the tag having been consumed before the payload's
/// members are reached, so the configuration is dead in both directions rather
/// than merely unwise.
///
/// `ctor` is never called. A tuple variant's constructor is a value of type
/// `fn(P) -> E`, so naming it is what tells this function what `P` is: the
/// payload's type is deliberately absent from the declaration, and the
/// constructor is the one place the macro can reach it without asking for it
/// twice. `tag` is an ordinary argument rather than an associated constant
/// because the call sites are const contexts holding the literal, which is
/// what keeps this free of any trait the enum would have to implement.
///
/// [`tagged_enum!`](crate::tagged_enum) calls it, for a declaration carrying a
/// tag clause, from an item-level `const`, so with no generics it is evaluated
/// by `cargo check`. A generic one has no keys until it is instantiated, and
/// is checked from the write path's own `const` block instead, which is
/// [`Keys::REQUIRED`]'s tier: reported when the crate is built.
///
/// Comparing bytes rather than `==`, `str` equality not being callable in a
/// const context on this crate's minimum compiler.
pub const