vole-document 0.1.0-alpha.32

Persistent procedural document runtime: byte-exact reconstruction plus a content-addressed procedural seed DAG, queryable observations with provenance, and selective late materialization.
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
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
//! Bounded, representation-preserving JSON adapter (Phase 21.5.1).
//!
//! JSON is the first **Wave-2 structured-tree** format. It is *not* an office
//! package: there is no OPC/ZIP layer, no `mimetype`, and no relationship graph.
//! The exact leaf is therefore the **whole source** (a `DocumentExact`, a
//! RAW-like authority), and everything this module produces is a bounded,
//! deterministic (`Q_gen`) projection that never sits on the exactness path.
//!
//! ## Why a bespoke parser
//!
//! The point of a JSON adapter is to preserve **representation**, not merely
//! values. For every token the parser records its exact **byte span** in the
//! source, so it preserves, and can report:
//!
//! * object **member order** (never normalized or sorted);
//! * **duplicate keys** (kept as distinct members, never dropped/overwritten);
//! * **whitespace** (each token's span is exact, so inter-token whitespace is
//!   recoverable as the gaps);
//! * **numeric spelling** (`1e3`, `1.0`, `-0` are kept as their literal text,
//!   never parsed into a binary float);
//! * **string-escape spelling** (`\u00e9` is distinct from `é`).
//!
//! The parser is a bounded recursive-descent scanner. Recursion is safe because
//! the nesting depth is capped by [`Limits::max_json_depth`]; the total node
//! count by [`Limits::max_json_nodes`]; the raw string bytes by
//! [`Limits::max_json_string_bytes`]; and the source length by
//! [`Limits::max_json_document_bytes`]. Untrusted input can only ever yield a
//! typed decline, never a panic or an unbounded allocation.
//!
//! ## Detection
//!
//! [`detect`] is deliberately conservative: the whole source must parse as
//! **exactly one** JSON value (trailing bytes other than whitespace reject), and
//! it must stay within every cap. Anything else is not JSON. Detection runs the
//! same parser with node-building disabled, so it is O(1) in extra memory and
//! cannot be forced to allocate by a large near-JSON input.

use crate::error::{Error, Result};
use crate::limits::Limits;

/// Wire version of the canonical derived model.
pub const MODEL_VERSION: u8 = 1;
/// Hard cap on decoded model nodes (defends the decoder against a hostile blob).
pub const MAX_MODEL_NODES: u32 = 1 << 24;

/// JSON node kind: an object (`{ … }`).
pub const K_OBJECT: u8 = 0;
/// JSON node kind: an array (`[ … ]`).
pub const K_ARRAY: u8 = 1;
/// JSON node kind: a string (`"…"`).
pub const K_STRING: u8 = 2;
/// JSON node kind: a number.
pub const K_NUMBER: u8 = 3;
/// JSON node kind: the literal `true`.
pub const K_TRUE: u8 = 4;
/// JSON node kind: the literal `false`.
pub const K_FALSE: u8 = 5;
/// JSON node kind: the literal `null`.
pub const K_NULL: u8 = 6;

/// Stable lower-case kind name for reports and JSON output.
pub const fn kind_name(kind: u8) -> &'static str {
    match kind {
        K_OBJECT => "object",
        K_ARRAY => "array",
        K_STRING => "string",
        K_NUMBER => "number",
        K_TRUE => "true",
        K_FALSE => "false",
        K_NULL => "null",
        _ => "unknown",
    }
}

/// Whether `kind` is a container (has children).
pub const fn is_container(kind: u8) -> bool {
    matches!(kind, K_OBJECT | K_ARRAY)
}

/// One parsed JSON node: its kind, its exact source token span, and its children.
///
/// For an object, `children` is the interleaved document-order list
/// `[key0, value0, key1, value1, …]`, so member order and duplicate keys are both
/// preserved verbatim. For an array, `children` is the element list in order. For
/// a scalar, `children` is empty and `[start, end)` is the exact token.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JNode {
    /// One of the `K_*` tags.
    pub kind: u8,
    /// The token's first source byte (for a string, the opening quote).
    pub start: u64,
    /// One past the token's last source byte (for a string, past the close quote).
    pub end: u64,
    /// Object: interleaved key/value indices; array: element indices.
    pub children: Vec<u32>,
}

/// The canonical derived JSON model (the materialization of a `JsonModel` node).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JsonModel {
    /// Index of the root node.
    pub root: u32,
    /// The root's kind tag.
    pub top_type: u8,
    /// The observed maximum container nesting depth (root = 1).
    pub max_depth: u32,
    /// The exact source length the spans are relative to.
    pub doc_len: u64,
    /// The node arena.
    pub nodes: Vec<JNode>,
}

impl JsonModel {
    /// The node at `index`, if present.
    pub fn node(&self, index: u32) -> Option<&JNode> {
        self.nodes.get(index as usize)
    }

    /// Deterministically encode the model (little-endian, length-prefixed).
    pub fn encode(&self) -> Vec<u8> {
        let mut out = Vec::with_capacity(32 + self.nodes.len() * 24);
        out.extend_from_slice(b"JSONM");
        out.push(MODEL_VERSION);
        out.extend_from_slice(&self.root.to_le_bytes());
        out.push(self.top_type);
        out.extend_from_slice(&self.max_depth.to_le_bytes());
        out.extend_from_slice(&self.doc_len.to_le_bytes());
        out.extend_from_slice(&(self.nodes.len() as u32).to_le_bytes());
        for n in &self.nodes {
            out.push(n.kind);
            out.extend_from_slice(&n.start.to_le_bytes());
            out.extend_from_slice(&n.end.to_le_bytes());
            out.extend_from_slice(&(n.children.len() as u32).to_le_bytes());
            for c in &n.children {
                out.extend_from_slice(&c.to_le_bytes());
            }
        }
        out
    }

    /// Decode a model produced by [`Self::encode`]. Fail-closed on any corruption.
    pub fn decode(bytes: &[u8]) -> Result<JsonModel> {
        let mut r = BinReader::new(bytes);
        if r.bytes(5)? != b"JSONM" {
            return Err(corrupt("bad model magic"));
        }
        if r.u8()? != MODEL_VERSION {
            return Err(corrupt("unsupported model version"));
        }
        let root = r.u32()?;
        let top_type = r.u8()?;
        if top_type > K_NULL {
            return Err(corrupt("unknown top-level kind"));
        }
        let max_depth = r.u32()?;
        let doc_len = r.u64()?;
        let count = r.u32()?;
        if count > MAX_MODEL_NODES {
            return Err(corrupt("model node count is implausible"));
        }
        let mut nodes = Vec::with_capacity(count as usize);
        for _ in 0..count {
            let kind = r.u8()?;
            if kind > K_NULL {
                return Err(corrupt("unknown node kind"));
            }
            let start = r.u64()?;
            let end = r.u64()?;
            if start > end || end > doc_len {
                return Err(corrupt("node span is outside the document"));
            }
            let cn = r.u32()?;
            if cn as u64 > count as u64 {
                return Err(corrupt("node child count is implausible"));
            }
            let mut children = Vec::with_capacity(cn as usize);
            for _ in 0..cn {
                let c = r.u32()?;
                if c >= count {
                    return Err(corrupt("child index is out of range"));
                }
                children.push(c);
            }
            nodes.push(JNode {
                kind,
                start,
                end,
                children,
            });
        }
        if !r.at_end() {
            return Err(corrupt("model has trailing bytes"));
        }
        if root as usize >= nodes.len() {
            return Err(corrupt("root index is out of range"));
        }
        Ok(JsonModel {
            root,
            top_type,
            max_depth,
            doc_len,
            nodes,
        })
    }
}

/// A resolved pointer query: the node index and how many members matched the
/// final key segment (so duplicate keys are reported, never hidden).
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Resolved {
    /// The resolved node index.
    pub index: u32,
    /// The number of object members with the final segment's key (`1` for an
    /// array index or the root; `>1` witnesses duplicate keys).
    pub matches: u32,
}

/// One lexical-match record from [`find`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct JsonMatch {
    /// Canonical RFC 6901 pointer to the matching node.
    pub pointer: String,
    /// Whether the match is an object key or a string value.
    pub role: MatchRole,
    /// The matching node's kind (always `K_STRING`).
    pub kind: u8,
    /// The exact source span of the matching string token.
    pub start: u64,
    /// One past the matching string token.
    pub end: u64,
    /// The decoded text of the matching string.
    pub text: String,
}

/// Whether a lexical match is an object member key or a string value.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MatchRole {
    /// An object member key.
    Key,
    /// A string value (object member value or array element).
    Value,
}

impl MatchRole {
    /// Stable lower-case name.
    pub const fn name(self) -> &'static str {
        match self {
            MatchRole::Key => "key",
            MatchRole::Value => "value",
        }
    }
}

/// Fast-fail JSON detector: does the whole `source` parse as exactly one JSON
/// value within `limits`? Conservative by construction.
pub fn detect(source: &[u8], limits: Limits) -> bool {
    if source.len() as u64 > limits.max_json_document_bytes {
        return false;
    }
    parse(source, limits, false).is_ok()
}

/// Parse `source` and return its canonical derived model, encoded (the
/// materialization of a `JsonModel` node).
pub fn build_json_model(source: &[u8], limits: Limits) -> Result<Vec<u8>> {
    Ok(parse(source, limits, true)?.encode())
}

/// Parse `source` into a [`JsonModel`]. `build` selects whether the node arena is
/// populated (detection runs with `build = false` to stay O(1) in memory).
pub fn parse(source: &[u8], limits: Limits, build: bool) -> Result<JsonModel> {
    if source.len() as u64 > limits.max_json_document_bytes {
        return Err(Error::resource_limit(format!(
            "JSON source is {} bytes, above the {}-byte document cap",
            source.len(),
            limits.max_json_document_bytes
        )));
    }
    let mut p = Parser::new(source, limits, build);
    p.skip_ws();
    if p.at >= p.b.len() {
        return Err(corrupt("empty input is not a JSON value"));
    }
    let root = p.value(1)?;
    p.skip_ws();
    if p.at != p.b.len() {
        return Err(corrupt("trailing bytes after the single JSON value"));
    }
    Ok(JsonModel {
        root,
        top_type: p.nodes.first().map_or(0, |_| p.root_kind),
        max_depth: p.max_depth_seen,
        doc_len: source.len() as u64,
        nodes: p.nodes,
    })
}

/// RFC 6901 pointer resolution against a parsed model. A missing node, an
/// out-of-range array index, or indexing into a scalar is a typed decline
/// (never a silent empty answer).
pub fn resolve_pointer(model: &JsonModel, source: &[u8], pointer: &str) -> Result<Resolved> {
    let segments = parse_pointer(pointer)?;
    let mut index = model.root;
    let mut matches = 1u32;
    for seg in &segments {
        let node = model
            .node(index)
            .ok_or_else(|| corrupt("pointer traversal left the model"))?;
        match node.kind {
            K_OBJECT => {
                let mut found = 0u32;
                let mut first = None;
                let mut i = 0usize;
                while i + 1 < node.children.len() {
                    let key_idx = node.children[i];
                    let val_idx = node.children[i + 1];
                    i += 2;
                    let key_node = model
                        .node(key_idx)
                        .ok_or_else(|| corrupt("object key index is out of range"))?;
                    if key_node.kind != K_STRING {
                        continue;
                    }
                    let key = decode_string(source, key_node)?;
                    if key == *seg {
                        found = found.saturating_add(1);
                        if first.is_none() {
                            first = Some(val_idx);
                        }
                    }
                }
                index = first.ok_or_else(|| {
                    Error::unsupported_feature(format!(
                        "JSON object has no member {seg:?} at this pointer"
                    ))
                })?;
                matches = found;
            }
            K_ARRAY => {
                let idx = parse_array_index(seg)?;
                let len = node.children.len();
                if idx as usize >= len {
                    return Err(Error::unsupported_feature(format!(
                        "JSON array index {idx} is out of range (length {len})"
                    )));
                }
                index = node.children[idx as usize];
                matches = 1;
            }
            _ => {
                return Err(Error::unsupported_feature(format!(
                    "cannot index into a JSON {} at {seg:?}",
                    kind_name(node.kind)
                )));
            }
        }
    }
    Ok(Resolved { index, matches })
}

/// The exact source bytes of a node's token (`[start, end)`), bounded by the
/// document length.
pub fn token_bytes<'a>(source: &'a [u8], node: &JNode) -> Result<&'a [u8]> {
    let s = usize::try_from(node.start).map_err(|_| corrupt("span overflow"))?;
    let e = usize::try_from(node.end).map_err(|_| corrupt("span overflow"))?;
    source
        .get(s..e)
        .ok_or_else(|| corrupt("node span is outside the source"))
}

/// Decode a JSON string node's token into its Rust text (escapes resolved,
/// `\uXXXX` surrogate pairs combined). Never panics; a malformed token is typed.
pub fn decode_string(source: &[u8], node: &JNode) -> Result<String> {
    let tok = token_bytes(source, node)?;
    if node.kind != K_STRING || tok.len() < 2 || tok[0] != b'"' || tok[tok.len() - 1] != b'"' {
        return Err(corrupt("node is not a well-formed JSON string token"));
    }
    unescape(&tok[1..tok.len() - 1])
}

/// Render a deterministic canonical text projection of the whole model: member
/// order and token spelling (numeric/escape) are preserved, separators are
/// canonical (`:`, `,`, no insignificant whitespace).
pub fn canonical_text(model: &JsonModel, source: &[u8]) -> Result<String> {
    subtree_text(model, source, model.root)
}

/// Render one node's subtree to canonical text (see [`canonical_text`]).
pub fn subtree_text(model: &JsonModel, source: &[u8], index: u32) -> Result<String> {
    let mut out = String::new();
    render(model, source, index, &mut out, 0)?;
    Ok(out)
}

/// The index of a node's parent, if any (the root has none).
pub fn find_parent(model: &JsonModel, index: u32) -> Option<u32> {
    model
        .nodes
        .iter()
        .position(|n| n.children.contains(&index))
        .map(|i| i as u32)
}

fn render(
    model: &JsonModel,
    source: &[u8],
    index: u32,
    out: &mut String,
    depth: u32,
) -> Result<()> {
    if depth > MAX_MODEL_NODES {
        return Err(Error::resource_limit(
            "JSON render exceeded its depth bound",
        ));
    }
    let node = model
        .node(index)
        .ok_or_else(|| corrupt("render hit an out-of-range node"))?;
    match node.kind {
        K_OBJECT => {
            out.push('{');
            let mut i = 0usize;
            while i + 1 < node.children.len() {
                if i > 0 {
                    out.push(',');
                }
                render(model, source, node.children[i], out, depth + 1)?;
                out.push(':');
                render(model, source, node.children[i + 1], out, depth + 1)?;
                i += 2;
            }
            out.push('}');
        }
        K_ARRAY => {
            out.push('[');
            for (i, child) in node.children.iter().enumerate() {
                if i > 0 {
                    out.push(',');
                }
                render(model, source, *child, out, depth + 1)?;
            }
            out.push(']');
        }
        _ => {
            let tok = token_bytes(source, node)?;
            out.push_str(&String::from_utf8_lossy(tok));
        }
    }
    Ok(())
}

/// A bounded, case-sensitive lexical search over object keys and string values.
/// Returns matches in document order (object members before descendants), each
/// with its canonical pointer and exact source span.
pub fn find(
    model: &JsonModel,
    source: &[u8],
    pattern: &str,
    limits: Limits,
) -> Result<Vec<JsonMatch>> {
    let mut out = Vec::new();
    let mut path: Vec<String> = Vec::new();
    walk_find(
        model, source, model.root, true, &mut path, pattern, &mut out, limits, 0,
    )?;
    Ok(out)
}

#[allow(clippy::too_many_arguments)]
fn walk_find(
    model: &JsonModel,
    source: &[u8],
    index: u32,
    is_value: bool,
    path: &mut Vec<String>,
    pattern: &str,
    out: &mut Vec<JsonMatch>,
    limits: Limits,
    depth: u32,
) -> Result<()> {
    if depth > limits.max_json_depth {
        return Err(Error::resource_limit(
            "JSON find exceeded the nesting-depth cap",
        ));
    }
    let node = model
        .node(index)
        .ok_or_else(|| corrupt("find hit an out-of-range node"))?;
    match node.kind {
        K_OBJECT => {
            let mut i = 0usize;
            while i + 1 < node.children.len() {
                let key_idx = node.children[i];
                let val_idx = node.children[i + 1];
                i += 2;
                let key_node = model
                    .node(key_idx)
                    .ok_or_else(|| corrupt("object key index is out of range"))?;
                let key = decode_string(source, key_node)?;
                if key.contains(pattern) {
                    out.push(JsonMatch {
                        pointer: pointer_of(path, Some(&key)),
                        role: MatchRole::Key,
                        kind: K_STRING,
                        start: key_node.start,
                        end: key_node.end,
                        text: key.clone(),
                    });
                }
                path.push(escape_segment(&key));
                walk_find(
                    model,
                    source,
                    val_idx,
                    true,
                    path,
                    pattern,
                    out,
                    limits,
                    depth + 1,
                )?;
                path.pop();
            }
        }
        K_ARRAY => {
            for (i, child) in node.children.iter().enumerate() {
                path.push(i.to_string());
                walk_find(
                    model,
                    source,
                    *child,
                    true,
                    path,
                    pattern,
                    out,
                    limits,
                    depth + 1,
                )?;
                path.pop();
            }
        }
        K_STRING if is_value => {
            let text = decode_string(source, node)?;
            if text.contains(pattern) {
                out.push(JsonMatch {
                    pointer: pointer_of(path, None),
                    role: MatchRole::Value,
                    kind: K_STRING,
                    start: node.start,
                    end: node.end,
                    text,
                });
            }
        }
        _ => {}
    }
    Ok(())
}

fn pointer_of(path: &[String], leaf_key: Option<&str>) -> String {
    let mut s = String::new();
    for seg in path {
        s.push('/');
        s.push_str(seg);
    }
    if let Some(k) = leaf_key {
        s.push('/');
        s.push_str(&escape_segment(k));
    }
    s
}

/// Escape one pointer segment per RFC 6901 (`~` → `~0`, `/` → `~1`).
pub fn escape_segment(seg: &str) -> String {
    let mut out = String::with_capacity(seg.len());
    for c in seg.chars() {
        match c {
            '~' => out.push_str("~0"),
            '/' => out.push_str("~1"),
            _ => out.push(c),
        }
    }
    out
}

/// Parse an RFC 6901 pointer into decoded segments.
fn parse_pointer(pointer: &str) -> Result<Vec<String>> {
    if pointer.is_empty() {
        return Ok(Vec::new());
    }
    if !pointer.starts_with('/') {
        return Err(Error::usage(format!(
            "JSON pointer {pointer:?} must be empty or start with '/'"
        )));
    }
    let mut out = Vec::new();
    for raw in pointer.split('/').skip(1) {
        out.push(unescape_segment(raw)?);
    }
    Ok(out)
}

fn unescape_segment(raw: &str) -> Result<String> {
    let mut out = String::with_capacity(raw.len());
    let mut chars = raw.chars();
    while let Some(c) = chars.next() {
        if c == '~' {
            match chars.next() {
                Some('0') => out.push('~'),
                Some('1') => out.push('/'),
                _ => {
                    return Err(Error::usage(format!(
                        "invalid JSON pointer escape in segment {raw:?}"
                    )));
                }
            }
        } else {
            out.push(c);
        }
    }
    Ok(out)
}

fn parse_array_index(seg: &str) -> Result<u32> {
    if seg == "-" || (seg.len() > 1 && seg.starts_with('0')) || seg.is_empty() {
        return Err(Error::usage(format!(
            "JSON array segment {seg:?} is not a canonical index"
        )));
    }
    seg.parse::<u32>()
        .map_err(|_| Error::usage(format!("JSON array index {seg:?} is not a u32")))
}

fn unescape(inner: &[u8]) -> Result<String> {
    let mut out: Vec<u8> = Vec::with_capacity(inner.len());
    let mut i = 0usize;
    while i < inner.len() {
        let c = inner[i];
        if c != b'\\' {
            out.push(c);
            i += 1;
            continue;
        }
        i += 1;
        let e = *inner
            .get(i)
            .ok_or_else(|| corrupt("string ends inside an escape"))?;
        i += 1;
        match e {
            b'"' => out.push(b'"'),
            b'\\' => out.push(b'\\'),
            b'/' => out.push(b'/'),
            b'b' => out.push(0x08),
            b'f' => out.push(0x0C),
            b'n' => out.push(b'\n'),
            b'r' => out.push(b'\r'),
            b't' => out.push(b'\t'),
            b'u' => {
                let cp = read_hex4(inner, &mut i)?;
                if (0xD800..0xDC00).contains(&cp) {
                    // High surrogate: a low surrogate must follow.
                    if inner.get(i) != Some(&b'\\') || inner.get(i + 1) != Some(&b'u') {
                        return Err(corrupt("lone high surrogate in string"));
                    }
                    i += 2;
                    let lo = read_hex4(inner, &mut i)?;
                    if !(0xDC00..0xE000).contains(&lo) {
                        return Err(corrupt("high surrogate not followed by a low surrogate"));
                    }
                    let combined = 0x10000 + ((cp - 0xD800) << 10) + (lo - 0xDC00);
                    let ch = char::from_u32(combined)
                        .ok_or_else(|| corrupt("invalid surrogate pair"))?;
                    let mut buf = [0u8; 4];
                    out.extend_from_slice(ch.encode_utf8(&mut buf).as_bytes());
                } else if (0xDC00..0xE000).contains(&cp) {
                    return Err(corrupt("lone low surrogate in string"));
                } else {
                    let ch = char::from_u32(cp).ok_or_else(|| corrupt("invalid code point"))?;
                    let mut buf = [0u8; 4];
                    out.extend_from_slice(ch.encode_utf8(&mut buf).as_bytes());
                }
            }
            _ => return Err(corrupt("invalid string escape")),
        }
    }
    String::from_utf8(out).map_err(|_| corrupt("string is not valid UTF-8"))
}

fn read_hex4(inner: &[u8], i: &mut usize) -> Result<u32> {
    let mut v = 0u32;
    for _ in 0..4 {
        let b = *inner
            .get(*i)
            .ok_or_else(|| corrupt("truncated \\u escape"))?;
        *i += 1;
        let d = match b {
            b'0'..=b'9' => (b - b'0') as u32,
            b'a'..=b'f' => (b - b'a' + 10) as u32,
            b'A'..=b'F' => (b - b'A' + 10) as u32,
            _ => return Err(corrupt("\\u escape has a non-hex digit")),
        };
        v = (v << 4) | d;
    }
    Ok(v)
}

// ---------------------------------------------------------------------------
// The bounded parser
// ---------------------------------------------------------------------------

struct Parser<'a> {
    b: &'a [u8],
    at: usize,
    limits: Limits,
    build: bool,
    nodes: Vec<JNode>,
    count: u64,
    string_bytes: u64,
    max_depth_seen: u32,
    root_kind: u8,
}

impl<'a> Parser<'a> {
    fn new(b: &'a [u8], limits: Limits, build: bool) -> Self {
        Parser {
            b,
            at: 0,
            limits,
            build,
            nodes: Vec::new(),
            count: 0,
            string_bytes: 0,
            max_depth_seen: 0,
            root_kind: 0,
        }
    }

    fn skip_ws(&mut self) {
        while let Some(&c) = self.b.get(self.at) {
            match c {
                b' ' | b'\t' | b'\n' | b'\r' => self.at += 1,
                _ => break,
            }
        }
    }

    fn peek(&self) -> Option<u8> {
        self.b.get(self.at).copied()
    }

    fn bump_count(&mut self) -> Result<()> {
        self.count = self.count.saturating_add(1);
        if self.count > self.limits.max_json_nodes as u64 {
            return Err(Error::resource_limit(format!(
                "JSON document exceeds the {}-node cap",
                self.limits.max_json_nodes
            )));
        }
        Ok(())
    }

    fn value(&mut self, depth: u32) -> Result<u32> {
        if depth > self.limits.max_json_depth {
            return Err(Error::resource_limit(format!(
                "JSON nesting exceeds the {}-level cap",
                self.limits.max_json_depth
            )));
        }
        self.max_depth_seen = self.max_depth_seen.max(depth);
        self.skip_ws();
        let start = self.at as u64;
        let c = self
            .peek()
            .ok_or_else(|| corrupt("unexpected end of input"))?;
        match c {
            b'{' => self.object(depth, start),
            b'[' => self.array(depth, start),
            b'"' => {
                self.string_token()?;
                self.scalar(K_STRING, start, self.at as u64)
            }
            b't' => {
                self.keyword(b"true")?;
                self.scalar(K_TRUE, start, self.at as u64)
            }
            b'f' => {
                self.keyword(b"false")?;
                self.scalar(K_FALSE, start, self.at as u64)
            }
            b'n' => {
                self.keyword(b"null")?;
                self.scalar(K_NULL, start, self.at as u64)
            }
            b'-' | b'0'..=b'9' => {
                self.number()?;
                self.scalar(K_NUMBER, start, self.at as u64)
            }
            _ => Err(corrupt("not a JSON value")),
        }
    }

    fn scalar(&mut self, kind: u8, start: u64, end: u64) -> Result<u32> {
        self.bump_count()?;
        if self.count == 1 {
            self.root_kind = kind;
        }
        if self.build {
            let idx = self.nodes.len() as u32;
            self.nodes.push(JNode {
                kind,
                start,
                end,
                children: Vec::new(),
            });
            Ok(idx)
        } else {
            Ok(0)
        }
    }

    fn placeholder(&mut self, kind: u8, start: u64) -> Result<u32> {
        self.bump_count()?;
        if self.count == 1 {
            self.root_kind = kind;
        }
        if self.build {
            let idx = self.nodes.len() as u32;
            self.nodes.push(JNode {
                kind,
                start,
                end: start,
                children: Vec::new(),
            });
            Ok(idx)
        } else {
            Ok(0)
        }
    }

    fn finish(&mut self, idx: u32, children: Vec<u32>, end: u64) -> u32 {
        if self.build {
            let n = &mut self.nodes[idx as usize];
            n.children = children;
            n.end = end;
        }
        idx
    }

    fn object(&mut self, depth: u32, start: u64) -> Result<u32> {
        let idx = self.placeholder(K_OBJECT, start)?;
        self.at += 1; // consume '{'
        let mut children: Vec<u32> = Vec::new();
        self.skip_ws();
        if self.peek() == Some(b'}') {
            self.at += 1;
            return Ok(self.finish(idx, children, self.at as u64));
        }
        loop {
            self.skip_ws();
            if self.peek() != Some(b'"') {
                return Err(corrupt("object member key must be a string"));
            }
            let kstart = self.at as u64;
            self.string_token()?;
            let key = self.scalar(K_STRING, kstart, self.at as u64)?;
            self.skip_ws();
            if self.peek() != Some(b':') {
                return Err(corrupt("object member key must be followed by ':'"));
            }
            self.at += 1;
            let val = self.value(depth + 1)?;
            if self.build {
                children.push(key);
                children.push(val);
            }
            self.skip_ws();
            match self.peek() {
                Some(b',') => self.at += 1,
                Some(b'}') => {
                    self.at += 1;
                    break;
                }
                _ => return Err(corrupt("expected ',' or '}' in object")),
            }
        }
        Ok(self.finish(idx, children, self.at as u64))
    }

    fn array(&mut self, depth: u32, start: u64) -> Result<u32> {
        let idx = self.placeholder(K_ARRAY, start)?;
        self.at += 1; // consume '['
        let mut children: Vec<u32> = Vec::new();
        self.skip_ws();
        if self.peek() == Some(b']') {
            self.at += 1;
            return Ok(self.finish(idx, children, self.at as u64));
        }
        loop {
            let val = self.value(depth + 1)?;
            if self.build {
                children.push(val);
            }
            self.skip_ws();
            match self.peek() {
                Some(b',') => self.at += 1,
                Some(b']') => {
                    self.at += 1;
                    break;
                }
                _ => return Err(corrupt("expected ',' or ']' in array")),
            }
        }
        Ok(self.finish(idx, children, self.at as u64))
    }

    fn keyword(&mut self, kw: &[u8]) -> Result<()> {
        let end = self
            .at
            .checked_add(kw.len())
            .ok_or_else(|| corrupt("keyword overflow"))?;
        if self.b.get(self.at..end) != Some(kw) {
            return Err(corrupt("invalid JSON keyword"));
        }
        self.at = end;
        Ok(())
    }

    fn number(&mut self) -> Result<()> {
        if self.peek() == Some(b'-') {
            self.at += 1;
        }
        match self.peek() {
            Some(b'0') => self.at += 1,
            Some(b'1'..=b'9') => {
                while matches!(self.peek(), Some(b'0'..=b'9')) {
                    self.at += 1;
                }
            }
            _ => return Err(corrupt("number has no integer part")),
        }
        if self.peek() == Some(b'.') {
            self.at += 1;
            if !matches!(self.peek(), Some(b'0'..=b'9')) {
                return Err(corrupt("number fraction has no digits"));
            }
            while matches!(self.peek(), Some(b'0'..=b'9')) {
                self.at += 1;
            }
        }
        if matches!(self.peek(), Some(b'e') | Some(b'E')) {
            self.at += 1;
            if matches!(self.peek(), Some(b'+') | Some(b'-')) {
                self.at += 1;
            }
            if !matches!(self.peek(), Some(b'0'..=b'9')) {
                return Err(corrupt("number exponent has no digits"));
            }
            while matches!(self.peek(), Some(b'0'..=b'9')) {
                self.at += 1;
            }
        }
        Ok(())
    }

    /// Parse one string token, validating escapes and UTF-8, and charge its raw
    /// byte length against `max_json_string_bytes`.
    fn string_token(&mut self) -> Result<()> {
        let open = self.at;
        self.at += 1; // consume opening quote
        loop {
            let c = self
                .peek()
                .ok_or_else(|| corrupt("unterminated JSON string"))?;
            match c {
                b'"' => {
                    self.at += 1;
                    break;
                }
                b'\\' => {
                    self.at += 1;
                    let e = self
                        .peek()
                        .ok_or_else(|| corrupt("string ends inside an escape"))?;
                    match e {
                        b'"' | b'\\' | b'/' | b'b' | b'f' | b'n' | b'r' | b't' => self.at += 1,
                        b'u' => {
                            self.at += 1;
                            for _ in 0..4 {
                                let h =
                                    self.peek().ok_or_else(|| corrupt("truncated \\u escape"))?;
                                if !h.is_ascii_hexdigit() {
                                    return Err(corrupt("\\u escape has a non-hex digit"));
                                }
                                self.at += 1;
                            }
                        }
                        _ => return Err(corrupt("invalid string escape")),
                    }
                }
                0x00..=0x1F => {
                    return Err(corrupt("unescaped control character in string"));
                }
                _ => self.at += 1,
            }
        }
        // The raw inner bytes must be valid UTF-8 (escapes are ASCII, so this
        // checks the literal multi-byte characters).
        let inner = self
            .b
            .get(open + 1..self.at - 1)
            .ok_or_else(|| corrupt("string span overflow"))?;
        core::str::from_utf8(inner).map_err(|_| corrupt("string is not valid UTF-8"))?;
        self.string_bytes = self.string_bytes.saturating_add(inner.len() as u64);
        if self.string_bytes > self.limits.max_json_string_bytes {
            return Err(Error::resource_limit(format!(
                "JSON string bytes exceed the {}-byte cap",
                self.limits.max_json_string_bytes
            )));
        }
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// Small helpers
// ---------------------------------------------------------------------------

fn corrupt(msg: &str) -> Error {
    Error::invalid_json_structure(format!("malformed JSON: {msg}"))
}

struct BinReader<'a> {
    b: &'a [u8],
    at: usize,
}

impl<'a> BinReader<'a> {
    fn new(b: &'a [u8]) -> Self {
        BinReader { b, at: 0 }
    }

    fn bytes(&mut self, n: usize) -> Result<&'a [u8]> {
        let end = self
            .at
            .checked_add(n)
            .ok_or_else(|| corrupt("reader overflow"))?;
        let s = self
            .b
            .get(self.at..end)
            .ok_or_else(|| corrupt("reader underrun"))?;
        self.at = end;
        Ok(s)
    }

    fn u8(&mut self) -> Result<u8> {
        Ok(self.bytes(1)?[0])
    }

    fn u32(&mut self) -> Result<u32> {
        let b = self.bytes(4)?;
        Ok(u32::from_le_bytes([b[0], b[1], b[2], b[3]]))
    }

    fn u64(&mut self) -> Result<u64> {
        let b = self.bytes(8)?;
        Ok(u64::from_le_bytes([
            b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7],
        ]))
    }

    fn at_end(&self) -> bool {
        self.at == self.b.len()
    }
}

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

    fn model(src: &[u8]) -> JsonModel {
        parse(src, Limits::DEFAULT, true).unwrap()
    }

    #[test]
    fn detects_simple_documents_and_rejects_others() {
        assert!(detect(b"{\"a\":1}", Limits::DEFAULT));
        assert!(detect(b"  [1, 2, 3]\n", Limits::DEFAULT));
        assert!(detect(b"42", Limits::DEFAULT));
        assert!(detect(b"\"hi\"", Limits::DEFAULT));
        assert!(detect(b"true", Limits::DEFAULT));
        assert!(detect(b"null", Limits::DEFAULT));
        assert!(!detect(b"", Limits::DEFAULT));
        assert!(!detect(b"{", Limits::DEFAULT));
        assert!(!detect(b"{\"a\":1} trailing", Limits::DEFAULT));
        assert!(!detect(b"[1,2,]", Limits::DEFAULT));
        assert!(!detect(b"not json", Limits::DEFAULT));
        assert!(!detect(b"01", Limits::DEFAULT));
        assert!(!detect(b"+1", Limits::DEFAULT));
    }

    #[test]
    fn preserves_spelling_order_and_duplicate_keys() {
        let src = br#"{"b":1e3,"a":1.0,"a":-0,"c":"\u00e9"}"#;
        let m = model(src);
        assert_eq!(m.top_type, K_OBJECT);
        let root = m.node(m.root).unwrap();
        // 4 members = 8 children, duplicates kept.
        assert_eq!(root.children.len(), 8);
        // Member order preserved: first key is "b".
        let k0 = m.node(root.children[0]).unwrap();
        assert_eq!(token_bytes(src, k0).unwrap(), br#""b""#);
        // Numeric spelling preserved verbatim.
        let v0 = m.node(root.children[1]).unwrap();
        assert_eq!(token_bytes(src, v0).unwrap(), b"1e3");
        // The two "a" keys are distinct children.
        let k1 = m.node(root.children[2]).unwrap();
        let k2 = m.node(root.children[4]).unwrap();
        assert_eq!(token_bytes(src, k1).unwrap(), br#""a""#);
        assert_eq!(token_bytes(src, k2).unwrap(), br#""a""#);
        let v1 = m.node(root.children[3]).unwrap();
        let v2 = m.node(root.children[5]).unwrap();
        assert_eq!(token_bytes(src, v1).unwrap(), b"1.0");
        assert_eq!(token_bytes(src, v2).unwrap(), b"-0");
        // Escape spelling preserved verbatim; decoded value is `é`.
        let v3 = m.node(root.children[7]).unwrap();
        assert_eq!(token_bytes(src, v3).unwrap(), br#""\u00e9""#);
        assert_eq!(decode_string(src, v3).unwrap(), "é");
    }

    #[test]
    fn pointer_resolution_handles_escapes_and_indices() {
        let src = br#"{"a/b":0,"c":{"~x":[10,20,30]}}"#;
        let m = model(src);
        let r = resolve_pointer(&m, src, "/a~1b").unwrap();
        assert_eq!(token_bytes(src, m.node(r.index).unwrap()).unwrap(), b"0");
        let r = resolve_pointer(&m, src, "/c/~0x/2").unwrap();
        assert_eq!(token_bytes(src, m.node(r.index).unwrap()).unwrap(), b"30");
        // Out of range and missing are typed declines.
        assert!(resolve_pointer(&m, src, "/c/~0x/9").is_err());
        assert!(resolve_pointer(&m, src, "/nope").is_err());
        assert!(resolve_pointer(&m, src, "no-slash").is_err());
    }

    #[test]
    fn duplicate_key_pointer_reports_matches() {
        let src = br#"{"a":1,"a":2}"#;
        let m = model(src);
        let r = resolve_pointer(&m, src, "/a").unwrap();
        assert_eq!(r.matches, 2);
        assert_eq!(token_bytes(src, m.node(r.index).unwrap()).unwrap(), b"1");
    }

    #[test]
    fn canonical_text_is_deterministic_and_order_preserving() {
        let src = b"{\n  \"b\" : 1e3 ,\n  \"a\" : [ 1 , 2 ]\n}";
        let m = model(src);
        assert_eq!(canonical_text(&m, src).unwrap(), r#"{"b":1e3,"a":[1,2]}"#);
    }

    #[test]
    fn find_matches_keys_and_string_values() {
        let src = br#"{"hello":"world","k":[ "hello" ]}"#;
        let m = model(src);
        let ms = find(&m, src, "hello", Limits::DEFAULT).unwrap();
        assert_eq!(ms.len(), 2);
        assert_eq!(ms[0].role, MatchRole::Key);
        assert_eq!(ms[0].pointer, "/hello");
        assert_eq!(ms[1].role, MatchRole::Value);
        assert_eq!(ms[1].pointer, "/k/0");
    }

    #[test]
    fn deep_nesting_declines_typed() {
        let mut src = Vec::new();
        src.extend(std::iter::repeat_n(b'[', 200));
        src.push(b'0');
        src.extend(std::iter::repeat_n(b']', 200));
        assert!(!detect(&src, Limits::STRICT));
        let e = parse(&src, Limits::STRICT, true).unwrap_err();
        assert_eq!(e.class(), crate::error::ErrorClass::ResourceLimit);
    }

    #[test]
    fn model_roundtrips() {
        let src = br#"{"z":[true,false,null,1.5],"a":{"b":"c"}}"#;
        let m = model(src);
        let bytes = m.encode();
        assert_eq!(JsonModel::decode(&bytes).unwrap(), m);
        // Truncation is a typed decline, never a panic.
        for cut in 0..bytes.len() {
            let _ = JsonModel::decode(&bytes[..cut]);
        }
    }

    #[test]
    fn random_bytes_never_panic() {
        let mut x: u64 = 0x9E37_79B9_7F4A_7C15;
        for _ in 0..512 {
            let mut buf = vec![0u8; 256];
            for b in buf.iter_mut() {
                x ^= x << 13;
                x ^= x >> 7;
                x ^= x << 17;
                *b = (x & 0xFF) as u8;
            }
            let _ = detect(&buf, Limits::STRICT);
            if let Ok(m) = parse(&buf, Limits::STRICT, true) {
                let _ = m.encode();
                let _ = canonical_text(&m, &buf);
                let _ = find(&m, &buf, "a", Limits::STRICT);
            }
        }
    }
}