rusty_rtos_json-core 0.1.0

The pure no_std core of rusty_rtos_json: types, traits and algorithms with no CPU, no allocator and no operating system. forbid(unsafe).
Documentation
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
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
//! `JSON_Validate`: coreJSON's strict ECMA-404 validator, over a byte slice.
//!
//! A transcription of `core_json.c` v3.3.1 (`cffa492`), which is a ladder of
//! `skip*` scanners each advancing a cursor. The shape is kept because the
//! shape IS the specification: coreJSON's acceptances and rejections fall out
//! of exactly where each scanner gives up, and a "tidier" parser would be a
//! different parser.
//!
//! Zero allocation, no recursion, `no_std`, `forbid(unsafe)`. Nesting is
//! bounded by an explicit stack of [`MAX_DEPTH`], as the C bounds it.
//!
//! # Measured before it was written
//!
//! coreJSON scores **100 %** on JSONTestSuite: 95 of 95 `y_` files accepted,
//! 188 of 188 `n_` files rejected. So "agree with the C" and "pass the suite"
//! are the same target here, which was worth establishing rather than
//! assuming — if the C had failed the suite anywhere, those two goals would
//! have pulled apart and one of them would have had to give.
//!
//! The 35 `i_` files, where the standard leaves the answer to the
//! implementation, are the interesting ones: coreJSON accepts 10 and rejects
//! 25. Being *a* JSON parser does not determine those; being *coreJSON* does,
//! and they are where a transcription drifts first.

/// `JSON_MAX_DEPTH`. The C's default, and the reason there is no recursion.
pub const MAX_DEPTH: usize = 32;

/// `JSONStatus_t`, for the subset [`validate`] can answer with.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Validity {
    /// `JSONSuccess`: valid and complete.
    Valid,
    /// `JSONIllegalDocument`: invalid or malformed.
    Illegal,
    /// `JSONMaxDepthExceeded`.
    MaxDepthExceeded,
    /// `JSONPartial`: valid so far, but the buffer ends mid-document.
    Partial,
    /// `JSONBadParameter`: an empty buffer.
    BadParameter,
}

// ---- the character classes, which are the C's macros ---------------------

/// `isspace_`, and it is JSON's whitespace rather than C's: space, tab,
/// newline and carriage return only. A vertical tab or form feed is NOT
/// whitespace to ECMA-404, and `isspace()` from `<ctype.h>` would say it was.
const fn is_space(c: u8) -> bool {
    c == b' ' || c == b'\t' || c == b'\n' || c == b'\r'
}

/// `isascii_`.
const fn is_ascii(c: u8) -> bool {
    c <= 0x7F
}

/// `iscntrl_`: an ASCII control character, which a string may not contain
/// unescaped.
const fn is_cntrl(c: u8) -> bool {
    is_ascii(c) && c < b' '
}

/// `isdigit_`.
const fn is_digit(c: u8) -> bool {
    c.is_ascii_digit()
}

const fn is_open_bracket(c: u8) -> bool {
    c == b'{' || c == b'['
}

const fn is_close_bracket(c: u8) -> bool {
    c == b'}' || c == b']'
}

/// `isMatchingBracket_`.
const fn is_matching_bracket(open: u8, close: u8) -> bool {
    (open == b'{' && close == b'}') || (open == b'[' && close == b']')
}

/// The byte at `i`, or `None` past the end. Every scanner reads through this,
/// so "past the end" is a value rather than a panic.
///
/// # This is defence in depth, and it was measured
///
/// Replacing the body with `Some(buf[i])` -- a panic on any read past the end
/// -- leaves all eleven tests passing, including 20,000 fuzz documents, every
/// truncation of six valid documents, every single-byte corruption of a
/// seventh, and all 318 corpus files. So no caller reads out of bounds today:
/// every scanner establishes `i < max` first.
///
/// The `Option` stays anyway, because that property holds only while every
/// caller is right, and a scanner edited later would lose it silently. The two
/// places where the bound is genuinely load-bearing are
/// [`skip_literal`]'s slice and the depth stack in [`validate`]; poisoning
/// either one panics immediately.
pub(crate) fn at(buf: &[u8], i: usize) -> Option<u8> {
    buf.get(i).copied()
}

// ---- the scanners --------------------------------------------------------

/// `skipSpace`.
pub(crate) fn skip_space(buf: &[u8], start: &mut usize, max: usize) {
    let mut i = *start;
    while i < max {
        match at(buf, i) {
            Some(c) if is_space(c) => i = i.saturating_add(1),
            _ => break,
        }
    }
    *start = i;
}

/// `countHighBits`.
const fn count_high_bits(c: u8) -> usize {
    let mut n = c;
    let mut i = 0usize;
    while (n & 0x80) != 0 {
        i = i.saturating_add(1);
        n = (n & 0x7F) << 1;
    }
    i
}

/// `shortestUTF8`: is this the shortest encoding of this value, and is the
/// value a legal scalar?
///
/// Rejecting an over-long encoding is a security property, not a nicety: the
/// classic exploit is smuggling an ASCII character past a filter as a
/// two-byte sequence.
const fn shortest_utf8(length: usize, value: u32) -> bool {
    let (min, max) = match length {
        2 => (1u32 << 7, (1u32 << 11) - 1),
        3 => (1u32 << 11, (1u32 << 16) - 1),
        _ => (1u32 << 16, 0x10_FFFF),
    };
    value >= min && value <= max && (value < 0xD800 || value > 0xDFFF)
}

/// `skipUTF8MultiByte`.
fn skip_utf8_multibyte(buf: &[u8], start: &mut usize, max: usize) -> bool {
    let mut i = *start;
    let Some(first) = at(buf, i) else {
        return false;
    };
    // `> 0xC1` rejects the two over-long lead bytes; `< 0xF5` rejects
    // anything above U+10FFFF.
    if !(first > 0xC1 && first < 0xF5) {
        return false;
    }

    let bit_count = count_high_bits(first);
    let shift = 7usize.saturating_sub(bit_count);
    let mut value = u32::from(first) & ((1u32 << shift).saturating_sub(1));

    // The bit count is one greater than the number of continuation bytes.
    let mut j = bit_count.saturating_sub(1);
    while j > 0 {
        i = i.saturating_add(1);
        if i >= max {
            break;
        }
        let Some(c) = at(buf, i) else { break };
        // Continuation bytes must match 10xxxxxx.
        if (c & 0xC0) != 0x80 {
            break;
        }
        value = (value << 6) | u32::from(c & 0x3F);
        j = j.saturating_sub(1);
    }

    if j == 0 && shortest_utf8(bit_count, value) {
        *start = i.saturating_add(1);
        return true;
    }
    false
}

/// `skipUTF8`.
fn skip_utf8(buf: &[u8], start: &mut usize, max: usize) -> bool {
    if *start >= max {
        return false;
    }
    match at(buf, *start) {
        Some(c) if is_ascii(c) => {
            *start = start.saturating_add(1);
            true
        }
        Some(_) => skip_utf8_multibyte(buf, start, max),
        None => false,
    }
}

/// `NOT_A_HEX_CHAR`.
const NOT_A_HEX_CHAR: u8 = 0x10;

/// `hexToInt`.
const fn hex_to_int(c: u8) -> u8 {
    // Each arm's own range is what makes its arithmetic total: the
    // subtraction cannot go below zero and the sum cannot pass 15. The
    // saturating forms say so to the compiler as well as to the reader.
    match c {
        b'a'..=b'f' => c.saturating_sub(b'a').saturating_add(10),
        b'A'..=b'F' => c.saturating_sub(b'A').saturating_add(10),
        b'0'..=b'9' => c.saturating_sub(b'0'),
        _ => NOT_A_HEX_CHAR,
    }
}

/// `skipOneHexEscape`: `\uXXXX`.
fn skip_one_hex_escape(buf: &[u8], start: &mut usize, max: usize, out: &mut u16) -> bool {
    let mut i = *start;
    // `HEX_ESCAPE_LENGTH` is 6: the backslash, the `u`, and four digits.
    let end = i.saturating_add(6);
    let mut value = 0u16;

    // `end < max` and NOT `<=`, which is the C's. It means the escape must be
    // followed by at least one more byte -- and it always is, because a string
    // still needs its closing quote.
    if end > i
        && end < max
        && at(buf, i) == Some(b'\\')
        && at(buf, i.saturating_add(1)) == Some(b'u')
    {
        i = i.saturating_add(2);
        while i < end {
            let Some(c) = at(buf, i) else { break };
            let n = hex_to_int(c);
            if n == NOT_A_HEX_CHAR {
                break;
            }
            value = (value << 4) | u16::from(n);
            i = i.saturating_add(1);
        }
    }

    if i == end {
        *out = value;
        *start = i;
        return true;
    }
    false
}

const fn is_high_surrogate(x: u16) -> bool {
    x >= 0xD800 && x <= 0xDBFF
}

const fn is_low_surrogate(x: u16) -> bool {
    x >= 0xDC00 && x <= 0xDFFF
}

/// `skipHexEscape`: one escape, or a surrogate PAIR.
///
/// A high surrogate must be followed by a low one, and a lone low surrogate is
/// refused outright. That is stricter than "any four hex digits" and it is
/// where several `i_` files in JSONTestSuite land.
fn skip_hex_escape(buf: &[u8], start: &mut usize, max: usize) -> bool {
    let mut i = *start;
    let mut value = 0u16;
    let mut ret = false;

    if skip_one_hex_escape(buf, &mut i, max, &mut value) {
        if is_high_surrogate(value) {
            if skip_one_hex_escape(buf, &mut i, max, &mut value) && is_low_surrogate(value) {
                ret = true;
            }
        } else if is_low_surrogate(value) {
            // A premature low surrogate: refused.
        } else {
            ret = true;
        }
    }

    if ret {
        *start = i;
    }
    ret
}

/// `skipEscape`.
fn skip_escape(buf: &[u8], start: &mut usize, max: usize) -> bool {
    let mut i = *start;
    let mut ret = false;

    // `i < max - 1`: an escape needs at least one byte after the backslash.
    if i < max.saturating_sub(1) && at(buf, i) == Some(b'\\') {
        match at(buf, i.saturating_add(1)) {
            Some(b'u') => ret = skip_hex_escape(buf, &mut i, max),
            Some(b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't') => {
                i = i.saturating_add(2);
                ret = true;
            }
            _ => {}
        }
    }

    if ret {
        *start = i;
    }
    ret
}

/// `skipString`.
pub(crate) fn skip_string(buf: &[u8], start: &mut usize, max: usize) -> bool {
    let mut i = *start;
    let mut ret = false;

    if i < max && at(buf, i) == Some(b'"') {
        i = i.saturating_add(1);
        while i < max {
            match at(buf, i) {
                Some(b'"') => {
                    ret = true;
                    i = i.saturating_add(1);
                    break;
                }
                Some(b'\\') => {
                    if !skip_escape(buf, &mut i, max) {
                        break;
                    }
                }
                // An unescaped control character is not allowed.
                Some(c) if is_cntrl(c) => break,
                Some(_) => {
                    if !skip_utf8(buf, &mut i, max) {
                        break;
                    }
                }
                None => break,
            }
        }
    }

    if ret {
        *start = i;
    }
    ret
}

/// `skipLiteral`.
fn skip_literal(buf: &[u8], start: &mut usize, max: usize, literal: &[u8]) -> bool {
    let from = *start;
    if from >= max || literal.len() > max.saturating_sub(from) {
        return false;
    }
    let end = from.saturating_add(literal.len());
    let Some(slice) = buf.get(from..end) else {
        return false;
    };
    if slice == literal {
        *start = end;
        return true;
    }
    false
}

/// `skipAnyLiteral`: `true`, `false` or `null`.
fn skip_any_literal(buf: &[u8], start: &mut usize, max: usize) -> bool {
    skip_literal(buf, start, max, b"true")
        || skip_literal(buf, start, max, b"false")
        || skip_literal(buf, start, max, b"null")
}

/// The largest value an array index may reach: `MAX_INDEX_VALUE`, which the
/// C's header defines as `0x7FFFFFF7`, or 2^31 - 9.
pub(crate) const MAX_INDEX_VALUE: i32 = 0x7FFF_FFF7;

/// `MAX_FACTOR`: the largest accumulator that can still take another digit.
const MAX_FACTOR: i32 = MAX_INDEX_VALUE / 10;

/// `skipDigits`, including the `outValue` half.
///
/// [`validate`] never asks for the value, but `multiSearch` reads an array
/// index with it, and the overflow behaviour is load-bearing there: once the
/// accumulator passes `MAX_FACTOR` the C latches it to **-1** and stops
/// accumulating, and a negative index is what makes `[99999999999999999999]`
/// a `BadParameter` rather than a wrapped-around lookup.
///
/// Returns `None` when there were no digits at all (the C's `false`), and
/// otherwise the accumulated value, which may be -1.
pub(crate) fn skip_digits_value(buf: &[u8], start: &mut usize, max: usize) -> Option<i32> {
    let from = *start;
    let mut i = from;
    let mut value: i32 = 0;

    while i < max {
        let Some(c) = at(buf, i) else { break };
        if !is_digit(c) {
            break;
        }

        if value > -1 {
            let n = i32::from(hex_to_int(c));
            value = if value <= MAX_FACTOR {
                value.saturating_mul(10).saturating_add(n)
            } else {
                -1
            };
        }

        i = i.saturating_add(1);
    }

    if i > from {
        *start = i;
        return Some(value);
    }
    None
}

/// `skipDigits` with `outValue == NULL`. The C skips only the accumulation,
/// never the scan, so this is the same walk with the answer thrown away.
fn skip_digits(buf: &[u8], start: &mut usize, max: usize) -> bool {
    skip_digits_value(buf, start, max).is_some()
}

/// `skipDecimals`. Note it advances `start` only when digits FOLLOW the dot,
/// so `1.` leaves the cursor on the dot and the document fails later.
fn skip_decimals(buf: &[u8], start: &mut usize, max: usize) {
    let mut i = *start;
    if i < max && at(buf, i) == Some(b'.') {
        i = i.saturating_add(1);
        if skip_digits(buf, &mut i, max) {
            *start = i;
        }
    }
}

/// `skipExponent`. Same shape as [`skip_decimals`]: a bare `e` does not
/// advance, so `1e` fails.
fn skip_exponent(buf: &[u8], start: &mut usize, max: usize) {
    let mut i = *start;
    if i < max && matches!(at(buf, i), Some(b'e' | b'E')) {
        i = i.saturating_add(1);
        if i < max && matches!(at(buf, i), Some(b'-' | b'+')) {
            i = i.saturating_add(1);
        }
        if skip_digits(buf, &mut i, max) {
            *start = i;
        }
    }
}

/// `skipNumber`.
fn skip_number(buf: &[u8], start: &mut usize, max: usize) -> bool {
    let mut i = *start;
    let mut ret = false;

    if i < max && at(buf, i) == Some(b'-') {
        i = i.saturating_add(1);
    }

    if i < max {
        // JSON disallows superfluous leading zeroes, so a leading zero is a
        // whole integer part on its own.
        if at(buf, i) == Some(b'0') {
            ret = true;
            i = i.saturating_add(1);
        } else {
            ret = skip_digits(buf, &mut i, max);
        }
    }

    if ret {
        skip_decimals(buf, &mut i, max);
        skip_exponent(buf, &mut i, max);
        *start = i;
    }
    ret
}

/// `skipAnyScalar`.
pub(crate) fn skip_any_scalar(buf: &[u8], start: &mut usize, max: usize) -> bool {
    skip_string(buf, start, max)
        || skip_any_literal(buf, start, max)
        || skip_number(buf, start, max)
}

/// `skipSpaceAndComma`: true only when a comma is followed by more content.
///
/// A comma before a closing bracket answers false, which is what makes a
/// trailing comma illegal.
pub(crate) fn skip_space_and_comma(buf: &[u8], start: &mut usize, max: usize) -> bool {
    skip_space(buf, start, max);
    let mut i = *start;
    if i < max && at(buf, i) == Some(b',') {
        i = i.saturating_add(1);
        skip_space(buf, &mut i, max);
        if i < max && !at(buf, i).is_some_and(is_close_bracket) {
            *start = i;
            return true;
        }
    }
    false
}

/// `skipArrayScalars`.
fn skip_array_scalars(buf: &[u8], start: &mut usize, max: usize) -> bool {
    let mut i = *start;
    let mut ret = true;

    while i < max {
        if !skip_any_scalar(buf, &mut i, max) {
            break;
        }
        if !skip_space_and_comma(buf, &mut i, max) {
            // After a scalar there must be either a comma with more content,
            // or the closing bracket.
            if i >= max || at(buf, i) != Some(b']') {
                ret = false;
            }
            break;
        }
    }

    *start = i;
    ret
}

/// `skipObjectScalars`.
fn skip_object_scalars(buf: &[u8], start: &mut usize, max: usize) -> bool {
    let mut i = *start;
    let mut ret = true;

    while i < max {
        if !skip_string(buf, &mut i, max) {
            ret = false;
            break;
        }
        skip_space(buf, &mut i, max);
        if i < max && at(buf, i) != Some(b':') {
            ret = false;
            break;
        }
        i = i.saturating_add(1);
        skip_space(buf, &mut i, max);

        // A nested collection: hand back to `skipCollection`.
        if i < max && at(buf, i).is_some_and(is_open_bracket) {
            *start = i;
            break;
        }
        if !skip_any_scalar(buf, &mut i, max) {
            ret = false;
            break;
        }

        let comma = skip_space_and_comma(buf, &mut i, max);
        *start = i;
        if !comma {
            break;
        }
    }

    ret
}

/// `skipScalars`.
fn skip_scalars(buf: &[u8], start: &mut usize, max: usize, mode: u8) -> bool {
    skip_space(buf, start, max);
    let i = *start;
    if i >= max {
        return true;
    }
    let Some(c) = at(buf, i) else { return true };

    if mode == b'[' {
        if c != b']' {
            return skip_array_scalars(buf, start, max);
        }
    } else if c != b'}' {
        return skip_object_scalars(buf, start, max);
    }
    true
}

/// `skipCollection`: the explicit stack that replaces recursion.
pub(crate) fn skip_collection(buf: &[u8], start: &mut usize, max: usize) -> Validity {
    let mut ret = Validity::Partial;
    let mut stack = [0u8; MAX_DEPTH];
    // `int16_t depth = -1`, as an Option so there is no negative index.
    let mut depth: Option<usize> = None;
    let mut i = *start;

    while i < max {
        let Some(c) = at(buf, i) else { break };
        i = i.saturating_add(1);

        match c {
            b'{' | b'[' => {
                let next = match depth {
                    None => 0usize,
                    Some(d) => d.saturating_add(1),
                };
                if next >= MAX_DEPTH {
                    ret = Validity::MaxDepthExceeded;
                } else {
                    depth = Some(next);
                    if let Some(slot) = stack.get_mut(next) {
                        *slot = c;
                    }
                    if !skip_scalars(buf, &mut i, max, c) {
                        ret = Validity::Illegal;
                    }
                }
            }
            b'}' | b']' => {
                let d = depth.unwrap_or(0);
                let open = stack.get(d).copied().unwrap_or(0);
                // The nested case: close one level and carry on.
                if depth.is_some_and(|d| d > 0) && d < MAX_DEPTH && is_matching_bracket(open, c) {
                    let outer = d.saturating_sub(1);
                    depth = Some(outer);
                    let mode = stack.get(outer).copied().unwrap_or(0);
                    if skip_space_and_comma(buf, &mut i, max) {
                        if !skip_scalars(buf, &mut i, max, mode) {
                            ret = Validity::Illegal;
                        }
                    } else if i < max && !at(buf, i).is_some_and(|n| is_matching_bracket(mode, n)) {
                        // After closing a nested collection with no comma, the
                        // only legal next byte is the outer collection's own
                        // close.
                        ret = Validity::Illegal;
                    }
                } else {
                    ret = if depth == Some(0) && is_matching_bracket(open, c) {
                        Validity::Valid
                    } else {
                        Validity::Illegal
                    };
                }
            }
            _ => ret = Validity::Illegal,
        }

        if ret != Validity::Partial {
            break;
        }
    }

    if ret == Validity::Valid {
        *start = i;
    }
    ret
}

/// `JSON_Validate`: is this a complete, valid JSON document?
///
/// Strict ECMA-404, as coreJSON is strict: no trailing commas, no leading
/// zeroes, no unescaped control characters in strings, no over-long UTF-8, no
/// lone surrogates, and nesting no deeper than [`MAX_DEPTH`].
///
/// A **scalar at the top level is valid** — `42` and `"hi"` are documents —
/// which is ECMA-404 and not the older RFC 4627 rule that a document must be
/// an object or an array. The C makes that switchable with
/// `JSON_VALIDATE_COLLECTIONS_ONLY`; this follows the default.
#[must_use]
pub fn validate(buf: &[u8]) -> Validity {
    let max = buf.len();
    if max == 0 {
        // `JSONBadParameter` for `max == 0`. There is no null pointer to
        // report here, which is the one status this port cannot produce.
        return Validity::BadParameter;
    }

    let mut i = 0usize;
    skip_space(buf, &mut i, max);

    let mut ret = if skip_any_scalar(buf, &mut i, max) {
        Validity::Valid
    } else {
        skip_collection(buf, &mut i, max)
    };

    if ret == Validity::Valid && i < max {
        skip_space(buf, &mut i, max);
        if i != max {
            ret = Validity::Illegal;
        }
    }

    ret
}

/// Whether [`validate`] accepts this document.
///
/// The convenience the differential and the test suite both want: coreJSON
/// distinguishes four kinds of "no", and a corpus only asks "yes or no".
#[must_use]
pub fn is_valid(buf: &[u8]) -> bool {
    validate(buf) == Validity::Valid
}