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
//! `JSON_SearchConst` and `JSON_Iterate`: the query half of coreJSON.
//!
//! The validator answers one question about a whole document. This answers
//! questions about its *parts*, and it does so **without building anything** —
//! no tree, no allocation, no copy. A query returns a sub-slice of the buffer
//! the caller already has, which is why coreJSON exists at all on a part with
//! 64 KiB of RAM.
//!
//! # Indices, not pointers
//!
//! The C hands back a `const char *` into the caller's buffer and a length.
//! Here the same answer is a `&[u8]` borrowed from the input, so the lifetime
//! that the C leaves to the reader's care is checked by the compiler instead.
//! Internally everything is an index, per the family rule: handles are
//! indices, never pointers.
//!
//! # Two refusal types rather than one
//!
//! The C has a single `JSONStatus_t` and each function documents the subset it
//! can return. [`search`] can only ever answer `NotFound` or `BadParameter`,
//! and [`iterate`] adds `IllegalDocument`, so they get different types and the
//! impossible variant is not there to be matched on. That is the same move the
//! validator makes with [`Validity`](crate::Validity), and the same one
//! `heap_1` makes by refusing to have a `free`.
//!
//! # This does NOT validate first
//!
//! Neither does the C. A query walks whatever bytes it is given, so a
//! malformed document produces a refusal or a partial answer rather than a
//! diagnosis. Call [`validate`](crate::validate) if you need one. The
//! differential leans on this deliberately: it runs both entry points over all
//! 188 deliberately-malformed corpus files, because walking a broken document
//! is exactly where a reimplementation runs off the end.

use crate::validate::{
    Validity, at, skip_any_scalar, skip_collection, skip_digits_value, skip_space,
    skip_space_and_comma, skip_string,
};

/// `JSON_QUERY_KEY_SEPARATOR`, the character between the parts of a query.
pub const QUERY_KEY_SEPARATOR: u8 = b'.';

/// `JSONTypes_t`, for the types a found value can actually have.
///
/// The C's enum also carries `JSONInvalid`, which is what its callers use to
/// initialise the out-parameter before the call. Nothing here ever returns it,
/// so it is not a variant: a [`Match`] always has a real type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
    /// A quoted string. The quotes are stripped from [`Match::value`].
    String,
    /// Any number, including negative and exponent forms.
    Number,
    /// `true`.
    True,
    /// `false`.
    False,
    /// `null`.
    Null,
    /// An object, reported whole, braces included.
    Object,
    /// An array, reported whole, brackets included.
    Array,
}

/// `getType`: the type of a value, from its first byte alone.
///
/// Anything that is not one of the six punctuation or literal starts is a
/// number — the C does not check, because by the time a value has been
/// scanned it is already known to be one of the seven.
const fn kind_of(c: u8) -> Kind {
    match c {
        b'"' => Kind::String,
        b'{' => Kind::Object,
        b'[' => Kind::Array,
        b't' => Kind::True,
        b'f' => Kind::False,
        b'n' => Kind::Null,
        _ => Kind::Number,
    }
}

/// Why a [`search`] found nothing.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NotFound {
    /// `JSONNotFound`: the document is walkable and the query is not in it.
    Missing,
    /// `JSONBadParameter`: an empty buffer, an empty query, a query with an
    /// empty part or a trailing separator, or an array index that is
    /// malformed or too large to be one.
    BadQuery,
}

/// Why an [`iterate`] stopped.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Stopped {
    /// `JSONNotFound`: the collection has no further values.
    Exhausted,
    /// `JSONIllegalDocument`: the buffer does not begin with `[` or `{`.
    NotACollection,
    /// `JSONBadParameter`: an empty buffer, or a cursor outside it.
    BadCursor,
}

/// A value found by [`search`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Match<'a> {
    /// Where the value starts in the buffer that was searched.
    ///
    /// For a [`Kind::String`] this is the first byte **inside** the quotes,
    /// matching the C, which advances the pointer and shortens the length by
    /// two so that a caller gets the text rather than the literal.
    pub offset: usize,
    /// The value itself, borrowed from the buffer that was searched.
    pub value: &'a [u8],
    /// What kind of value it is.
    pub kind: Kind,
}

/// One key-value pair, or one array element, from [`iterate`].
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Pair<'a> {
    /// The key, with its quotes stripped — or `None` for an array element.
    ///
    /// The C signals "no key" with a NULL pointer, and derives it from the key
    /// index being zero. That conflation is harmless there because an object's
    /// first key can never start at offset 0 (a `{` precedes it), and it is
    /// reproduced here exactly rather than improved on.
    pub key: Option<&'a [u8]>,
    /// Where the key starts, or 0 when there is none.
    ///
    /// The C hands back a pointer and leaves the caller to subtract; carrying
    /// the index means nobody has to do pointer arithmetic to find out where
    /// a key was. The first version of the differential did exactly that, and
    /// needing it was the sign this field was missing.
    pub key_offset: usize,
    /// Where the value starts, with the same string adjustment as [`Match`].
    pub offset: usize,
    /// The value itself.
    pub value: &'a [u8],
    /// What kind of value it is.
    pub kind: Kind,
}

// ---- the walkers ---------------------------------------------------------

/// `nextValue`: the next value, scalar or collection, and where it ends.
fn next_value(buf: &[u8], start: &mut usize, max: usize) -> Option<(usize, usize)> {
    let mut i = *start;
    let value_start = i;

    // A scalar first, then a collection, which is the C's order.
    //
    // It was worth claiming that the order MATTERS, and it does not: swapping
    // these two leaves all 3,089 lines of the query differential identical,
    // across 30 documents and all 318 corpus files. The two scanners are
    // disjoint on their first byte and neither moves the cursor when it
    // fails, which is what makes the order free -- and
    // `the_two_scanners_are_disjoint` below pins both halves of that, so it
    // stays a property rather than a coincidence.
    let found = if skip_any_scalar(buf, &mut i, max) {
        true
    } else {
        skip_collection(buf, &mut i, max) == Validity::Valid
    };

    if !found {
        return None;
    }

    *start = i;
    Some((value_start, i.saturating_sub(value_start)))
}

/// `nextKeyValuePair`: a `"key" : value` pair, and where it ends.
///
/// Returns `(key, key_length, value, value_length)`, with the key index
/// already moved past its opening quote and the length already shortened by
/// the two quotes.
fn next_key_value_pair(
    buf: &[u8],
    start: &mut usize,
    max: usize,
) -> Option<(usize, usize, usize, usize)> {
    let mut i = *start;
    let key_start = i;

    if !skip_string(buf, &mut i, max) {
        return None;
    }

    let key = key_start.saturating_add(1);
    let key_length = i.saturating_sub(key_start).saturating_sub(2);

    skip_space(buf, &mut i, max);

    if !(i < max && at(buf, i) == Some(b':')) {
        return None;
    }

    i = i.saturating_add(1);
    skip_space(buf, &mut i, max);

    let (value, value_length) = next_value(buf, &mut i, max)?;

    *start = i;
    Some((key, key_length, value, value_length))
}

/// `objectSearch`: the value for a key, by walking the pairs in order.
///
/// The first match wins, so a document with duplicate keys answers with the
/// earlier one. That is the C's behaviour and it is worth knowing, because
/// ECMA-404 does not say which should win.
fn object_search(buf: &[u8], max: usize, query: &[u8]) -> Option<(usize, usize)> {
    let mut i = 0usize;

    skip_space(buf, &mut i, max);

    if !(i < max && at(buf, i) == Some(b'{')) {
        return None;
    }

    i = i.saturating_add(1);
    skip_space(buf, &mut i, max);

    while i < max {
        let Some((key, key_length, value, value_length)) = next_key_value_pair(buf, &mut i, max)
        else {
            break;
        };

        if query.len() == key_length && buf.get(key..key.saturating_add(key_length)) == Some(query)
        {
            return Some((value, value_length));
        }

        if !skip_space_and_comma(buf, &mut i, max) {
            break;
        }
    }

    None
}

/// `arraySearch`: the value at an index, by counting values in order.
fn array_search(buf: &[u8], max: usize, query_index: u32) -> Option<(usize, usize)> {
    let mut i = 0usize;

    skip_space(buf, &mut i, max);

    if !(i < max && at(buf, i) == Some(b'[')) {
        return None;
    }

    i = i.saturating_add(1);
    skip_space(buf, &mut i, max);

    let mut current: u32 = 0;

    while i < max {
        let Some((value, value_length)) = next_value(buf, &mut i, max) else {
            break;
        };

        if current == query_index {
            return Some((value, value_length));
        }

        // The `u32::MAX` half of this is the C's overflow guard, and it is
        // why the loop stops rather than wrapping to index zero.
        if !skip_space_and_comma(buf, &mut i, max) || current == u32::MAX {
            break;
        }

        current = current.saturating_add(1);
    }

    None
}

/// `skipQueryPart`: everything up to the next separator or `[`.
///
/// Returns the part's length, or `None` if it would be empty — which is how
/// `.a` and `a..b` are refused.
fn skip_query_part(query: &[u8], start: &mut usize, max: usize) -> Option<usize> {
    let from = *start;
    let mut i = from;

    while i < max {
        match at(query, i) {
            Some(c) if c != QUERY_KEY_SEPARATOR && c != b'[' => i = i.saturating_add(1),
            _ => break,
        }
    }

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

/// `multiSearch`: walk the query's parts, narrowing the buffer at each one.
///
/// The narrowing is the whole trick, and it is easy to transcribe wrongly:
/// `length` is both the size of the sub-buffer being searched **and** the
/// place the found value's length is written back to, so each part searches
/// only inside what the previous part found.
fn multi_search(buf: &[u8], query: &[u8]) -> Result<(usize, usize), NotFound> {
    let query_length = query.len();
    let mut i = 0usize;
    let mut start = 0usize;
    let mut length = buf.len();

    while i < query_length {
        // Each part searches the slice the last one found. This is always in
        // bounds: `start + length` only ever shrinks, because a found value
        // lies inside the buffer it was found in. The `else` is therefore
        // unreachable, and answering `Missing` rather than panicking is what
        // keeps the crate's no-panic property a property rather than a hope.
        let Some(sub) = buf.get(start..).and_then(|rest| rest.get(..length)) else {
            return Err(NotFound::Missing);
        };

        let found = if at(query, i) == Some(b'[') {
            i = i.saturating_add(1);

            let query_index = skip_digits_value(query, &mut i, query_length).unwrap_or(-1);

            if query_index < 0 || i >= query_length || at(query, i) != Some(b']') {
                return Err(NotFound::BadQuery);
            }

            i = i.saturating_add(1);

            // `query_index` is non-negative and at most MAX_INDEX_VALUE, so
            // this conversion cannot lose anything.
            array_search(sub, length, query_index.unsigned_abs())
        } else {
            let query_start = i;

            let Some(key_length) = skip_query_part(query, &mut i, query_length) else {
                return Err(NotFound::BadQuery);
            };

            // The C's one-liner for "an empty key part or a trailing
            // separator": landing exactly one byte short of the end means the
            // remaining byte is a separator with nothing after it.
            if i == query_length.saturating_sub(1) {
                return Err(NotFound::BadQuery);
            }

            let Some(key) = query.get(query_start..query_start.saturating_add(key_length)) else {
                return Err(NotFound::BadQuery);
            };

            object_search(sub, length, key)
        };

        let Some((value, value_length)) = found else {
            return Err(NotFound::Missing);
        };

        start = start.saturating_add(value);
        length = value_length;

        if i < query_length && at(query, i) == Some(QUERY_KEY_SEPARATOR) {
            i = i.saturating_add(1);
        }
    }

    Ok((start, length))
}

// ---- the public surface --------------------------------------------------

/// `JSON_SearchConst`: find a value by a dotted, bracketed path.
///
/// `a.b` is the key `b` of the object at key `a`; `a[2]` is the third element
/// of the array at key `a`; `[0].x` is the key `x` of the first element of a
/// top-level array. A key containing a `.` or a `[` cannot be reached — the
/// query grammar has no escape, in the C or here.
///
/// A [`Kind::String`] match has its quotes stripped, so the value is the text.
/// Every other kind is reported exactly as it appears, which means an object
/// or array comes back whole and can be searched again.
///
/// # Errors
///
/// [`NotFound::BadQuery`] for an empty buffer or query, a query part that is
/// empty, a trailing separator, or an index that is malformed or too large.
/// [`NotFound::Missing`] when the query simply is not there.
///
/// ```
/// use rusty_rtos_json_core::search::{search, Kind};
///
/// let doc = br#"{"a":{"b":[10,20,{"c":"hi"}]}}"#;
/// assert_eq!(search(doc, b"a.b[1]").unwrap().value, b"20");
/// assert_eq!(search(doc, b"a.b[2].c").unwrap().kind, Kind::String);
/// assert_eq!(search(doc, b"a.b[2].c").unwrap().value, b"hi");
/// ```
pub fn search<'a>(buf: &'a [u8], query: &[u8]) -> Result<Match<'a>, NotFound> {
    if buf.is_empty() || query.is_empty() {
        return Err(NotFound::BadQuery);
    }

    let (value, length) = multi_search(buf, query)?;

    // A found value always starts inside the buffer, so this is the same
    // unreachable-by-construction branch as the one in `multi_search`.
    let Some(first) = at(buf, value) else {
        return Err(NotFound::Missing);
    };

    let kind = kind_of(first);
    let (offset, length) = if kind == Kind::String {
        // Strip the surrounding quotes. A string value came from `skipString`
        // so it always carries both, and the subtraction cannot go negative.
        (value.saturating_add(1), length.saturating_sub(2))
    } else {
        (value, length)
    };

    let Some(slice) = buf.get(offset..).and_then(|rest| rest.get(..length)) else {
        return Err(NotFound::Missing);
    };

    Ok(Match {
        offset,
        value: slice,
        kind,
    })
}

/// `JSON_Iterate`: the next element of a collection, carrying its own cursor.
///
/// `start` marks where the collection begins and `next` where to look; both
/// are updated. Pass them in as zero to begin. [`pairs`] wraps this as a Rust
/// iterator and is what most callers want; this exists because it is the C's
/// shape, and because the differential compares the cursors themselves.
///
/// # Errors
///
/// [`Stopped::BadCursor`] for an empty buffer or a cursor outside it,
/// [`Stopped::NotACollection`] if the buffer does not begin with `[` or `{`,
/// and [`Stopped::Exhausted`] when there is nothing further.
pub fn iterate<'a>(
    buf: &'a [u8],
    start: &mut usize,
    next: &mut usize,
) -> Result<Pair<'a>, Stopped> {
    let max = buf.len();

    if max == 0 || *start >= max || *next > max {
        return Err(Stopped::BadCursor);
    }

    skip_space(buf, start, max);

    if *next <= *start {
        *next = start.saturating_add(1);
        skip_space(buf, next, max);
    }

    // `skipSpace` above can push `start` to the end, which is why this is
    // checked again rather than relying on the guard at the top.
    let Some(opener) = at(buf, *start) else {
        return Err(Stopped::Exhausted);
    };

    let (key, key_length, value, value_length) = match opener {
        b'[' => {
            let Some((value, value_length)) = next_value(buf, next, max) else {
                return Err(Stopped::Exhausted);
            };
            (0usize, 0usize, value, value_length)
        }
        b'{' => {
            let Some(found) = next_key_value_pair(buf, next, max) else {
                return Err(Stopped::Exhausted);
            };
            found
        }
        _ => return Err(Stopped::NotACollection),
    };

    let _ = skip_space_and_comma(buf, next, max);

    let Some(first) = at(buf, value) else {
        return Err(Stopped::Exhausted);
    };

    let kind = kind_of(first);
    let (offset, value_length) = if kind == Kind::String {
        (value.saturating_add(1), value_length.saturating_sub(2))
    } else {
        (value, value_length)
    };

    let Some(value_slice) = buf.get(offset..).and_then(|rest| rest.get(..value_length)) else {
        return Err(Stopped::Exhausted);
    };

    // Zero means "no key", which is the C's NULL. See `Pair::key`.
    let key_slice = if key == 0 {
        None
    } else {
        buf.get(key..).and_then(|rest| rest.get(..key_length))
    };

    Ok(Pair {
        key: key_slice,
        key_offset: if key_slice.is_some() { key } else { 0 },
        offset,
        value: value_slice,
        kind,
    })
}

/// The elements of a collection, as a Rust iterator.
///
/// This is [`iterate`] with its two cursors carried for you. It ends at the
/// first refusal of any kind, so a malformed document simply stops early
/// rather than reporting why — use [`iterate`] directly if the reason matters.
///
/// ```
/// use rusty_rtos_json_core::search::pairs;
///
/// let doc = br#"{"a":1,"b":"two","c":[3]}"#;
/// let keys: Vec<&[u8]> = pairs(doc).filter_map(|p| p.key).collect();
/// assert_eq!(keys, vec![&b"a"[..], b"b", b"c"]);
///
/// // An array yields values with no keys.
/// assert_eq!(pairs(b"[1,2,3]").count(), 3);
/// ```
#[derive(Debug, Clone)]
pub struct Pairs<'a> {
    buf: &'a [u8],
    start: usize,
    next: usize,
    done: bool,
}

/// Iterate the elements of a collection. See [`Pairs`].
#[must_use]
pub const fn pairs(buf: &[u8]) -> Pairs<'_> {
    Pairs {
        buf,
        start: 0,
        next: 0,
        done: false,
    }
}

impl<'a> Iterator for Pairs<'a> {
    type Item = Pair<'a>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.done {
            return None;
        }

        match iterate(self.buf, &mut self.start, &mut self.next) {
            Ok(pair) => Some(pair),
            Err(_) => {
                self.done = true;
                None
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    /// Why [`next_value`] may try its two scanners in either order.
    ///
    /// This started as a poison that did not fire. Swapping the order changed
    /// nothing anywhere in the differential, so the comment claiming the order
    /// mattered was wrong. Two facts make it free, and an accidental property
    /// nobody checks is one edit away from being false:
    ///
    /// 1. the scanners are **disjoint** -- no input satisfies both;
    /// 2. a scanner that fails **does not move the cursor**, so the fallback
    ///    starts exactly where the first attempt did.
    ///
    /// Lose either one and the order becomes load-bearing silently.
    #[test]
    fn the_two_scanners_are_disjoint_and_do_not_move_on_failure() {
        const INPUTS: [&[u8]; 18] = [
            b"",
            b" ",
            b"[",
            b"]",
            b"{",
            b"}",
            b"[]",
            b"{}",
            b"[1,2]",
            b"{\"a\":1}",
            b"42",
            b"-0.5e3",
            b"\"s\"",
            b"true",
            b"false",
            b"null",
            b"[1,2",
            b"{\"a\":",
        ];

        for input in INPUTS {
            let max = input.len();

            let mut scalar_at = 0usize;
            let scalar = skip_any_scalar(input, &mut scalar_at, max);

            let mut collection_at = 0usize;
            let collection = skip_collection(input, &mut collection_at, max) == Validity::Valid;

            assert!(
                !(scalar && collection),
                "both scanners claimed {input:?}, so the order WOULD matter"
            );

            if !scalar {
                assert_eq!(
                    scalar_at, 0,
                    "a failed scalar scan moved the cursor on {input:?}"
                );
            }
            if !collection {
                assert_eq!(
                    collection_at, 0,
                    "a failed collection scan moved the cursor on {input:?}"
                );
            }
        }
    }
}