ktav 0.6.0

Ktav — a plain configuration format. Three rules, zero indentation, zero quoting. Serde-native.
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
//! Canonical writer — emits a deterministic byte sequence for any [`Value`]
//! per spec § 5.9.
//!
//! The canonical form is:
//! - LF-only line endings (no `CR`).
//! - 4-space indent per nesting level.
//! - Trailing `LF` at end of document (empty Object root → zero bytes).
//! - No comments.
//! - No inline compounds (except empty `{}` / `[]`).
//! - Numbers in canonical form (Integer: base-10; Float: shortest decimal).
//! - Multi-line strings prefer verbatim `((…))`.
//!
//! Two writer-conforming implementations fed the same Value MUST produce
//! identical output (§ 8.2).

use crate::error::{Error, Result};
use crate::value::{ObjectMap, Value};

// ---------------------------------------------------------------------------
// Public entry point
// ---------------------------------------------------------------------------

/// Emit a canonical Ktav serialisation of `value` (spec § 5.9).
///
/// The top-level value must be an Object or an Array (§ 5.0.1).
/// Returns an error for any other variant, or if a String contains a
/// `CR` byte (not representable in canonical form, § 5.9.7).
pub fn emit_canonical(value: &Value) -> Result<String> {
    let mut out = String::with_capacity(estimate_size(value));
    match value {
        Value::Object(o) if o.is_empty() => { /* § 5.9.3: empty Object → zero bytes */ }
        Value::Object(o) => emit_object_pairs(o, 0, &mut out)?,
        Value::Array(items) if items.is_empty() => {
            // § 5.9.3: empty Array root → `[]\n`
            out.push_str("[]\n");
        }
        Value::Array(items) => emit_array_root(items, &mut out)?,
        _ => {
            return Err(Error::Message(
                "top-level value must be an Object or an Array".into(),
            ))
        }
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// § 5.9.3 Root-level emission
// ---------------------------------------------------------------------------

/// Emit an Object's pairs at the given indent level (root uses 0).
fn emit_object_pairs(obj: &ObjectMap, indent: usize, out: &mut String) -> Result<()> {
    for (k, v) in obj {
        emit_pair(k, v, indent, out)?;
    }
    Ok(())
}

/// Emit a root-level Array. Items are bare at indent 0 unless the first
/// item is itself a non-empty compound (§ 5.9.3 lone-`{`/`[` wrap).
fn emit_array_root(items: &[Value], out: &mut String) -> Result<()> {
    let needs_wrap = !items.is_empty() && first_item_needs_wrap(&items[0]);
    if needs_wrap {
        out.push_str("[\n");
        for item in items {
            emit_array_item(item, 1, out)?;
        }
        out.push_str("]\n");
    } else {
        for item in items {
            emit_array_item(item, 0, out)?;
        }
    }
    Ok(())
}

/// § 5.9.3: if the first array item, rendered on its own line, would be
/// detected by § 5.0.1 as a root opener (lone `{` or lone `[`), the
/// writer wraps the root in `[` / `]`. Non-empty Object/Array items
/// render as `{`/`[` on their own line, which triggers this.
fn first_item_needs_wrap(item: &Value) -> bool {
    matches!(item, Value::Object(o) if !o.is_empty())
        || matches!(item, Value::Array(a) if !a.is_empty())
}

// ---------------------------------------------------------------------------
// § 5.9.5 Pair emission
// ---------------------------------------------------------------------------

/// Emit a single `key: value` / `key:: value` / compound pair.
fn emit_pair(key: &str, value: &Value, indent: usize, out: &mut String) -> Result<()> {
    push_indent(out, indent);
    // Spec 0.6.0 § 3.7 — re-escape `\`, `.`, `:` in the key.
    crate::render::helpers::push_escaped_key_segment(key, out);
    match value {
        Value::Null => {
            // § 5.9.9
            out.push_str(": null\n");
        }
        Value::Bool(b) => {
            out.push_str(": ");
            out.push_str(if *b { "true" } else { "false" });
            out.push('\n');
        }
        Value::Integer(s) => {
            // § 5.9.8: canonical base-10 decimal. Always a valid integer
            // literal — no raw marker needed.
            out.push_str(": ");
            out.push_str(s);
            out.push('\n');
        }
        Value::Float(s) => {
            // § 5.9.8: canonical float form — scientific for large/small abs.
            out.push_str(": ");
            out.push_str(&canonical_float(s));
            out.push('\n');
        }
        Value::String(s) => {
            emit_string_in_pair(s, indent, out)?;
        }
        Value::Array(items) => {
            if items.is_empty() {
                out.push_str(": []\n");
            } else {
                out.push_str(": [\n");
                for item in items {
                    emit_array_item(item, indent + 1, out)?;
                }
                push_indent(out, indent);
                out.push_str("]\n");
            }
        }
        Value::Object(obj) => {
            if obj.is_empty() {
                out.push_str(": {}\n");
            } else {
                out.push_str(": {\n");
                emit_object_pairs(obj, indent + 1, out)?;
                push_indent(out, indent);
                out.push_str("}\n");
            }
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// § 5.9.6 Array-item emission
// ---------------------------------------------------------------------------

/// Emit one array item at the given indent level.
fn emit_array_item(value: &Value, indent: usize, out: &mut String) -> Result<()> {
    push_indent(out, indent);
    match value {
        Value::Null => {
            out.push_str("null\n");
        }
        Value::Bool(b) => {
            out.push_str(if *b { "true" } else { "false" });
            out.push('\n');
        }
        Value::Integer(s) => {
            out.push_str(s);
            out.push('\n');
        }
        Value::Float(s) => {
            out.push_str(&canonical_float(s));
            out.push('\n');
        }
        Value::String(s) => {
            emit_string_as_item(s, indent, out)?;
        }
        Value::Array(items) => {
            if items.is_empty() {
                out.push_str("[]\n");
            } else {
                out.push_str("[\n");
                for item in items {
                    emit_array_item(item, indent + 1, out)?;
                }
                push_indent(out, indent);
                out.push_str("]\n");
            }
        }
        Value::Object(obj) => {
            if obj.is_empty() {
                out.push_str("{}\n");
            } else {
                out.push_str("{\n");
                emit_object_pairs(obj, indent + 1, out)?;
                push_indent(out, indent);
                out.push_str("}\n");
            }
        }
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// § 5.9.7 String form selection — pair context
// ---------------------------------------------------------------------------

/// Emit a String value inside a pair. Chooses between:
/// - `key:` (empty string, no body)
/// - `key: body` (one-line plain)
/// - `key:: body` (one-line raw — would reclassify)
/// - `key: ((\n...\n))` (verbatim multi-line)
/// - `key: (\n...\n)` (stripped multi-line, fallback)
fn emit_string_in_pair(s: &str, indent: usize, out: &mut String) -> Result<()> {
    if s.is_empty() {
        // § 5.9.7: empty String → `key:` with no body.
        out.push_str(":\n");
        return Ok(());
    }

    if s.contains('\r') {
        // § 5.9.7: CR byte not representable in canonical form.
        return Err(Error::Message(
            "String containing CR (0x0D) is not representable in canonical form (§ 5.9.7)".into(),
        ));
    }

    if s.contains('\n') {
        // Multi-line string.
        return emit_multiline_string(s, indent, true, out);
    }

    // One-line string. Check if it needs the raw marker.
    if needs_raw_marker(s) {
        out.push_str(":: ");
        out.push_str(s);
        out.push('\n');
    } else {
        out.push_str(": ");
        out.push_str(s);
        out.push('\n');
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// § 5.9.7 String form selection — array-item context
// ---------------------------------------------------------------------------

/// Emit a String value as an array item. Chooses between:
/// - `::` (empty string, no body)
/// - bare `body` (one-line plain)
/// - `:: body` (one-line raw — would reclassify)
/// - `((\n...\n))` (verbatim multi-line)
/// - `(\n...\n)` (stripped multi-line, fallback)
fn emit_string_as_item(s: &str, indent: usize, out: &mut String) -> Result<()> {
    if s.is_empty() {
        // § 5.9.7: empty String item → `::` with no body.
        // Wait — looking at fixtures, canonical `empty_stripped.canonical.ktav`
        // shows `note:\n` for an empty string in a pair, and for array items
        // the canonical form is `::`. But actually let's re-check § 5.9.6:
        // "Bare scalar item: <bytes> on its own line" — empty string can't be
        // a bare scalar (it would be a blank line). Use `::`.
        out.push_str("::\n");
        return Ok(());
    }

    if s.contains('\r') {
        return Err(Error::Message(
            "String containing CR (0x0D) is not representable in canonical form (§ 5.9.7)".into(),
        ));
    }

    if s.contains('\n') {
        return emit_multiline_string(s, indent, false, out);
    }

    // One-line string. Check if it needs the raw marker.
    if needs_raw_marker(s) {
        out.push_str(":: ");
        out.push_str(s);
        out.push('\n');
    } else {
        out.push_str(s);
        out.push('\n');
    }
    Ok(())
}

// ---------------------------------------------------------------------------
// § 5.9.7 Multi-line string emission (shared for pair + item)
// ---------------------------------------------------------------------------

/// Emit a multi-line string in canonical form.
///
/// Prefers verbatim `((…))` (§ 5.9.7). Falls back to stripped `(…)` when
/// a content line is exactly `))`. If both `)` and `))` appear as sole
/// content lines, either form is accepted (pathological case).
///
/// `is_pair`: if true, we need `key: ((` prefix; if false, just `((`.
/// For the item case, `indent` is where `((` goes, and body is at indent 0.
fn emit_multiline_string(s: &str, indent: usize, is_pair: bool, out: &mut String) -> Result<()> {
    let segments: Vec<&str> = s.split('\n').collect();
    let has_sole_double_paren = segments.iter().any(|l| l.trim() == "))");
    let has_sole_single_paren = segments.iter().any(|l| l.trim() == ")");

    if has_sole_double_paren && !has_sole_single_paren {
        // Verbatim would hit `))` content line → use stripped form.
        // § 5.9.7: body at indent 0 (no leading whitespace), closer `)`
        // at outer indent.
        emit_multiline_stripped(&segments, indent, is_pair, out);
    } else {
        // Default: verbatim form.
        emit_multiline_verbatim(&segments, indent, is_pair, out);
    }
    Ok(())
}

/// Verbatim multi-line `((…))`. Body lines at indent 0 (§ 5.9.6).
fn emit_multiline_verbatim(segments: &[&str], indent: usize, is_pair: bool, out: &mut String) {
    if is_pair {
        out.push_str(": ((\n");
    } else {
        out.push_str("((\n");
    }
    // Body lines at indent 0 (verbatim preserves bytes exactly).
    for (i, seg) in segments.iter().enumerate() {
        if i > 0 {
            out.push('\n');
        }
        out.push_str(seg);
    }
    out.push('\n');
    push_indent(out, indent);
    out.push_str("))\n");
}

/// Stripped multi-line `(…)` fallback. Body lines at indent 0
/// so the common-indent computation yields 0.
fn emit_multiline_stripped(segments: &[&str], indent: usize, is_pair: bool, out: &mut String) {
    if is_pair {
        out.push_str(": (\n");
    } else {
        out.push_str("(\n");
    }
    for (i, seg) in segments.iter().enumerate() {
        if i > 0 {
            out.push('\n');
        }
        // Body at indent 0; blank lines stay blank.
        if !seg.trim().is_empty() {
            out.push_str(seg.trim_start());
        }
    }
    out.push('\n');
    push_indent(out, indent);
    out.push_str(")\n");
}

// ---------------------------------------------------------------------------
// § 5.9.8 Float canonical form
// ---------------------------------------------------------------------------

/// Convert a stored float scalar (ryu shortest-decimal) to the spec § 5.9.8
/// canonical form:
/// - Use scientific notation when `abs(value) >= 1e7` or
///   `0 < abs(value) < 1e-3`.
/// - Otherwise keep the ryu decimal form unchanged.
/// - Scientific: lowercase `e`, no `+` in exponent, strip trailing `.0`
///   in mantissa (so `1.0e9` → `1e9`).
fn canonical_float(s: &str) -> String {
    // Parse the stored ryu string back to f64.
    let val: f64 = match s.parse() {
        Ok(v) => v,
        Err(_) => return s.to_string(), // shouldn't happen; pass through
    };

    if val == 0.0 {
        // Positive/negative zero in ryu is "0.0" or "-0.0"; keep as-is.
        return s.to_string();
    }

    let abs = val.abs();

    if !(1e-2..1e7).contains(&abs) {
        // Build scientific form.
        // Use Rust's {:e} formatter then normalise.
        let raw = format!("{:e}", val); // e.g. "1e9", "1.5e9", "-2.5e-10"
        normalise_scientific(&raw)
    } else {
        // Decimal region: ryu's output is already correct.
        s.to_string()
    }
}

/// Normalise Rust's `{:e}` scientific output to the spec form:
/// - lowercase `e` (already lowercase from `{:e}`)
/// - no `+` sign in the exponent
/// - strip trailing `.0` in the mantissa  (`1.0e9` → `1e9`)
/// - strip trailing zeros after decimal point in mantissa (`1.50e9` → `1.5e9`)
fn normalise_scientific(raw: &str) -> String {
    // Rust {:e} format: "<mantissa>e<exp>" where exp may be negative.
    // Example: "1e9", "1.5e9", "-2.5e-10", "1.5e-3".
    let e_pos = raw.find('e').unwrap_or(raw.len());
    let mantissa = &raw[..e_pos];
    let exp_part = &raw[e_pos + 1..]; // e.g. "9", "-10", "3"

    // Strip trailing zeros and unnecessary decimal point from mantissa.
    let mantissa = if mantissa.contains('.') {
        let trimmed = mantissa.trim_end_matches('0');
        trimmed.trim_end_matches('.')
    } else {
        mantissa
    };

    // Remove leading '+' from exponent (Rust never emits one, but be safe).
    let exp_str = exp_part.trim_start_matches('+');

    format!("{}e{}", mantissa, exp_str)
}

// ---------------------------------------------------------------------------
// § 5.9.5 / 5.9.6 / 5.9.7 — Would the parser re-classify this body?
// ---------------------------------------------------------------------------

/// Returns `true` if `body` would be classified by § 5.2 as something
/// other than a String (number, keyword, compound opener, or multi-line
/// opener). In that case the canonical writer must use the `::` raw
/// marker so the parser reads it back as a String.
///
/// This is a standalone implementation for the canonical writer. It will
/// be unified with the parser's classify logic in Phase 3.
fn needs_raw_marker(body: &str) -> bool {
    if body.is_empty() {
        return false;
    }

    let bytes = body.as_bytes();

    // Leading/trailing whitespace → must use raw marker (or multi-line).
    // But we already handle the multi-line case (contains '\n') in the
    // caller, so here body is single-line. Edge whitespace means the
    // parser would trim and get a different body → raw marker.
    if bytes[0] == b' '
        || bytes[0] == b'\t'
        || *bytes.last().unwrap() == b' '
        || *bytes.last().unwrap() == b'\t'
    {
        return true;
    }

    // Starts with `{` or `[` → would be classified as inline compound.
    if bytes[0] == b'{' || bytes[0] == b'[' {
        return true;
    }

    // Exact multi-line opener tokens → would open multiline, not String.
    if matches!(body, "(" | "((" | "()" | "(())") {
        return true;
    }

    // Keywords.
    if matches!(body, "null" | "true" | "false") {
        return true;
    }

    // Integer literal (§ 3.6).
    if crate::parser::classify::matches_integer_grammar(body) {
        return true;
    }

    // Float literal (§ 3.6).
    if crate::parser::classify::matches_float_grammar(body) {
        return true;
    }

    // Contains control bytes (0x00–0x1F except TAB 0x09) — but these
    // can't appear in a single-line body in valid UTF-8 config text
    // anyway. Skip this check for now; multi-line handles it.

    false
}

// Number grammar matching now delegated to crate::parser::classify
// (matches_integer_grammar / matches_float_grammar).

// ---------------------------------------------------------------------------
// Indent helper
// ---------------------------------------------------------------------------

const INDENT: &str = "    ";

/// Push `level * 4` spaces into `out`.
fn push_indent(out: &mut String, level: usize) {
    const SPACES: &str = "                                                                "; // 64
    let mut remaining = level * INDENT.len();
    if remaining == 0 {
        return;
    }
    out.reserve(remaining);
    while remaining > 0 {
        let chunk = remaining.min(SPACES.len());
        out.push_str(&SPACES[..chunk]);
        remaining -= chunk;
    }
}

// ---------------------------------------------------------------------------
// Size estimate
// ---------------------------------------------------------------------------

fn estimate_size(value: &Value) -> usize {
    match value {
        Value::Null => 5,
        Value::Bool(_) => 6,
        Value::Integer(s) | Value::Float(s) | Value::String(s) => s.len() + 8,
        Value::Array(items) => 4 + items.iter().map(estimate_size).sum::<usize>(),
        Value::Object(obj) => obj
            .iter()
            .map(|(k, v)| k.len() + 4 + estimate_size(v))
            .sum::<usize>()
            .saturating_add(4),
    }
}

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::value::ObjectMap;
    use compact_str::CompactString;
    use indexmap::IndexMap;
    use rustc_hash::FxBuildHasher;

    fn obj(pairs: Vec<(&str, Value)>) -> Value {
        let mut map: ObjectMap = IndexMap::with_capacity_and_hasher(pairs.len(), FxBuildHasher);
        for (k, v) in pairs {
            map.insert(CompactString::new(k), v);
        }
        Value::Object(map)
    }

    fn arr(items: Vec<Value>) -> Value {
        Value::Array(items)
    }

    fn int(n: i64) -> Value {
        let mut buf = itoa::Buffer::new();
        Value::Integer(CompactString::new(buf.format(n)))
    }

    fn float(f: f64) -> Value {
        let mut buf = ryu::Buffer::new();
        Value::Float(CompactString::new(buf.format(f)))
    }

    fn s(text: &str) -> Value {
        Value::String(CompactString::new(text))
    }

    #[test]
    fn empty_object_root_produces_zero_bytes() {
        let v = obj(vec![]);
        assert_eq!(emit_canonical(&v).unwrap(), "");
    }

    #[test]
    fn empty_array_root_produces_brackets() {
        let v = arr(vec![]);
        assert_eq!(emit_canonical(&v).unwrap(), "[]\n");
    }

    #[test]
    fn simple_pairs() {
        let v = obj(vec![
            ("host", s("localhost")),
            ("port", int(8080)),
            ("debug", Value::Bool(true)),
        ]);
        let out = emit_canonical(&v).unwrap();
        assert_eq!(out, "host: localhost\nport: 8080\ndebug: true\n");
    }

    #[test]
    fn null_and_false_keywords() {
        let v = obj(vec![
            ("maintenance", Value::Null),
            ("enabled", Value::Bool(false)),
        ]);
        let out = emit_canonical(&v).unwrap();
        assert_eq!(out, "maintenance: null\nenabled: false\n");
    }

    #[test]
    fn float_values() {
        let v = obj(vec![("ratio", float(0.5)), ("sci", float(1.5e-3))]);
        let out = emit_canonical(&v).unwrap();
        // ryu shortest: 0.5 → "0.5", 1.5e-3 → "0.0015"
        assert!(out.contains("ratio: 0.5\n"), "got: {out}");
        // ryu may produce "0.0015" or "1.5e-3" — accept both canonical forms
        assert!(
            out.contains("sci: 1.5e-3\n") || out.contains("sci: 0.0015\n"),
            "got: {out}"
        );
    }

    #[test]
    fn empty_string_pair() {
        let v = obj(vec![("note", s(""))]);
        let out = emit_canonical(&v).unwrap();
        assert_eq!(out, "note:\n");
    }

    #[test]
    fn raw_marker_for_keywords() {
        let v = obj(vec![("a", s("true")), ("b", s("null")), ("c", s("false"))]);
        let out = emit_canonical(&v).unwrap();
        assert_eq!(out, "a:: true\nb:: null\nc:: false\n");
    }

    #[test]
    fn raw_marker_for_numbers() {
        let v = obj(vec![("a", s("42")), ("b", s("0.5")), ("c", s("0xFF"))]);
        let out = emit_canonical(&v).unwrap();
        assert!(out.contains("a:: 42\n"));
        assert!(out.contains("b:: 0.5\n"));
        assert!(out.contains("c:: 0xFF\n"));
    }

    #[test]
    fn raw_marker_for_inline_opener() {
        let v = obj(vec![("a", s("{hello}"))]);
        let out = emit_canonical(&v).unwrap();
        assert!(out.contains("a:: {hello}\n"));
    }

    #[test]
    fn nested_object() {
        let v = obj(vec![(
            "server",
            obj(vec![("host", s("localhost")), ("port", int(8080))]),
        )]);
        let out = emit_canonical(&v).unwrap();
        let expected = "server: {\n    host: localhost\n    port: 8080\n}\n";
        assert_eq!(out, expected);
    }

    #[test]
    fn nested_array() {
        let v = obj(vec![("tags", arr(vec![s("a"), s("b")]))]);
        let out = emit_canonical(&v).unwrap();
        let expected = "tags: [\n    a\n    b\n]\n";
        assert_eq!(out, expected);
    }

    #[test]
    fn array_root_bare_items() {
        let v = arr(vec![s("foo"), s("bar"), s("baz")]);
        let out = emit_canonical(&v).unwrap();
        assert_eq!(out, "foo\nbar\nbaz\n");
    }

    #[test]
    fn array_root_wraps_when_first_item_is_compound() {
        let v = arr(vec![arr(vec![s("a"), s("b")]), arr(vec![s("c"), s("d")])]);
        let out = emit_canonical(&v).unwrap();
        let expected =
            "[\n    [\n        a\n        b\n    ]\n    [\n        c\n        d\n    ]\n]\n";
        assert_eq!(out, expected);
    }

    #[test]
    fn array_root_does_not_wrap_for_scalars() {
        let v = arr(vec![int(1), int(2), int(3)]);
        let out = emit_canonical(&v).unwrap();
        assert_eq!(out, "1\n2\n3\n");
    }

    #[test]
    fn cr_in_string_is_error() {
        let v = obj(vec![("x", s("hello\rworld"))]);
        assert!(emit_canonical(&v).is_err());
    }

    #[test]
    fn verbatim_multiline_string() {
        let v = obj(vec![("msg", s("line one\nline two"))]);
        let out = emit_canonical(&v).unwrap();
        assert_eq!(
            out,
            "msg: ((\n\
             line one\n\
             line two\n\
             ))\n"
        );
    }

    #[test]
    fn verbatim_multiline_in_array_item() {
        let v = arr(vec![s("line one\nline two"), s("end")]);
        let out = emit_canonical(&v).unwrap();
        assert_eq!(
            out,
            "((\n\
             line one\n\
             line two\n\
             ))\n\
             end\n"
        );
    }

    #[test]
    fn empty_string_array_item() {
        let v = arr(vec![s(""), s("ok")]);
        let out = emit_canonical(&v).unwrap();
        assert_eq!(out, "::\nok\n");
    }

    #[test]
    fn raw_marker_for_paren_tokens() {
        let v = obj(vec![
            ("a", s("(")),
            ("b", s("((")),
            ("c", s("()")),
            ("d", s("(())")),
        ]);
        let out = emit_canonical(&v).unwrap();
        assert!(out.contains("a:: (\n"));
        assert!(out.contains("b:: ((\n"));
        assert!(out.contains("c:: ()\n"));
        assert!(out.contains("d:: (())\n"));
    }

    #[test]
    fn mixed_heterogeneous_array() {
        let v = obj(vec![(
            "mixed",
            arr(vec![
                s("plain_string"),
                int(42),
                Value::Bool(true),
                Value::Null,
                s("true"), // keyword collision → raw marker
                obj(vec![("nested_obj", s("inside"))]),
                arr(vec![s("nested_array")]),
            ]),
        )]);
        let out = emit_canonical(&v).unwrap();
        let expected = "\
mixed: [
    plain_string
    42
    true
    null
    :: true
    {
        nested_obj: inside
    }
    [
        nested_array
    ]
]
";
        assert_eq!(out, expected);
    }

    #[test]
    fn integer_canonical_negative() {
        let v = obj(vec![("x", int(-1)), ("y", int(-42))]);
        let out = emit_canonical(&v).unwrap();
        assert!(out.contains("x: -1\n"));
        assert!(out.contains("y: -42\n"));
    }

    #[test]
    fn integer_canonical_zero() {
        // `-0` should normalise to `0` — but since we use itoa, -0i64
        // would be `0` anyway (no negative zero in i64). The canonical
        // form from the parser would store "0".
        let v = obj(vec![("z", int(0))]);
        let out = emit_canonical(&v).unwrap();
        assert!(out.contains("z: 0\n"));
    }

    #[test]
    fn needs_raw_marker_integer_forms() {
        assert!(needs_raw_marker("42"));
        assert!(needs_raw_marker("-1"));
        assert!(needs_raw_marker("+7"));
        assert!(needs_raw_marker("0xFF"));
        assert!(needs_raw_marker("0o755"));
        assert!(needs_raw_marker("0b1111_0000"));
        assert!(needs_raw_marker("1_000_000"));
        assert!(!needs_raw_marker("hello"));
        assert!(!needs_raw_marker("42abc"));
    }

    #[test]
    fn needs_raw_marker_float_forms() {
        assert!(needs_raw_marker("0.5"));
        assert!(needs_raw_marker("1.5e-3"));
        assert!(needs_raw_marker("1e9"));
        assert!(!needs_raw_marker("1."));
        assert!(!needs_raw_marker(".5"));
    }
}