alpha2-diagnose 0.10.0

Format-neutral document diagnostics for alpha2: a document tree, a JSON reader that recovers, a schema walker, advice and fixes.
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
//! A document as a tree of values, read from text or built by a program.

use std::fmt;

use serde::de::{self, Deserializer, MapAccess, SeqAccess, Visitor};
use serde::ser::{SerializeMap, SerializeSeq, Serializer};
use serde::{Deserialize, Serialize};

use crate::path::{Path, Segment};

/// A place in the text a node was read from.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(schemars::JsonSchema))]
pub struct Span {
    /// Byte offset of the first byte, in UTF-8.
    pub offset: usize,
    /// Length in bytes.
    pub length: usize,
    /// Line of the first byte, from 1.
    pub line: u32,
    /// Column of the first byte, from 1, counted in characters (Unicode scalar values).
    pub column: u32,
}

impl Span {
    /// The byte offset just past the span.
    #[must_use]
    pub const fn end(&self) -> usize {
        self.offset + self.length
    }

    /// Whether `offset` lies inside the span (its end excluded, an empty span holding its start).
    #[must_use]
    pub const fn holds(&self, offset: usize) -> bool {
        offset >= self.offset && (offset < self.end() || offset == self.offset)
    }
}

/// One value of a document and where it was read from.
#[derive(Clone, Debug, PartialEq)]
pub struct Node {
    /// The value.
    pub value: Value,
    /// Where it was read from; `None` for a node built by a program or changed by a fix.
    pub span: Option<Span>,
}

/// The kinds of value a document holds.
#[derive(Clone, Debug, PartialEq)]
pub enum Value {
    /// `null`.
    Null,
    /// `true` or `false`.
    Bool(bool),
    /// A number, kept as it was spelt.
    Number(Number),
    /// A string.
    Text(String),
    /// An array.
    List(Vec<Node>),
    /// An object, its entries in document order; a repeated key is kept.
    Map(Vec<Entry>),
}

/// One key and value of a map.
#[derive(Clone, Debug, PartialEq)]
pub struct Entry {
    /// The key.
    pub key: String,
    /// Where the key was read from, its quotes included.
    pub key_span: Option<Span>,
    /// The value.
    pub value: Node,
}

/// What a number's spelling says about it.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum NumberKind {
    /// Digits only, with an optional minus, and not `-0`.
    Integer,
    /// A fraction, an exponent or `-0`.
    Decimal,
    /// `NaN` or an infinity, which JSON cannot spell; only a recovering reader makes one.
    NotFinite,
}

/// A number as it was spelt, so `1920.0` and `1920` stay apart.
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Number {
    text: String,
    kind: NumberKind,
}

impl Number {
    /// A number from a JSON spelling, or `None` when `text` is not one.
    #[must_use]
    pub fn parse(text: &str) -> Option<Self> {
        let kind = json_number_kind(text)?;
        Some(Self {
            text: text.to_owned(),
            kind,
        })
    }

    /// An integer.
    #[must_use]
    pub fn integer(value: i64) -> Self {
        Self {
            text: value.to_string(),
            kind: NumberKind::Integer,
        }
    }

    /// An unsigned integer.
    #[must_use]
    pub fn unsigned(value: u64) -> Self {
        Self {
            text: value.to_string(),
            kind: NumberKind::Integer,
        }
    }

    /// A float in its shortest spelling that reads back to it; `NaN` and infinities are kept
    /// as [`NumberKind::NotFinite`].
    #[must_use]
    pub fn float(value: f64) -> Self {
        if !value.is_finite() {
            return Self::not_finite(if value.is_nan() {
                "NaN"
            } else if value > 0.0 {
                "Infinity"
            } else {
                "-Infinity"
            });
        }
        let text = format!("{value:?}");
        let kind = json_number_kind(&text).unwrap_or(NumberKind::Decimal);
        Self { text, kind }
    }

    pub(crate) fn not_finite(text: &str) -> Self {
        Self {
            text: text.to_owned(),
            kind: NumberKind::NotFinite,
        }
    }

    /// The spelling.
    #[must_use]
    pub fn text(&self) -> &str {
        &self.text
    }

    /// What the spelling says.
    #[must_use]
    pub const fn kind(&self) -> NumberKind {
        self.kind
    }

    /// The value as a double (an infinity past its range, `NaN` for `NaN`).
    #[must_use]
    pub fn as_f64(&self) -> f64 {
        match self.text.as_str() {
            "NaN" => f64::NAN,
            "Infinity" => f64::INFINITY,
            "-Infinity" => f64::NEG_INFINITY,
            text => text.parse().unwrap_or(f64::NAN),
        }
    }

    /// The value when the spelling is an integer that fits in 128 bits.
    #[must_use]
    pub fn as_i128(&self) -> Option<i128> {
        (self.kind == NumberKind::Integer)
            .then(|| self.text.parse().ok())
            .flatten()
    }

    /// The value when it is a whole number, whatever the spelling (`1920.0` gives 1920).
    #[must_use]
    pub fn whole(&self) -> Option<i128> {
        if let Some(value) = self.as_i128() {
            return Some(value);
        }
        let value = self.as_f64();
        #[expect(
            clippy::cast_possible_truncation,
            reason = "checked against the range below"
        )]
        (value.is_finite() && value.fract() == 0.0 && value.abs() < 1e36).then_some(value as i128)
    }
}

impl fmt::Display for Number {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(&self.text)
    }
}

fn json_number_kind(text: &str) -> Option<NumberKind> {
    let bytes = text.as_bytes();
    let mut at = usize::from(bytes.first() == Some(&b'-'));
    let digits = |at: &mut usize| {
        let start = *at;
        while bytes.get(*at).is_some_and(u8::is_ascii_digit) {
            *at += 1;
        }
        *at - start
    };
    let integer_start = at;
    let integer_digits = digits(&mut at);
    if integer_digits == 0 || (integer_digits > 1 && bytes[integer_start] == b'0') {
        return None;
    }
    let mut decimal = false;
    if bytes.get(at) == Some(&b'.') {
        at += 1;
        if digits(&mut at) == 0 {
            return None;
        }
        decimal = true;
    }
    if matches!(bytes.get(at), Some(b'e' | b'E')) {
        at += 1;
        if matches!(bytes.get(at), Some(b'+' | b'-')) {
            at += 1;
        }
        if digits(&mut at) == 0 {
            return None;
        }
        decimal = true;
    }
    if at != bytes.len() {
        return None;
    }
    Some(if decimal || text == "-0" {
        NumberKind::Decimal
    } else {
        NumberKind::Integer
    })
}

impl Node {
    /// A node without a place in a text.
    #[must_use]
    pub const fn new(value: Value) -> Self {
        Self { value, span: None }
    }

    /// `null`.
    #[must_use]
    pub const fn null() -> Self {
        Self::new(Value::Null)
    }

    /// A boolean.
    #[must_use]
    pub const fn bool(value: bool) -> Self {
        Self::new(Value::Bool(value))
    }

    /// An integer.
    #[must_use]
    pub fn integer(value: i64) -> Self {
        Self::new(Value::Number(Number::integer(value)))
    }

    /// A float.
    #[must_use]
    pub fn float(value: f64) -> Self {
        Self::new(Value::Number(Number::float(value)))
    }

    /// A number.
    #[must_use]
    pub const fn number(value: Number) -> Self {
        Self::new(Value::Number(value))
    }

    /// A string.
    #[must_use]
    pub fn text(value: impl Into<String>) -> Self {
        Self::new(Value::Text(value.into()))
    }

    /// An array.
    #[must_use]
    pub const fn list(items: Vec<Self>) -> Self {
        Self::new(Value::List(items))
    }

    /// An object from keys and values, in order.
    #[must_use]
    pub fn map<K: Into<String>>(entries: impl IntoIterator<Item = (K, Self)>) -> Self {
        Self::new(Value::Map(
            entries
                .into_iter()
                .map(|(key, value)| Entry {
                    key: key.into(),
                    key_span: None,
                    value,
                })
                .collect(),
        ))
    }

    /// The JSON name of the value's kind: `null`, `boolean`, `number`, `string`, `array` or
    /// `object`.
    #[must_use]
    pub const fn kind_name(&self) -> &'static str {
        match self.value {
            Value::Null => "null",
            Value::Bool(_) => "boolean",
            Value::Number(_) => "number",
            Value::Text(_) => "string",
            Value::List(_) => "array",
            Value::Map(_) => "object",
        }
    }

    /// The string, when the node is one.
    #[must_use]
    pub fn as_str(&self) -> Option<&str> {
        match &self.value {
            Value::Text(text) => Some(text),
            _ => None,
        }
    }

    /// The number, when the node is one.
    #[must_use]
    pub const fn as_number(&self) -> Option<&Number> {
        match &self.value {
            Value::Number(number) => Some(number),
            _ => None,
        }
    }

    /// The boolean, when the node is one.
    #[must_use]
    pub const fn as_bool(&self) -> Option<bool> {
        match self.value {
            Value::Bool(value) => Some(value),
            _ => None,
        }
    }

    /// The items, when the node is an array.
    #[must_use]
    pub fn as_list(&self) -> Option<&[Self]> {
        match &self.value {
            Value::List(items) => Some(items),
            _ => None,
        }
    }

    /// The entries, when the node is an object.
    #[must_use]
    pub fn as_map(&self) -> Option<&[Entry]> {
        match &self.value {
            Value::Map(entries) => Some(entries),
            _ => None,
        }
    }

    /// The value under `key` in an object; of a repeated key, the last, as JSON readers keep.
    #[must_use]
    pub fn get(&self, key: &str) -> Option<&Self> {
        self.as_map()?
            .iter()
            .rev()
            .find(|entry| entry.key == key)
            .map(|entry| &entry.value)
    }

    /// The entry under `key` in an object (the last of a repeated key).
    #[must_use]
    pub fn entry(&self, key: &str) -> Option<&Entry> {
        self.as_map()?.iter().rev().find(|entry| entry.key == key)
    }

    /// The node a path names, its root name ignored.
    #[must_use]
    pub fn at(&self, path: &Path) -> Option<&Self> {
        path.segments()
            .iter()
            .try_fold(self, |node, segment| node.child(segment))
    }

    /// The node a path names, for change.
    pub fn at_mut(&mut self, path: &Path) -> Option<&mut Self> {
        path.segments()
            .iter()
            .try_fold(self, |node, segment| node.child_mut(segment))
    }

    /// The child a segment names.
    #[must_use]
    pub fn child(&self, segment: &Segment) -> Option<&Self> {
        match (segment, &self.value) {
            (Segment::Key(key), Value::Map(_)) => self.get(key),
            (Segment::Index(index), Value::List(items)) => items.get(*index),
            _ => None,
        }
    }

    /// The child a segment names, for change.
    pub fn child_mut(&mut self, segment: &Segment) -> Option<&mut Self> {
        match (segment, &mut self.value) {
            (Segment::Key(key), Value::Map(entries)) => entries
                .iter_mut()
                .rev()
                .find(|entry| entry.key == *key)
                .map(|entry| &mut entry.value),
            (Segment::Index(index), Value::List(items)) => items.get_mut(*index),
            _ => None,
        }
    }

    /// The number of nodes in the tree, this one included.
    #[must_use]
    pub fn count(&self) -> usize {
        1 + match &self.value {
            Value::List(items) => items.iter().map(Self::count).sum(),
            Value::Map(entries) => entries.iter().map(|entry| entry.value.count()).sum(),
            _ => 0,
        }
    }

    /// Whether two trees hold the same document, places ignored: keys in order, repeated keys
    /// kept, numbers by their spelling.
    #[must_use]
    pub fn same_document(&self, other: &Self) -> bool {
        match (&self.value, &other.value) {
            (Value::List(left), Value::List(right)) => {
                left.len() == right.len()
                    && left
                        .iter()
                        .zip(right)
                        .all(|(left, right)| left.same_document(right))
            }
            (Value::Map(left), Value::Map(right)) => {
                left.len() == right.len()
                    && left.iter().zip(right).all(|(left, right)| {
                        left.key == right.key && left.value.same_document(&right.value)
                    })
            }
            (left, right) => left == right,
        }
    }

    /// Whether two values are equal as JSON values: numbers by value, objects by their last
    /// value per key whatever the order.
    #[must_use]
    pub fn same_value(&self, other: &Self) -> bool {
        match (&self.value, &other.value) {
            (Value::Number(left), Value::Number(right)) => {
                match (left.as_i128(), right.as_i128()) {
                    (Some(left), Some(right)) => left == right,
                    _ => left.as_f64().total_cmp(&right.as_f64()).is_eq(),
                }
            }
            (Value::List(left), Value::List(right)) => {
                left.len() == right.len()
                    && left
                        .iter()
                        .zip(right)
                        .all(|(left, right)| left.same_value(right))
            }
            (Value::Map(left), Value::Map(_)) => {
                map_keys(self) == map_keys(other)
                    && left.iter().all(|entry| {
                        self.get(&entry.key)
                            .zip(other.get(&entry.key))
                            .is_some_and(|(left, right)| left.same_value(right))
                    })
            }
            (left, right) => left == right,
        }
    }

    /// The tree with every place removed, as a program would build it.
    #[must_use]
    pub fn without_spans(mut self) -> Self {
        self.clear_spans();
        self
    }

    fn clear_spans(&mut self) {
        self.span = None;
        match &mut self.value {
            Value::List(items) => items.iter_mut().for_each(Self::clear_spans),
            Value::Map(entries) => {
                for entry in entries {
                    entry.key_span = None;
                    entry.value.clear_spans();
                }
            }
            _ => {}
        }
    }
}

fn map_keys(node: &Node) -> Vec<&str> {
    let mut keys: Vec<&str> = node
        .as_map()
        .unwrap_or_default()
        .iter()
        .map(|entry| entry.key.as_str())
        .collect();
    keys.sort_unstable();
    keys.dedup();
    keys
}

impl From<bool> for Node {
    fn from(value: bool) -> Self {
        Self::bool(value)
    }
}

impl From<i64> for Node {
    fn from(value: i64) -> Self {
        Self::integer(value)
    }
}

impl From<u64> for Node {
    fn from(value: u64) -> Self {
        Self::number(Number::unsigned(value))
    }
}

impl From<usize> for Node {
    fn from(value: usize) -> Self {
        Self::number(Number::unsigned(value as u64))
    }
}

impl From<f64> for Node {
    fn from(value: f64) -> Self {
        Self::float(value)
    }
}

impl From<&str> for Node {
    fn from(value: &str) -> Self {
        Self::text(value)
    }
}

impl From<String> for Node {
    fn from(value: String) -> Self {
        Self::text(value)
    }
}

impl<T: Into<Self>> From<Vec<T>> for Node {
    fn from(items: Vec<T>) -> Self {
        Self::list(items.into_iter().map(Into::into).collect())
    }
}

impl Serialize for Node {
    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match &self.value {
            Value::Null => serializer.serialize_unit(),
            Value::Bool(value) => serializer.serialize_bool(*value),
            Value::Number(number) => {
                if let Some(value) = number.as_i128() {
                    if let Ok(value) = i64::try_from(value) {
                        return serializer.serialize_i64(value);
                    }
                    if let Ok(value) = u64::try_from(value) {
                        return serializer.serialize_u64(value);
                    }
                    return serializer.serialize_i128(value);
                }
                let value = number.as_f64();
                if value.is_finite() {
                    serializer.serialize_f64(value)
                } else {
                    serializer.serialize_unit()
                }
            }
            Value::Text(text) => serializer.serialize_str(text),
            Value::List(items) => {
                let mut seq = serializer.serialize_seq(Some(items.len()))?;
                for item in items {
                    seq.serialize_element(item)?;
                }
                seq.end()
            }
            Value::Map(entries) => {
                let mut map = serializer.serialize_map(Some(entries.len()))?;
                for entry in entries {
                    map.serialize_entry(&entry.key, &entry.value)?;
                }
                map.end()
            }
        }
    }
}

impl<'de> Deserialize<'de> for Node {
    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
        deserializer.deserialize_any(NodeVisitor)
    }
}

struct NodeVisitor;

impl<'de> Visitor<'de> for NodeVisitor {
    type Value = Node;

    fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str("any JSON value")
    }

    fn visit_unit<E: de::Error>(self) -> Result<Node, E> {
        Ok(Node::null())
    }

    fn visit_none<E: de::Error>(self) -> Result<Node, E> {
        Ok(Node::null())
    }

    fn visit_some<D: Deserializer<'de>>(self, deserializer: D) -> Result<Node, D::Error> {
        Node::deserialize(deserializer)
    }

    fn visit_bool<E: de::Error>(self, value: bool) -> Result<Node, E> {
        Ok(Node::bool(value))
    }

    fn visit_i64<E: de::Error>(self, value: i64) -> Result<Node, E> {
        Ok(Node::integer(value))
    }

    fn visit_u64<E: de::Error>(self, value: u64) -> Result<Node, E> {
        Ok(Node::from(value))
    }

    fn visit_i128<E: de::Error>(self, value: i128) -> Result<Node, E> {
        Ok(Node::number(Number {
            text: value.to_string(),
            kind: NumberKind::Integer,
        }))
    }

    fn visit_u128<E: de::Error>(self, value: u128) -> Result<Node, E> {
        Ok(Node::number(Number {
            text: value.to_string(),
            kind: NumberKind::Integer,
        }))
    }

    fn visit_f64<E: de::Error>(self, value: f64) -> Result<Node, E> {
        Ok(Node::float(value))
    }

    fn visit_str<E: de::Error>(self, value: &str) -> Result<Node, E> {
        Ok(Node::text(value))
    }

    fn visit_string<E: de::Error>(self, value: String) -> Result<Node, E> {
        Ok(Node::text(value))
    }

    fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Node, A::Error> {
        let mut items = Vec::new();
        while let Some(item) = seq.next_element()? {
            items.push(item);
        }
        Ok(Node::list(items))
    }

    fn visit_map<A: MapAccess<'de>>(self, mut map: A) -> Result<Node, A::Error> {
        let mut entries = Vec::new();
        while let Some((key, value)) = map.next_entry::<String, Node>()? {
            entries.push((key, value));
        }
        Ok(Node::map(entries))
    }
}

#[cfg(feature = "schema")]
impl schemars::JsonSchema for Node {
    fn schema_name() -> std::borrow::Cow<'static, str> {
        "Value".into()
    }

    fn json_schema(_: &mut schemars::SchemaGenerator) -> schemars::Schema {
        schemars::json_schema!({ "description": "Any JSON value." })
    }
}

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

    #[test]
    fn number_spelling_sets_its_kind() {
        assert_eq!(Number::parse("1920").unwrap().kind(), NumberKind::Integer);
        assert_eq!(Number::parse("1920.0").unwrap().kind(), NumberKind::Decimal);
        assert_eq!(Number::parse("1e3").unwrap().kind(), NumberKind::Decimal);
        assert_eq!(Number::parse("-0").unwrap().kind(), NumberKind::Decimal);
        assert_eq!(Number::parse("-12").unwrap().kind(), NumberKind::Integer);
        for bad in ["", "-", "01", "1.", ".5", "+1", "1e", "1e+", "0x10", "NaN"] {
            assert!(Number::parse(bad).is_none(), "{bad}");
        }
    }

    #[test]
    fn number_values() {
        assert_eq!(Number::parse("1920.0").unwrap().whole(), Some(1920));
        assert_eq!(Number::parse("1920.5").unwrap().whole(), None);
        assert_eq!(Number::parse("1920.0").unwrap().as_i128(), None);
        assert_eq!(
            Number::parse("18446744073709551616").unwrap().as_i128(),
            Some(1 << 64)
        );
        assert_eq!(Number::float(0.5).text(), "0.5");
        assert_eq!(Number::float(1.0).kind(), NumberKind::Decimal);
        assert_eq!(Number::float(f64::NAN).kind(), NumberKind::NotFinite);
    }

    #[test]
    fn get_takes_the_last_repeated_key() {
        let node = Node::map([("a", Node::integer(1)), ("a", Node::integer(2))]);
        assert_eq!(node.get("a"), Some(&Node::integer(2)));
        assert_eq!(node.count(), 3);
    }

    #[test]
    fn same_value_ignores_spelling_and_order() {
        let left = Node::map([("a", Node::integer(1)), ("b", Node::float(2.0))]);
        let right = Node::map([("b", Node::integer(2)), ("a", Node::float(1.0))]);
        assert!(left.same_value(&right));
        assert!(!left.same_document(&right));
    }

    #[test]
    fn at_follows_keys_and_indexes() {
        let node = Node::map([("a", Node::list(vec![Node::null(), Node::text("x")]))]);
        let path = Path::root("doc").key("a").index(1);
        assert_eq!(node.at(&path), Some(&Node::text("x")));
        assert_eq!(node.at(&Path::root("doc").key("b")), None);
    }
}