ayaml 1.0.0

A safe, pure-Rust YAML parser, event stream, value model, Serde layer, and emitter
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
//! Canonical hashing, semantic equality, `Value` comparison, and bounded key formatting for
//! [`TapeStorage`].

use std::fmt::Write as _;
use std::hash::{BuildHasher, Hash, Hasher};

use crate::limits::GlobalHasher;
use crate::value::{Value, key_text_full, push_quoted_key};

use super::{NodeTag, TapeStorage};

/// Canonical SipHash-1-3 hash of a tape node (matching `Value` / `Mapping` equality semantics).
///
/// Sequences and tags are traversed iteratively; nested mappings hash each entry recursively
/// (bounded by `MAX_DEPTH_CAP == 128`, matching `Mapping::hash`).
pub(crate) fn tape_node_hash(storage: &TapeStorage<'_>, root: usize) -> u64 {
    let mut hasher = GlobalHasher.build_hasher();
    hash_tape_node_into(storage, root, &mut hasher);
    hasher.finish()
}

fn hash_tape_node_into<H: Hasher>(storage: &TapeStorage<'_>, root: usize, state: &mut H) {
    enum Step {
        Node(usize),
        /// Order-independent XOR accumulator for mapping entries.
        MapEnd {
            acc: u64,
            cursor: usize,
            end: usize,
        },
    }

    // Fast path for non-collection scalar keys (99%+ of YAML mappings).
    let root_node = storage.node(root);
    match root_node.tag() {
        NodeTag::EmptyNull | NodeTag::ExplicitNull => {
            0_u8.hash(state);
            return;
        }
        NodeTag::Bool => {
            1_u8.hash(state);
            (root_node.payload() != 0).hash(state);
            return;
        }
        NodeTag::PosInt | NodeTag::NegInt | NodeTag::Float | NodeTag::WideInt => {
            2_u8.hash(state);
            if let Some(num) = storage.number_at(root) {
                num.hash(state);
            }
            return;
        }
        NodeTag::BorrowedStr | NodeTag::DecodedStr | NodeTag::LargeStr => {
            3_u8.hash(state);
            storage.str_at(root).unwrap_or("").hash(state);
            return;
        }
        _ => {}
    }

    let mut stack = vec![Step::Node(root)];
    while let Some(step) = stack.pop() {
        match step {
            Step::Node(idx) => {
                let node = storage.node(idx);
                match node.tag() {
                    NodeTag::EmptyNull | NodeTag::ExplicitNull => 0_u8.hash(state),
                    NodeTag::Bool => {
                        1_u8.hash(state);
                        (node.payload() != 0).hash(state);
                    }
                    NodeTag::PosInt | NodeTag::NegInt | NodeTag::Float | NodeTag::WideInt => {
                        2_u8.hash(state);
                        if let Some(num) = storage.number_at(idx) {
                            num.hash(state);
                        }
                    }
                    NodeTag::BorrowedStr | NodeTag::DecodedStr | NodeTag::LargeStr => {
                        3_u8.hash(state);
                        storage.str_at(idx).unwrap_or("").hash(state);
                    }
                    NodeTag::Sequence => {
                        let (len, end) = node.payload_pair();
                        4_u8.hash(state);
                        (len as usize).hash(state);
                        // Collect direct child indices in reverse for left-to-right hashing.
                        let base = stack.len();
                        let mut c = idx + 1;
                        while c < end as usize {
                            stack.push(Step::Node(c));
                            c = storage.node(c).subtree_end(c);
                        }
                        stack[base..].reverse();
                    }
                    NodeTag::Mapping => {
                        let (len, end) = node.payload_pair();
                        5_u8.hash(state);
                        (len as usize).hash(state);
                        stack.push(Step::MapEnd {
                            acc: 0,
                            cursor: idx + 1,
                            end: end as usize,
                        });
                    }
                    NodeTag::Tagged => {
                        6_u8.hash(state);
                        if let Some(tag) = storage.tag_at(idx) {
                            tag.hash(state);
                        }
                        stack.push(Step::Node(idx + 1));
                    }
                }
            }
            Step::MapEnd {
                mut acc,
                mut cursor,
                end,
            } => {
                // Compute each entry's hash with a fresh randomized SipHash-1-3 hasher and XOR
                // them for order-independent mapping hashing without O(N^2) collision chains.
                while cursor < end {
                    let key_idx = cursor;
                    let val_idx = storage.node(key_idx).subtree_end(key_idx);
                    cursor = storage.node(val_idx).subtree_end(val_idx);

                    let mut entry_hasher = GlobalHasher.build_hasher();
                    0x01_u8.hash(&mut entry_hasher);
                    entry_hasher.write_u64(tape_node_hash(storage, key_idx));
                    0x02_u8.hash(&mut entry_hasher);
                    entry_hasher.write_u64(tape_node_hash(storage, val_idx));
                    acc ^= entry_hasher.finish();
                }
                acc.hash(state);
            }
        }
    }
}

/// Canonical YAML semantic equality between two nodes in the same [`TapeStorage`],
/// matching `Value::eq` (including order-independent mapping equality and `NaN == NaN`).
pub(crate) fn tape_node_eq(storage: &TapeStorage<'_>, a: usize, b: usize) -> bool {
    if a == b {
        return true;
    }
    // Fast scalar check avoids allocating a stack for normal keys.
    if let Some(eq) = scalar_eq(storage, a, b) {
        return eq;
    }

    let mut stack = vec![(a, b)];
    while let Some((left, right)) = stack.pop() {
        if left == right {
            continue;
        }
        if let Some(eq) = scalar_eq(storage, left, right) {
            if !eq {
                return false;
            }
            continue;
        }
        let ln = storage.node(left);
        let rn = storage.node(right);
        match (ln.tag(), rn.tag()) {
            (NodeTag::Tagged, NodeTag::Tagged) => {
                if storage.tag_at(left) != storage.tag_at(right) {
                    return false;
                }
                stack.push((left + 1, right + 1));
            }
            (NodeTag::Sequence, NodeTag::Sequence) => {
                let (llen, lend) = ln.payload_pair();
                let (rlen, rend) = rn.payload_pair();
                if llen != rlen {
                    return false;
                }
                let mut lc = left + 1;
                let mut rc = right + 1;
                while lc < lend as usize && rc < rend as usize {
                    stack.push((lc, rc));
                    lc = storage.node(lc).subtree_end(lc);
                    rc = storage.node(rc).subtree_end(rc);
                }
            }
            (NodeTag::Mapping, NodeTag::Mapping) => {
                if !mapping_eq(storage, left, right) {
                    return false;
                }
            }
            _ => return false,
        }
    }
    true
}

#[inline]
fn scalar_eq(storage: &TapeStorage<'_>, a: usize, b: usize) -> Option<bool> {
    let ln = storage.node(a);
    let rn = storage.node(b);
    match (ln.tag(), rn.tag()) {
        (
            NodeTag::EmptyNull | NodeTag::ExplicitNull,
            NodeTag::EmptyNull | NodeTag::ExplicitNull,
        ) => Some(true),
        (NodeTag::Bool, NodeTag::Bool) => Some((ln.payload() != 0) == (rn.payload() != 0)),
        (
            NodeTag::PosInt | NodeTag::NegInt | NodeTag::Float | NodeTag::WideInt,
            NodeTag::PosInt | NodeTag::NegInt | NodeTag::Float | NodeTag::WideInt,
        ) => Some(storage.number_at(a) == storage.number_at(b)),
        (
            NodeTag::BorrowedStr | NodeTag::DecodedStr | NodeTag::LargeStr,
            NodeTag::BorrowedStr | NodeTag::DecodedStr | NodeTag::LargeStr,
        ) => Some(storage.str_at(a) == storage.str_at(b)),
        (
            NodeTag::Sequence | NodeTag::Mapping | NodeTag::Tagged,
            NodeTag::Sequence | NodeTag::Mapping | NodeTag::Tagged,
        ) => None,
        _ => Some(false),
    }
}

/// Order-independent mapping equality in O(N) average time using hashed buckets for large maps.
fn mapping_eq(storage: &TapeStorage<'_>, left: usize, right: usize) -> bool {
    let (llen, lend) = storage.node(left).payload_pair();
    let (rlen, rend) = storage.node(right).payload_pair();
    if llen != rlen {
        return false;
    }
    let len = llen as usize;
    if len == 0 {
        return true;
    }
    if len <= 12 {
        let mut lc = left + 1;
        while lc < lend as usize {
            let lk = lc;
            let lv = storage.node(lk).subtree_end(lk);
            lc = storage.node(lv).subtree_end(lv);

            let mut found = false;
            let mut rc = right + 1;
            while rc < rend as usize {
                let rk = rc;
                let rv = storage.node(rk).subtree_end(rk);
                rc = storage.node(rv).subtree_end(rv);
                if tape_node_eq(storage, lk, rk) {
                    if !tape_node_eq(storage, lv, rv) {
                        return false;
                    }
                    found = true;
                    break;
                }
            }
            if !found {
                return false;
            }
        }
        return true;
    }

    // Build a temporary SipHash-1-3 index of right's entries for O(N) lookup.
    let mut right_table: std::collections::HashMap<u64, Vec<(usize, usize)>, GlobalHasher> =
        std::collections::HashMap::with_capacity_and_hasher(len, GlobalHasher);
    let mut rc = right + 1;
    while rc < rend as usize {
        let rk = rc;
        let rv = storage.node(rk).subtree_end(rk);
        rc = storage.node(rv).subtree_end(rv);
        let hash = tape_node_hash(storage, rk);
        right_table.entry(hash).or_default().push((rk, rv));
    }

    let mut lc = left + 1;
    while lc < lend as usize {
        let lk = lc;
        let lv = storage.node(lk).subtree_end(lk);
        lc = storage.node(lv).subtree_end(lv);
        let hash = tape_node_hash(storage, lk);
        let Some(bucket) = right_table.get(&hash) else {
            return false;
        };
        let mut matched = false;
        for &(rk, rv) in bucket {
            if tape_node_eq(storage, lk, rk) {
                if !tape_node_eq(storage, lv, rv) {
                    return false;
                }
                matched = true;
                break;
            }
        }
        if !matched {
            return false;
        }
    }
    true
}

/// Compare a tape node against an owned [`Value`] without allocating a temporary `Value`.
pub(crate) fn tape_eq_value(storage: &TapeStorage<'_>, root: usize, target: &Value) -> bool {
    let mut stack = vec![(root, target)];
    while let Some((idx, val)) = stack.pop() {
        let node = storage.node(idx);
        match (node.tag(), val) {
            (NodeTag::EmptyNull | NodeTag::ExplicitNull, Value::Null) => {}
            (NodeTag::Bool, Value::Bool(b)) if (node.payload() != 0) == *b => {}
            (
                NodeTag::PosInt | NodeTag::NegInt | NodeTag::Float | NodeTag::WideInt,
                Value::Number(num),
            ) if storage.number_at(idx) == Some(*num) => {}
            (NodeTag::BorrowedStr | NodeTag::DecodedStr | NodeTag::LargeStr, Value::String(s))
                if storage.str_at(idx) == Some(s.as_str()) => {}
            (NodeTag::Tagged, Value::Tagged(tagged)) => {
                if storage.tag_at(idx) != Some(&tagged.tag) {
                    return false;
                }
                stack.push((idx + 1, &tagged.value));
            }
            (NodeTag::Sequence, Value::Sequence(items)) => {
                let (len, end) = node.payload_pair();
                if len as usize != items.len() {
                    return false;
                }
                let mut c = idx + 1;
                for item in items {
                    if c >= end as usize {
                        return false;
                    }
                    stack.push((c, item));
                    c = storage.node(c).subtree_end(c);
                }
            }
            (NodeTag::Mapping, Value::Mapping(map)) => {
                let (len, end) = node.payload_pair();
                if len as usize != map.len() {
                    return false;
                }
                let mut c = idx + 1;
                while c < end as usize {
                    let k_idx = c;
                    let v_idx = storage.node(k_idx).subtree_end(k_idx);
                    c = storage.node(v_idx).subtree_end(v_idx);
                    let key_val = storage.to_value_at(k_idx);
                    let Some(expected_val) = map.get(&key_val) else {
                        return false;
                    };
                    stack.push((v_idx, expected_val));
                }
            }
            _ => return false,
        }
    }
    true
}

/// Human-readable bounded diagnostic text for a mapping key in [`TapeStorage`],
/// matching [`crate::value::key_text`].
pub(crate) fn tape_key_text(storage: &TapeStorage<'_>, root: usize) -> String {
    let mut output = String::new();
    push_tape_key_text(storage, root, &mut output);
    output
}

fn push_tape_key_text(storage: &TapeStorage<'_>, root: usize, output: &mut String) {
    enum Step {
        Node(usize),
        Literal(&'static str),
        SeqRest { cursor: usize, end: usize },
        MapRest { cursor: usize, end: usize },
    }

    let mut stack = vec![Step::Node(root)];
    while let Some(step) = stack.pop() {
        if key_text_full(output) {
            return;
        }
        match step {
            Step::Literal(s) => output.push_str(s),
            Step::Node(idx) => {
                let node = storage.node(idx);
                match node.tag() {
                    NodeTag::EmptyNull | NodeTag::ExplicitNull => output.push_str("null"),
                    NodeTag::Bool => {
                        let _ = write!(output, "{}", node.payload() != 0);
                    }
                    NodeTag::PosInt | NodeTag::NegInt | NodeTag::Float | NodeTag::WideInt => {
                        if let Some(num) = storage.number_at(idx) {
                            let _ = write!(output, "{num}");
                        }
                    }
                    NodeTag::BorrowedStr | NodeTag::DecodedStr | NodeTag::LargeStr => {
                        push_quoted_key(storage.str_at(idx).unwrap_or(""), output);
                    }
                    NodeTag::Sequence => {
                        let (_, end) = node.payload_pair();
                        output.push('[');
                        if idx + 1 < end as usize {
                            let first = idx + 1;
                            let next = storage.node(first).subtree_end(first);
                            stack.push(Step::Literal("]"));
                            if next < end as usize {
                                stack.push(Step::SeqRest {
                                    cursor: next,
                                    end: end as usize,
                                });
                            }
                            stack.push(Step::Node(first));
                        } else {
                            output.push(']');
                        }
                    }
                    NodeTag::Mapping => {
                        let (_, end) = node.payload_pair();
                        output.push('{');
                        if idx + 1 < end as usize {
                            let k_idx = idx + 1;
                            let v_idx = storage.node(k_idx).subtree_end(k_idx);
                            let next = storage.node(v_idx).subtree_end(v_idx);
                            stack.push(Step::Literal("}"));
                            if next < end as usize {
                                stack.push(Step::MapRest {
                                    cursor: next,
                                    end: end as usize,
                                });
                            }
                            stack.push(Step::Node(v_idx));
                            stack.push(Step::Literal(": "));
                            stack.push(Step::Node(k_idx));
                        } else {
                            output.push('}');
                        }
                    }
                    NodeTag::Tagged => {
                        if let Some(tag) = storage.tag_at(idx) {
                            let _ = write!(output, "{tag} ");
                        }
                        stack.push(Step::Node(idx + 1));
                    }
                }
            }
            Step::SeqRest { cursor, end } => {
                output.push_str(", ");
                if key_text_full(output) {
                    output.push('…');
                    return;
                }
                let next = storage.node(cursor).subtree_end(cursor);
                if next < end {
                    stack.push(Step::SeqRest { cursor: next, end });
                }
                stack.push(Step::Node(cursor));
            }
            Step::MapRest { cursor, end } => {
                output.push_str(", ");
                if key_text_full(output) {
                    output.push('…');
                    return;
                }
                let k_idx = cursor;
                let v_idx = storage.node(k_idx).subtree_end(k_idx);
                let next = storage.node(v_idx).subtree_end(v_idx);
                if next < end {
                    stack.push(Step::MapRest { cursor: next, end });
                }
                stack.push(Step::Node(v_idx));
                stack.push(Step::Literal(": "));
                stack.push(Step::Node(k_idx));
            }
        }
    }
}