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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
//! JSON that goes through untouched.
//!
//! [`Raw`] is a field holding the text of one JSON value exactly as the
//! document spelled it, which it writes back out the same way. Nothing in it
//! is decoded and nothing is re-encoded: a number keeps the spelling it
//! arrived with, an integer literal wider than any type here keeps every
//! digit, an object keeps the key order its producer chose, and a string keeps
//! its escapes. A gateway that forwards a body it does not reshape forwards
//! the body that arrived, and a JSON-RPC `params` reaches the handler that
//! understands it as the caller wrote it.
//!
//! [`Value`](crate::Value) is the other destination for a value with no
//! declared type, and is not a substitute for this one. It is a tree, so it
//! respells its numbers through this crate's formatters, decodes an escape to
//! the character it stood for, and has nowhere to put an integer literal past
//! the width it stores. Those are the right properties for a value you are
//! going to *look at*, and the wrong ones for a value you are going to hand on
//! unchanged. Reach for `Value` to walk a document and for `Raw` to carry one.
//!
//! This is JSON only, which is why it lives here rather than at the crate root
//! among the format-agnostic names. What a `Raw` holds is JSON text, and BEVE
//! has no encoding for that: written as BEVE it would have to become either a
//! string carrying a document or a re-encoding of the value it stands for, and
//! neither is what a caller reaching for a passthrough asked for. A struct
//! with a `Raw` field is therefore declared with
//! [`json_object!`](crate::json_object), which exists for exactly the type
//! that only one format can describe.
//!
//! ```
//! use structio::json::Raw;
//!
//! #[derive(Default)]
//! struct Envelope<'a> {
//! id: u32,
//! payload: Raw<'a>,
//! }
//! structio::json_object!(['a] Envelope<'a> { id, payload });
//!
//! // `1.50` keeps its trailing zero and `b` stays behind `a`, because
//! // neither was ever read.
//! let text = r#"{"id":7,"payload":{"b":1.50,"a":[1,2]}}"#;
//! let envelope: Envelope = structio::from_str(text).unwrap();
//!
//! assert_eq!(envelope.payload.as_str(), r#"{"b":1.50,"a":[1,2]}"#);
//! assert_eq!(structio::to_string(&envelope), text);
//! ```
use fmt;
use Cow;
use crate;
use crateParser;
use crate;
use crateWriter;
use crate;
use crateOptions;
use cratefind_byte;
/// JSON's way of spelling "no value here", and what a `Raw` holds until
/// something puts a value in it. See [`Raw::default`].
const NULL: &str = "null";
/// One JSON value, kept as the text that spelled it.
///
/// The span is exactly one value with no whitespace on either side, which is
/// what lets a `Raw` stand anywhere a value stands: a member of an object, an
/// element of an array, or a whole document on its own.
///
/// Reading borrows out of the input under the default policy: the value is
/// stepped over rather than decoded, nothing inside it is converted, and the
/// field is a subslice of the document. Writing is then a copy of that run of
/// bytes. The owning form exists for the value that has to outlive its
/// document, through [`into_owned`](Self::into_owned), and for the span that
/// had a comment taken out of it and so is no longer any run of the input; see
/// the [`Read`] impl for both, and for exactly which spans those are.
///
/// The type deliberately has no `From<&str>`, no `From<String>` and no
/// `FromStr`. Each would be a conversion that looks total and is not:
/// [`new`](Self::new) and [`from_string`](Self::from_string) can fail, and the
/// unchecked ways in are [`new_unchecked`](Self::new_unchecked) and
/// [`from_string_unchecked`](Self::from_string_unchecked), spelled that way so
/// that a span nobody validated is visible at the call site rather than hidden
/// behind an `into()`.
;
/// Borrow the value's bytes straight out of the document.
///
/// The span is found by stepping over the value rather than by reading it, so
/// a `Raw` member costs a parse roughly what an unknown key costs: one walk to
/// find where the value ends, and then a slice of the input between the two
/// offsets that walk left. Nothing inside the value is converted, nothing is
/// copied, and under the default policy nothing is allocated, the field being
/// a subslice of the input.
///
/// The walk checks what [`Raw::new`] checks, so a document is refused or not
/// whatever type its value lands in, the number grammar aside. The one thing
/// it does that skipping an unknown key does not is decode each escape it
/// meets and throw the character away, because a skipped value is discarded
/// and this one is kept and written out again. Only a string holding a
/// backslash pays for that.
///
/// Under [`ALLOW_COMMENTS`](crate::Options::ALLOW_COMMENTS) the span may hold
/// `//` and `/* */`. The document was allowed to carry those; the output is
/// not, a comment being no part of what any writer here can emit, and a
/// forwarded body carrying one would be refused by the next plain JSON reader
/// that saw it. So that policy, and only that policy, searches the span for a
/// `/`, and runs it through the [minifier](crate::json::minify_with), which
/// already strips comments under it, only when it finds one.
///
/// **What that means for the bytes, exactly.** A span with no `/` in it holds
/// no comment, so it is borrowed just as the default policy borrows it: the
/// same bytes, interior whitespace and all. A span with a `/` in it is
/// minified into an owned string, which moves the whitespace between its
/// tokens but never the tokens themselves, so key order, number spellings and
/// escapes still come through as the document spelled them. The search is
/// conservative in the direction that costs nothing: a `/` inside a string,
/// as in `"http://x"`, buys a minify the span did not need, while a `/` that
/// is absent is proof there was nothing to strip.
///
/// The test is `O::ALLOW_COMMENTS`, a compile-time constant, so the default
/// policy compiles to the borrow with neither the search nor the stripping
/// present.
/// Write the span back out.
///
/// Compact, which is the default, that is one copy of the bytes that arrived
/// and nothing else. Byte-for-byte preservation is the whole point of the
/// type, so the bytes are not looked at, let alone reformatted.
///
/// Under [`PRETTY`](crate::Options::PRETTY) they are laid out again at the
/// writer's current depth, through the same walk
/// [`prettify`](crate::prettify) uses. A forwarded value is usually the one
/// part of a document that did not come from a writer here, and emitting it
/// verbatim would wedge an unindented blob between indented neighbours, which
/// is what Glaze does and is jarring exactly where a human is reading. Reusing
/// that walk rather than writing a second one is also what keeps the promise
/// its module docs make: the output is byte-identical to what writing the same
/// data under the same policy produces, because there is one set of layout
/// rules rather than two. Tokens are still copied through as the input spelled
/// them, so only the whitespace between them is this crate's and the
/// passthrough guarantee survives the layout.
///
/// A span that is not one complete JSON value can only come from
/// [`new_unchecked`](Raw::new_unchecked), and is written verbatim under either
/// policy. `write` returns `()` and has no way to report the problem, so the
/// choice is between laying out as much as parses and emitting what the caller
/// supplied; emitting it is what the compact path would have done, which makes
/// an invalid span behave the same way whichever policy is in force. The
/// layout walk therefore settles the span's structure before it emits a byte,
/// rather than starting and discovering the problem halfway: a writer has no
/// way to take output back, so a fallback after a partial layout would write
/// the value twice.
/// The half-open span of `s` that is the value: one whole JSON value, with the
/// whitespace on either side of it walked past.
///
/// The one walk behind both checked ways in, so what
/// [`Raw::from_string`] accepts is what [`Raw::new`] accepts by construction
/// rather than by two copies of a check staying in step.
///
/// `start` follows a run of whitespace and `end` is where stepping over the
/// value stopped, so both are token boundaries and neither can be inside a
/// character. That is what lets a caller slice on them, or move bytes between
/// them, without checking again.
/// Lay `span` out into `w` at the writer's current depth, or answer `false`
/// without having written anything.
///
/// Two passes, because the second one cannot be taken back. The probe walks
/// the span without emitting, and decides the question the layout walk would
/// otherwise decide halfway through it. Everything the probe checks,
/// [`prettify_value_into`] checks the same way and with the same limits, being
/// the same parser under the same policy, so a span the probe accepts is one
/// the layout lays out to the end.
///
/// The cost is a second structural walk, and it is paid only under
/// [`Options::PRETTY`], by a caller who has already asked for the expensive
/// layout of the whole document. The compact path never reaches here.
///
/// The layout itself is the public call rather than anything private to the
/// crate, so the entry point an outside implementer of a passthrough type has
/// is the one this type exercises on every pretty write.