neco-kdl 0.5.0

zero dependency KDL v2 parser, serializer, and document builder
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
/// Format-agnostic value type for KDL conversion.
///
/// This enum serves as an intermediate representation between KDL documents
/// and other data formats (JSON, CBOR, etc.) without requiring external
/// dependencies in the neco-kdl crate.
///
/// Integer(i64) and Float(f64) are kept distinct, unlike JSON's single Number(f64).
#[derive(Debug, Clone, PartialEq)]
pub enum Value {
    Null,
    Bool(bool),
    Integer(i64),
    Float(f64),
    String(String),
    Array(Vec<Value>),
    /// Order-preserving map of key-value pairs.
    Object(Vec<(String, Value)>),
}

use crate::{KdlDocument, KdlEntry, KdlNode, KdlNumber, KdlValue};

#[derive(Debug, Clone, PartialEq)]
/// Failure returned when a KDL document and `Value` cannot be converted losslessly.
pub struct ValueError {
    /// The conversion rule that rejected the input.
    pub reason: ValueErrorReason,
}

impl ValueError {
    fn new(reason: ValueErrorReason) -> Self {
        Self { reason }
    }

    /// Returns the conversion rule that rejected the input.
    pub fn reason(&self) -> &ValueErrorReason {
        &self.reason
    }
}

impl core::fmt::Display for ValueError {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "KDL value conversion failed: {:?}", self.reason)
    }
}

impl std::error::Error for ValueError {}

#[derive(Debug, Clone, PartialEq)]
/// Explicit reasons that a KDL document and `Value` cannot be converted losslessly.
pub enum ValueErrorReason {
    TopLevelMustBeObject,
    NestedCollectionInArray,
    EmptyArrayCannotBeConverted,
    PropertyCannotBeConverted,
    MixedArgumentsAndChildren,
    UnsupportedTypeAnnotation(String),
    NumberCannotBeConverted(String),
}

/// Converts a `Value` into a `KdlDocument`.
///
/// Only `Value::Object` can be represented as a KDL document (since KDL is a
/// collection of named nodes). Each key in the object becomes a node name.
///
/// Conversion rules:
/// - Primitive values (Bool/Integer/Float/String/Null) → single positional argument
/// - Array values → multiple positional arguments
/// - Nested Object values → children block
///
/// # Errors
///
/// Returns an error for a non-Object top level, an empty Array, a nested
/// collection inside an Array, or a number that cannot be constructed.
pub fn value_to_kdl_document(value: &Value) -> Result<KdlDocument, ValueError> {
    match value {
        Value::Object(fields) => {
            let nodes = fields
                .iter()
                .map(|(key, val)| value_to_kdl_node(key, val))
                .collect::<Result<Vec<_>, _>>()?;
            Ok(KdlDocument { nodes })
        }
        _ => Err(ValueError::new(ValueErrorReason::TopLevelMustBeObject)),
    }
}

/// Converts a `KdlDocument` back into a `Value`.
///
/// The document is always decoded as a `Value::Object` where each node name
/// becomes a key.
///
/// Decoding rules for node arguments:
/// - No positional arguments and no children → `Value::Null`
/// - Exactly one positional argument, no children → scalar value
/// - Two or more positional arguments, no children → `Value::Array`
/// - Children block (no positional arguments) → nested `Value::Object`
///
/// # Errors
///
/// Returns an error if a node cannot be decoded (e.g., mixed arguments and
/// children, or an unrecognised value type).
pub fn kdl_document_to_value(doc: &KdlDocument) -> Result<Value, ValueError> {
    let mut fields = Vec::with_capacity(doc.nodes.len());
    for node in &doc.nodes {
        let val = kdl_node_to_value(node)?;
        fields.push((node.name.clone(), val));
    }
    Ok(Value::Object(fields))
}

// ---------------------------------------------------------------------------
// Internal helpers
// ---------------------------------------------------------------------------

/// A sentinel type-annotation string used to distinguish `Value::Float` from
/// `Value::Integer` when both are stored as KDL numbers.
///
/// KDL's `KdlNumber` stores a raw string; we use a `(f64)` type annotation on
/// the argument to mark that the original value was a `Value::Float`.
const FLOAT_TYPE: &str = "f64";

/// Converts a single key-value pair into a `KdlNode`.
fn value_to_kdl_node(key: &str, value: &Value) -> Result<KdlNode, ValueError> {
    match value {
        // Nested object → children block
        Value::Object(fields) => {
            let children = fields
                .iter()
                .map(|(k, v)| value_to_kdl_node(k, v))
                .collect::<Result<Vec<_>, _>>()?;
            Ok(KdlNode {
                ty: None,
                name: key.to_string(),
                entries: Vec::new(),
                children: Some(children),
            })
        }

        // Array → multiple positional arguments
        Value::Array(items) => {
            if items.is_empty() {
                return Err(ValueError::new(
                    ValueErrorReason::EmptyArrayCannotBeConverted,
                ));
            }
            let entries = items
                .iter()
                .map(primitive_to_argument)
                .collect::<Result<Vec<_>, _>>()?;
            Ok(KdlNode {
                ty: None,
                name: key.to_string(),
                entries,
                children: None,
            })
        }

        // Primitive → single positional argument
        _ => {
            let entry = primitive_to_argument(value)?;
            Ok(KdlNode {
                ty: None,
                name: key.to_string(),
                entries: vec![entry],
                children: None,
            })
        }
    }
}

/// Converts a primitive `Value` (non-Array, non-Object) into a `KdlEntry::Argument`.
///
/// `Value::Float` gets a `(f64)` type annotation so that round-trip decoding
/// can distinguish it from `Value::Integer`.
fn primitive_to_argument(value: &Value) -> Result<KdlEntry, ValueError> {
    match value {
        Value::Null => Ok(KdlEntry::Argument {
            ty: None,
            value: KdlValue::Null,
        }),
        Value::Bool(b) => Ok(KdlEntry::Argument {
            ty: None,
            value: KdlValue::Bool(*b),
        }),
        Value::Integer(i) => Ok(KdlEntry::Argument {
            ty: None,
            value: KdlValue::Number(i64_to_kdl_number(*i)?),
        }),
        Value::Float(f) => Ok(KdlEntry::Argument {
            // Use a (f64) type annotation to distinguish from Integer on decode.
            ty: Some(FLOAT_TYPE.to_string()),
            value: KdlValue::Number(f64_to_kdl_number(*f)?),
        }),
        Value::String(s) => Ok(KdlEntry::Argument {
            ty: None,
            value: KdlValue::String(s.clone()),
        }),
        Value::Array(_) | Value::Object(_) => {
            Err(ValueError::new(ValueErrorReason::NestedCollectionInArray))
        }
    }
}

/// Decodes a `KdlNode` into a `Value`.
fn kdl_node_to_value(node: &KdlNode) -> Result<Value, ValueError> {
    if node
        .entries
        .iter()
        .any(|entry| matches!(entry, KdlEntry::Property { .. }))
    {
        return Err(ValueError::new(ValueErrorReason::PropertyCannotBeConverted));
    }
    if let Some(ty) = &node.ty {
        return Err(ValueError::new(
            ValueErrorReason::UnsupportedTypeAnnotation(ty.clone()),
        ));
    }
    for entry in &node.entries {
        let KdlEntry::Argument {
            ty: Some(ty),
            value,
        } = entry
        else {
            continue;
        };
        if ty != FLOAT_TYPE || !matches!(value, KdlValue::Number(_)) {
            return Err(ValueError::new(
                ValueErrorReason::UnsupportedTypeAnnotation(ty.clone()),
            ));
        }
    }

    let args: Vec<&KdlEntry> = node
        .entries
        .iter()
        .filter(|e| matches!(e, KdlEntry::Argument { .. }))
        .collect();

    match (args.len(), node.children.as_ref()) {
        // No arguments and no children → null
        (0, None) => Ok(Value::Null),

        // Children block only → nested Object
        (0, Some(children)) => {
            let mut fields = Vec::with_capacity(children.len());
            for child in children {
                let v = kdl_node_to_value(child)?;
                fields.push((child.name.clone(), v));
            }
            Ok(Value::Object(fields))
        }

        // Exactly one argument → scalar
        (1, None) => kdl_entry_to_value(args[0]),

        // Multiple arguments → Array
        (_, None) => {
            let items = args
                .iter()
                .map(|e| kdl_entry_to_value(e))
                .collect::<Result<Vec<_>, _>>()?;
            Ok(Value::Array(items))
        }

        // Both arguments and children → ambiguous, treat as error
        (_, Some(_)) => Err(ValueError::new(ValueErrorReason::MixedArgumentsAndChildren)),
    }
}

/// Decodes a single `KdlEntry::Argument` into a scalar `Value`.
fn kdl_entry_to_value(entry: &KdlEntry) -> Result<Value, ValueError> {
    let (ty, kdl_val) = match entry {
        KdlEntry::Argument { ty, value } => (ty.as_deref(), value),
        KdlEntry::Property { .. } => {
            return Err(ValueError::new(ValueErrorReason::PropertyCannotBeConverted))
        }
    };

    match kdl_val {
        KdlValue::Null => Ok(Value::Null),
        KdlValue::Bool(b) => Ok(Value::Bool(*b)),
        KdlValue::String(s) => Ok(Value::String(s.clone())),
        KdlValue::Number(n) => {
            // A (f64) type annotation marks an explicitly floating-point value.
            if ty == Some(FLOAT_TYPE) {
                match n.as_f64() {
                    Some(f) => Ok(Value::Float(f)),
                    None => Err(ValueError::new(ValueErrorReason::NumberCannotBeConverted(
                        n.raw().to_string(),
                    ))),
                }
            } else {
                // No annotation: prefer integer interpretation, fall back to float.
                match n.as_i64() {
                    Some(i) => Ok(Value::Integer(i)),
                    None => match n.as_f64() {
                        Some(f) => Ok(Value::Float(f)),
                        None => Err(ValueError::new(ValueErrorReason::NumberCannotBeConverted(
                            n.raw().to_string(),
                        ))),
                    },
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Number construction helpers
// ---------------------------------------------------------------------------

fn i64_to_kdl_number(i: i64) -> Result<KdlNumber, ValueError> {
    let raw = i.to_string();
    KdlNumber::new(raw.clone(), Some(i), Some(i as f64))
        .map_err(|_| ValueError::new(ValueErrorReason::NumberCannotBeConverted(raw)))
}

fn f64_to_kdl_number(f: f64) -> Result<KdlNumber, ValueError> {
    // Produce a raw string that round-trips through the KDL parser.
    // We always emit at least one decimal digit so the parser recognises it as
    // a float (e.g. "2.5", "1.0").
    let raw = format_f64(f);
    KdlNumber::new(raw.clone(), None, Some(f))
        .map_err(|_| ValueError::new(ValueErrorReason::NumberCannotBeConverted(raw)))
}

/// Formats an f64 as a KDL-parseable decimal string with an explicit dot.
fn format_f64(f: f64) -> String {
    if f.is_nan() {
        return "#nan".to_string();
    }
    if f.is_infinite() {
        return if f > 0.0 {
            "#inf".to_string()
        } else {
            "#-inf".to_string()
        };
    }

    // Use Rust's default Display which includes the decimal point when
    // needed.  For whole numbers like 1.0, Display produces "1" : we
    // append ".0" explicitly.
    let s = format!("{f}");
    if s.contains('.') || s.contains('e') || s.contains('E') || s.starts_with('#') {
        s
    } else {
        format!("{s}.0")
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    // Helper: round-trip a Value through KdlDocument and back.
    fn roundtrip(v: &Value) -> Value {
        let doc = value_to_kdl_document(v).expect("encode failed");
        kdl_document_to_value(&doc).expect("decode failed")
    }

    // -----------------------------------------------------------------------
    // Single scalar node: text "hello"
    // -----------------------------------------------------------------------

    #[test]
    fn roundtrip_single_string_node() {
        let input = Value::Object(vec![(
            "text".to_string(),
            Value::String("hello".to_string()),
        )]);
        let output = roundtrip(&input);
        assert_eq!(input, output);
    }

    // -----------------------------------------------------------------------
    // Multiple-value node: langs "en" "ja"  →  Array
    // -----------------------------------------------------------------------

    #[test]
    fn roundtrip_array_node() {
        let input = Value::Object(vec![(
            "langs".to_string(),
            Value::Array(vec![
                Value::String("en".to_string()),
                Value::String("ja".to_string()),
            ]),
        )]);
        let output = roundtrip(&input);
        assert_eq!(input, output);
    }

    // -----------------------------------------------------------------------
    // Nested Object → children block
    // -----------------------------------------------------------------------

    #[test]
    fn roundtrip_nested_object() {
        let root_ref = Value::Object(vec![
            ("cid".to_string(), Value::String("bafyCID".to_string())),
            ("uri".to_string(), Value::String("at://x".to_string())),
        ]);
        let input = Value::Object(vec![(
            "reply".to_string(),
            Value::Object(vec![("root".to_string(), root_ref)]),
        )]);
        let output = roundtrip(&input);
        assert_eq!(input, output);
    }

    // -----------------------------------------------------------------------
    // Integer vs Float are preserved
    // -----------------------------------------------------------------------

    #[test]
    fn roundtrip_integer() {
        let input = Value::Object(vec![("count".to_string(), Value::Integer(42))]);
        let output = roundtrip(&input);
        assert_eq!(input, output);
        // Verify the decoded value is still Integer, not Float.
        match &output {
            Value::Object(fields) => {
                assert_eq!(fields[0].1, Value::Integer(42));
            }
            _ => panic!("expected Object"),
        }
    }

    #[test]
    fn roundtrip_float() {
        let input = Value::Object(vec![("ratio".to_string(), Value::Float(2.5))]);
        let output = roundtrip(&input);
        match &output {
            Value::Object(fields) => {
                if let Value::Float(f) = fields[0].1 {
                    assert!((f - 2.5_f64).abs() < 1e-10);
                } else {
                    panic!("expected Float, got {:?}", fields[0].1);
                }
            }
            _ => panic!("expected Object"),
        }
    }

    #[test]
    fn non_finite_floats_are_accepted() {
        for (value, raw) in [
            (f64::INFINITY, "#inf"),
            (f64::NEG_INFINITY, "#-inf"),
            (f64::NAN, "#nan"),
        ] {
            let parsed = crate::parse(&format!("number {raw}")).unwrap();
            let decoded = kdl_document_to_value(&parsed).unwrap();
            let Value::Object(fields) = decoded else {
                panic!("expected object")
            };
            let Value::Float(decoded) = fields[0].1 else {
                panic!("expected float")
            };
            if value.is_nan() {
                assert!(decoded.is_nan());
            } else {
                assert_eq!(decoded, value);
            }

            let input = Value::Object(vec![("number".to_string(), Value::Float(value))]);
            let document = value_to_kdl_document(&input).unwrap();
            let KdlEntry::Argument {
                value: KdlValue::Number(number),
                ..
            } = &document.nodes[0].entries[0]
            else {
                panic!("expected number")
            };
            assert_eq!(number.raw(), raw);

            let decoded = kdl_document_to_value(&document).unwrap();
            let Value::Object(fields) = decoded else {
                panic!("expected object")
            };
            let Value::Float(decoded) = fields[0].1 else {
                panic!("expected float")
            };
            if value.is_nan() {
                assert!(decoded.is_nan());
            } else {
                assert_eq!(decoded, value);
            }
        }
    }

    #[test]
    fn integer_and_float_are_distinct() {
        let int_input = Value::Object(vec![("n".to_string(), Value::Integer(1))]);
        let flt_input = Value::Object(vec![("n".to_string(), Value::Float(1.0))]);

        let int_out = roundtrip(&int_input);
        let flt_out = roundtrip(&flt_input);

        // After round-trip they must remain distinct.
        assert_ne!(int_out, flt_out);

        match &int_out {
            Value::Object(f) => assert!(matches!(f[0].1, Value::Integer(_))),
            _ => panic!(),
        }
        match &flt_out {
            Value::Object(f) => assert!(matches!(f[0].1, Value::Float(_))),
            _ => panic!(),
        }
    }

    // -----------------------------------------------------------------------
    // Null
    // -----------------------------------------------------------------------

    #[test]
    fn roundtrip_null() {
        let input = Value::Object(vec![("deleted".to_string(), Value::Null)]);
        let output = roundtrip(&input);
        assert_eq!(input, output);
    }

    // -----------------------------------------------------------------------
    // Bool
    // -----------------------------------------------------------------------

    #[test]
    fn roundtrip_bool() {
        let input = Value::Object(vec![
            ("active".to_string(), Value::Bool(true)),
            ("deleted".to_string(), Value::Bool(false)),
        ]);
        let output = roundtrip(&input);
        assert_eq!(input, output);
    }

    // -----------------------------------------------------------------------
    // Negative integer
    // -----------------------------------------------------------------------

    #[test]
    fn roundtrip_negative_integer() {
        let input = Value::Object(vec![("offset".to_string(), Value::Integer(-7))]);
        let output = roundtrip(&input);
        assert_eq!(input, output);
    }

    // -----------------------------------------------------------------------
    // Error: non-Object at top level
    // -----------------------------------------------------------------------

    #[test]
    fn top_level_non_object_is_error() {
        let result = value_to_kdl_document(&Value::String("oops".to_string()));
        assert_eq!(
            result.unwrap_err().reason,
            ValueErrorReason::TopLevelMustBeObject
        );
    }

    // -----------------------------------------------------------------------
    // Round-trip via normalize() + parse() (full text serialization)
    // -----------------------------------------------------------------------

    #[test]
    fn roundtrip_via_text() {
        use crate::{normalize, parse};

        let input = Value::Object(vec![
            ("name".to_string(), Value::String("Alice".to_string())),
            ("age".to_string(), Value::Integer(30)),
            ("score".to_string(), Value::Float(9.5)),
            ("active".to_string(), Value::Bool(true)),
            (
                "meta".to_string(),
                Value::Object(vec![(
                    "role".to_string(),
                    Value::String("admin".to_string()),
                )]),
            ),
        ]);

        let doc = value_to_kdl_document(&input).unwrap();
        let text = normalize(&doc);
        let doc2 = parse(&text).unwrap();
        let output = kdl_document_to_value(&doc2).unwrap();

        // Check field by field (Float comparison needs epsilon).
        match (&input, &output) {
            (Value::Object(a), Value::Object(b)) => {
                assert_eq!(a.len(), b.len());
                assert_eq!(a[0], b[0]); // name
                assert_eq!(a[1], b[1]); // age
                                        // score: float comparison
                if let (Value::Float(fa), Value::Float(fb)) = (&a[2].1, &b[2].1) {
                    assert!((fa - fb).abs() < 1e-10);
                } else {
                    panic!("expected Float for score");
                }
                assert_eq!(a[3], b[3]); // active
                assert_eq!(a[4], b[4]); // meta
            }
            _ => panic!("expected Object"),
        }
    }

    // -----------------------------------------------------------------------
    // Node with no arguments decodes as Null
    // -----------------------------------------------------------------------

    #[test]
    fn empty_node_decoded_as_null() {
        // Build a document with a node that has no arguments.
        let doc = KdlDocument {
            nodes: vec![KdlNode {
                ty: None,
                name: "empty".to_string(),
                entries: vec![],
                children: None,
            }],
        };
        let val = kdl_document_to_value(&doc).unwrap();
        assert_eq!(val, Value::Object(vec![("empty".to_string(), Value::Null)]));
    }

    #[test]
    fn empty_object_roundtrips_through_structure_and_text() {
        use crate::{parse, serialize};

        let input = Value::Object(vec![("empty".to_string(), Value::Object(vec![]))]);
        let doc = value_to_kdl_document(&input).unwrap();
        assert_eq!(doc.nodes[0].children, Some(vec![]));
        assert_eq!(kdl_document_to_value(&doc).unwrap(), input);

        let text = serialize(&doc);
        assert_eq!(text, "empty {\n}\n");
        let reparsed = parse(&text).unwrap();
        assert_eq!(reparsed.nodes[0].children, Some(vec![]));
        assert_eq!(kdl_document_to_value(&reparsed).unwrap(), input);
    }

    #[test]
    fn empty_array_is_rejected() {
        let input = Value::Object(vec![("empty".to_string(), Value::Array(vec![]))]);
        assert_eq!(
            value_to_kdl_document(&input).unwrap_err().reason,
            ValueErrorReason::EmptyArrayCannotBeConverted
        );
    }

    #[test]
    fn nested_collections_in_array_are_rejected() {
        for nested in [Value::Array(vec![]), Value::Object(vec![])] {
            let input = Value::Object(vec![(
                "items".to_string(),
                Value::Array(vec![Value::Integer(1), nested]),
            )]);
            assert_eq!(
                value_to_kdl_document(&input).unwrap_err().reason,
                ValueErrorReason::NestedCollectionInArray
            );
        }
    }

    #[test]
    fn properties_are_rejected_with_and_without_arguments() {
        for input in ["node key=1", "node 1 key=2"] {
            let doc = crate::parse(input).unwrap();
            assert_eq!(
                kdl_document_to_value(&doc).unwrap_err().reason,
                ValueErrorReason::PropertyCannotBeConverted
            );
        }
    }

    #[test]
    fn unsupported_type_annotations_are_rejected() {
        for (input, expected) in [
            (
                "(node_ty)node 1",
                ValueErrorReason::UnsupportedTypeAnnotation("node_ty".to_string()),
            ),
            (
                "node (entry_ty)1",
                ValueErrorReason::UnsupportedTypeAnnotation("entry_ty".to_string()),
            ),
            (
                "node (f64)\"text\"",
                ValueErrorReason::UnsupportedTypeAnnotation("f64".to_string()),
            ),
        ] {
            let doc = crate::parse(input).unwrap();
            assert_eq!(kdl_document_to_value(&doc).unwrap_err().reason, expected);
        }
    }

    #[test]
    fn mixed_arguments_and_children_are_rejected() {
        let doc = crate::parse("node 1 { child }").unwrap();
        assert_eq!(
            kdl_document_to_value(&doc).unwrap_err().reason,
            ValueErrorReason::MixedArgumentsAndChildren
        );
    }

    #[test]
    fn number_decode_preserves_supported_values_and_rejects_uninterpreted_raw() {
        let hex = crate::parse("node 0xff").unwrap();
        assert_eq!(
            kdl_document_to_value(&hex).unwrap(),
            Value::Object(vec![("node".to_string(), Value::Integer(255))])
        );

        let infinity = crate::parse("node #inf").unwrap();
        let decoded = kdl_document_to_value(&infinity).unwrap();
        let Value::Object(fields) = decoded else {
            panic!("expected object")
        };
        let Value::Float(value) = fields[0].1 else {
            panic!("expected float")
        };
        assert!(value.is_infinite() && value > 0.0);

        let invalid = KdlDocument {
            nodes: vec![KdlNode {
                ty: None,
                name: "node".to_string(),
                entries: vec![KdlEntry::Argument {
                    ty: None,
                    value: KdlValue::Number(KdlNumber {
                        raw: "999999999999999999999999999999999999999999".to_string(),
                        as_i64: None,
                        as_f64: None,
                    }),
                }],
                children: None,
            }],
        };
        assert_eq!(
            kdl_document_to_value(&invalid).unwrap_err().reason,
            ValueErrorReason::NumberCannotBeConverted(
                "999999999999999999999999999999999999999999".to_string()
            )
        );
    }

    #[test]
    fn rejection_priority_is_deterministic() {
        let argument = KdlEntry::Argument {
            ty: Some("entry_ty".to_string()),
            value: KdlValue::Number(KdlNumber {
                raw: "1".to_string(),
                as_i64: Some(1),
                as_f64: Some(1.0),
            }),
        };
        let property = KdlEntry::Property {
            key: "key".to_string(),
            ty: None,
            value: KdlValue::Bool(true),
        };
        let child = KdlNode {
            ty: None,
            name: "child".to_string(),
            entries: vec![],
            children: None,
        };
        let reason = |ty, entries| {
            let doc = KdlDocument {
                nodes: vec![KdlNode {
                    ty,
                    name: "node".to_string(),
                    entries,
                    children: Some(vec![child.clone()]),
                }],
            };
            kdl_document_to_value(&doc).unwrap_err().reason
        };

        assert_eq!(
            reason(
                Some("node_ty".to_string()),
                vec![argument.clone(), property]
            ),
            ValueErrorReason::PropertyCannotBeConverted
        );
        assert_eq!(
            reason(Some("node_ty".to_string()), vec![argument.clone()]),
            ValueErrorReason::UnsupportedTypeAnnotation("node_ty".to_string())
        );
        assert_eq!(
            reason(None, vec![argument.clone()]),
            ValueErrorReason::UnsupportedTypeAnnotation("entry_ty".to_string())
        );
        let untyped = KdlEntry::Argument {
            ty: None,
            value: argument.value().clone(),
        };
        assert_eq!(
            reason(None, vec![untyped]),
            ValueErrorReason::MixedArgumentsAndChildren
        );
    }
}