lemma-engine 0.8.13

A language that means business.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
//! Lemma source code formatting.
//!
//! Formats parsed specs into canonical Lemma source text. Uses `AsLemmaSource`
//! and `Expression::Display` for syntax; this module handles layout only.

use crate::parsing::ast::{
    expression_precedence, AsLemmaSource, Constraint, DataValue, Expression, ExpressionKind,
    LemmaData, LemmaRule, LemmaSpec,
};
use crate::{parse, Error, ParseResult, ResourceLimits};

/// Soft line length limit. Longer lines may be wrapped (unless clauses, expressions).
/// Data and other constructs are not broken if they exceed this.
/// 56 has been chosen to fit on an average mobile screen with an 11pt font.
pub const MAX_COLS: usize = 56;

// =============================================================================
// Public entry points
// =============================================================================

/// Format a sequence of parsed specs into canonical Lemma source.
///
/// specs are separated by two blank lines.
/// The result ends with a single newline.
#[must_use]
pub fn format_specs(specs: &[LemmaSpec]) -> String {
    let mut out = String::new();
    for (index, spec) in specs.iter().enumerate() {
        if index > 0 {
            out.push_str("\n\n");
        }
        out.push_str(&format_spec(spec, MAX_COLS));
    }
    if !out.ends_with('\n') {
        out.push('\n');
    }
    out
}

/// Format a [`ParseResult`] (repository groups + specs) into canonical Lemma source.
#[must_use]
pub fn format_parse_result(result: &ParseResult) -> String {
    let mut blocks: Vec<String> = Vec::new();
    for (repo, specs) in &result.repositories {
        let mut prefix = String::new();
        if let Some(name) = repo.name.as_deref() {
            prefix.push_str("repo ");
            prefix.push_str(name);
            prefix.push_str("\n\n");
        }
        if specs.is_empty() {
            if !prefix.is_empty() {
                blocks.push(prefix);
            }
            continue;
        }
        let body = format_specs(specs.as_slice());
        if prefix.is_empty() {
            blocks.push(body);
        } else {
            prefix.push_str(&body);
            blocks.push(prefix);
        }
    }
    let mut out = blocks.join("\n\n");
    if !out.ends_with('\n') {
        out.push('\n');
    }
    out
}

/// Parse a source string and format it to canonical Lemma source.
///
/// Returns an error if the source does not parse.
pub fn format_source(
    source: &str,
    source_type: crate::parsing::source::SourceType,
) -> Result<String, Error> {
    let limits = ResourceLimits::default();
    let result = parse(source, source_type, &limits)?;
    Ok(format_parse_result(&result))
}

// =============================================================================
// Spec
// =============================================================================

pub(crate) fn format_spec(spec: &LemmaSpec, max_cols: usize) -> String {
    let mut out = String::new();
    out.push_str("spec ");
    out.push_str(&spec.name);
    if let crate::parsing::ast::EffectiveDate::DateTimeValue(ref af) = spec.effective_from {
        out.push(' ');
        out.push_str(&af.to_string());
    }
    out.push('\n');

    if let Some(ref commentary) = spec.commentary {
        out.push_str("\"\"\"\n");
        out.push_str(commentary);
        out.push_str("\n\"\"\"\n");
    }

    for meta in &spec.meta_fields {
        out.push_str(&format!(
            "meta {}: {}\n",
            meta.key,
            AsLemmaSource(&meta.value)
        ));
    }

    if !spec.data.is_empty() {
        format_sorted_data(&spec.data, &mut out, "");
    }

    if !spec.rules.is_empty() {
        out.push('\n');
        for (index, rule) in spec.rules.iter().enumerate() {
            if index > 0 {
                out.push('\n');
            }
            let rule_text = format_rule(rule, max_cols);
            for line in rule_text.lines() {
                out.push_str(line);
                out.push('\n');
            }
        }
    }

    out
}

// =============================================================================
// Data
// =============================================================================

/// Two spaces after `line_prefix` for each `-> ...` constraint line under `data ...: ...`.
const DATA_CONSTRAINT_INDENT: &str = "  ";

fn data_constraints_nonempty(constraints: &Option<Vec<Constraint>>) -> bool {
    constraints.as_ref().is_some_and(|v| !v.is_empty())
}

fn data_value_has_arrow_constraints(value: &DataValue) -> bool {
    match value {
        DataValue::Definition { constraints, .. } | DataValue::Reference { constraints, .. } => {
            data_constraints_nonempty(constraints)
        }
        _ => false,
    }
}

fn data_value_rhs_for_spec_body(value: &DataValue, continuation_prefix: &str) -> String {
    match value {
        DataValue::Definition {
            base,
            constraints,
            from,
            value,
        } if data_constraints_nonempty(constraints) => {
            let cs = constraints
                .as_ref()
                .expect("BUG: constraints checked above");
            let head: String = if base.is_none() && from.is_none() {
                match value {
                    Some(v) => format!("{}", AsLemmaSource(v)),
                    None => String::new(),
                }
            } else {
                match (base.as_ref(), from.as_ref()) {
                    (Some(b), Some(spec)) => format!("{} from {}", b, spec),
                    (Some(b), None) => format!("{}", b),
                    (None, Some(spec)) => format!("<type> from {}", spec),
                    (None, None) => String::new(),
                }
            };
            let mut out = head;
            for (cmd, args) in cs {
                out.push('\n');
                out.push_str(continuation_prefix);
                out.push_str("-> ");
                out.push_str(&crate::parsing::ast::format_constraint_as_source(cmd, args));
            }
            out
        }
        DataValue::Reference {
            target,
            constraints,
        } if data_constraints_nonempty(constraints) => {
            let cs = constraints
                .as_ref()
                .expect("BUG: constraints checked above");
            let mut out = target.to_string();
            for (cmd, args) in cs {
                out.push('\n');
                out.push_str(continuation_prefix);
                out.push_str("-> ");
                out.push_str(&crate::parsing::ast::format_constraint_as_source(cmd, args));
            }
            out
        }
        _ => format!("{}", AsLemmaSource(value)),
    }
}

fn format_data(data: &LemmaData, line_prefix: &str) -> String {
    let ref_str = format!("{}", data.reference);
    let continuation = format!("{line_prefix}{DATA_CONSTRAINT_INDENT}");
    let rhs = data_value_rhs_for_spec_body(&data.value, &continuation);
    if let Some((first, rest)) = rhs.split_once('\n') {
        format!("data {}: {}\n{}", ref_str, first, rest)
    } else {
        format!("data {}: {}", ref_str, rhs)
    }
}

/// Byte length from start of `data ` through the single space after `:` (same layout as [`format_data`]).
fn data_line_prefix_len_before_rhs(ref_str: &str) -> usize {
    // "data " + ref + ": "
    5 + ref_str.len() + 2
}

fn data_is_simple_single_line(data: &LemmaData, line_prefix: &str) -> bool {
    if data_value_has_arrow_constraints(&data.value) {
        return false;
    }
    let continuation = format!("{line_prefix}{DATA_CONSTRAINT_INDENT}");
    let rhs = data_value_rhs_for_spec_body(&data.value, &continuation);
    !rhs.contains('\n')
}

fn push_formatted_simple_data_line_padded(
    out: &mut String,
    data: &LemmaData,
    line_prefix: &str,
    target_prefix_len_before_rhs: usize,
) {
    let ref_str = format!("{}", data.reference);
    let continuation = format!("{line_prefix}{DATA_CONSTRAINT_INDENT}");
    let rhs = data_value_rhs_for_spec_body(&data.value, &continuation);
    let base = data_line_prefix_len_before_rhs(&ref_str);
    let gap = 1 + target_prefix_len_before_rhs.saturating_sub(base);
    out.push_str(line_prefix);
    out.push_str("data ");
    out.push_str(&ref_str);
    out.push(':');
    out.push_str(&" ".repeat(gap));
    out.push_str(&rhs);
}

fn emit_data_row_group(rows: &[&LemmaData], line_prefix: &str, out: &mut String) {
    let mut i = 0;
    while i < rows.len() {
        if data_is_simple_single_line(rows[i], line_prefix) {
            let run_start = i;
            i += 1;
            while i < rows.len() && data_is_simple_single_line(rows[i], line_prefix) {
                i += 1;
            }
            let run_end = i;
            let target = (run_start..run_end)
                .map(|k| data_line_prefix_len_before_rhs(&format!("{}", rows[k].reference)))
                .max()
                .expect("BUG: non-empty run");
            for row in rows[run_start..run_end].iter().copied() {
                push_formatted_simple_data_line_padded(out, row, line_prefix, target);
                out.push('\n');
            }
        } else {
            let row = rows[i];
            out.push_str(line_prefix);
            out.push_str(&format_data(row, line_prefix));
            out.push('\n');
            if data_value_has_arrow_constraints(&row.value) && i + 1 < rows.len() {
                out.push('\n');
            }
            i += 1;
        }
    }
}

fn format_import_row(data: &LemmaData) -> String {
    let alias = &data.reference.name;
    if let DataValue::Import(spec_ref) = &data.value {
        let spec_name = &spec_ref.name;
        let last_segment = spec_name.rsplit('/').next().unwrap_or(spec_name);
        if alias == last_segment {
            format!("uses {}", spec_ref)
        } else {
            format!("uses {}: {}", alias, spec_ref)
        }
    } else {
        unreachable!("BUG: format_import_row called on non-Import data")
    }
}

/// Group data into sections separated by blank lines:
///
/// 1. Imports (`uses`), each followed by their literal bindings — original order within this block
/// 2. Regular data (literals, type declarations, references) — original order
/// 3. Qualified overrides that did not attach to any import — original order
fn format_sorted_data(data: &[LemmaData], out: &mut String, line_prefix: &str) {
    let mut regular: Vec<&LemmaData> = Vec::new();
    let mut imports: Vec<&LemmaData> = Vec::new();
    let mut overrides: Vec<&LemmaData> = Vec::new();

    for data in data {
        if !data.reference.is_local() {
            overrides.push(data);
        } else if matches!(&data.value, DataValue::Import(_)) {
            imports.push(data);
        } else {
            regular.push(data);
        }
    }

    let emit_group =
        |rows: &[&LemmaData], out: &mut String| emit_data_row_group(rows, line_prefix, out);

    if !imports.is_empty() {
        out.push('\n');

        let has_overrides = |row: &LemmaData| -> bool {
            let ref_name = &row.reference.name;
            overrides.iter().any(|o| {
                o.reference.segments.first().map(|s| s.as_str()) == Some(ref_name.as_str())
            })
        };

        let is_bare = |row: &LemmaData| -> bool {
            if let DataValue::Import(sr) = &row.value {
                let last = sr.name.rsplit('/').next().unwrap_or(&sr.name);
                row.reference.name == last && sr.effective.is_none() && !has_overrides(row)
            } else {
                false
            }
        };

        let mut i = 0;
        while i < imports.len() {
            if i > 0 {
                out.push('\n');
            }
            if is_bare(imports[i]) {
                let mut group_names = Vec::new();
                while i < imports.len() && is_bare(imports[i]) {
                    if let DataValue::Import(sr) = &imports[i].value {
                        group_names.push(sr.to_string());
                    }
                    i += 1;
                }
                if group_names.len() == 1 {
                    out.push_str(line_prefix);
                    out.push_str(&format!("uses {}", group_names[0]));
                } else {
                    out.push_str(line_prefix);
                    out.push_str(&format!("uses {}", group_names.join(", ")));
                }
                out.push('\n');
            } else {
                let row = imports[i];
                out.push_str(line_prefix);
                out.push_str(&format_import_row(row));
                out.push('\n');
                let ref_name = &row.reference.name;
                let binding_overrides: Vec<&LemmaData> = overrides
                    .iter()
                    .filter(|o| {
                        o.reference.segments.first().map(|s| s.as_str()) == Some(ref_name.as_str())
                    })
                    .copied()
                    .collect();
                if !binding_overrides.is_empty() {
                    emit_data_row_group(&binding_overrides, line_prefix, out);
                }
                i += 1;
            }
        }
    }

    if !regular.is_empty() {
        out.push('\n');
        emit_group(&regular, out);
    }

    let matched_prefixes: Vec<&str> = imports.iter().map(|f| f.reference.name.as_str()).collect();
    let unmatched: Vec<&LemmaData> = overrides
        .iter()
        .filter(|o| {
            o.reference
                .segments
                .first()
                .map(|s| !matched_prefixes.contains(&s.as_str()))
                .unwrap_or(true)
        })
        .copied()
        .collect();
    if !unmatched.is_empty() {
        out.push('\n');
        emit_group(&unmatched, out);
    }
}

// =============================================================================
// Rules
// =============================================================================

const UNLESS_LINE_PREFIX: &str = "  unless ";

/// Logical line length for `max_cols` checks (no extra spec-level indent).
#[inline]
fn spec_line_len(line: &str) -> usize {
    line.len()
}

/// Default expression stays on the `rule name:` line when it fits under `max_cols`.
///
/// Single-line `unless … then …` clauses align `then` when every such line still fits under
/// `max_cols` after alignment. Any clause that splits across lines (expression wraps, or one line
/// would exceed `max_cols`) uses a fixed `then` indent — no column alignment with shorter sisters.
fn format_rule(rule: &LemmaRule, max_cols: usize) -> String {
    let expr_indent = "  ";
    let body = format_expr_wrapped(&rule.expression, max_cols, expr_indent, 10);
    let mut out = String::new();
    out.push_str("rule ");
    out.push_str(&rule.name);
    let body_single_line = !body.contains('\n');
    let header_fits_on_one_line =
        body_single_line && spec_line_len(&format!("rule {}: {}", rule.name, body)) <= max_cols;
    if header_fits_on_one_line {
        out.push_str(": ");
        out.push_str(&body);
    } else {
        out.push_str(":\n");
        out.push_str(expr_indent);
        out.push_str(&body);
    }

    let pl = UNLESS_LINE_PREFIX.len();
    let naive_single_len = |cond: &str, res: &str| pl + cond.len() + 6 + res.len();
    let aligned_single_len = |res: &str, max_end: usize| max_end + 6 + res.len();

    let mut clauses: Vec<(String, String, bool)> = Vec::new();
    for unless_clause in &rule.unless_clauses {
        let condition = format_expr_wrapped(&unless_clause.condition, max_cols, "    ", 10);
        let result = format_expr_wrapped(&unless_clause.result, max_cols, "    ", 10);
        let multiline = condition.contains('\n') || result.contains('\n');
        clauses.push((condition, result, multiline));
    }

    let mut singles: Vec<usize> = clauses
        .iter()
        .enumerate()
        .filter(|(_, (c, r, m))| !*m && naive_single_len(c, r) <= max_cols)
        .map(|(i, _)| i)
        .collect();

    loop {
        if singles.is_empty() {
            break;
        }
        let max_end = singles
            .iter()
            .map(|&i| pl + clauses[i].0.len())
            .max()
            .expect("BUG: singles non-empty");
        let before = singles.len();
        singles.retain(|&i| aligned_single_len(&clauses[i].1, max_end) <= max_cols);
        if singles.len() == before {
            break;
        }
    }

    let align_max_end = singles.iter().map(|&i| pl + clauses[i].0.len()).max();
    const SPLIT_THEN_INDENT_SPACES: usize = 4;

    for (i, (condition, result, multiline)) in clauses.iter().enumerate() {
        if *multiline {
            out.push_str("\n  unless ");
            out.push_str(condition);
            out.push('\n');
            out.push_str(&" ".repeat(SPLIT_THEN_INDENT_SPACES));
            out.push_str("then ");
            out.push_str(result);
            continue;
        }
        if singles.contains(&i) {
            let max_end = align_max_end.expect("BUG: singles.contains but align_max_end empty");
            let gap = 1 + max_end.saturating_sub(pl + condition.len());
            out.push('\n');
            out.push_str(UNLESS_LINE_PREFIX);
            out.push_str(condition);
            out.push_str(&" ".repeat(gap));
            out.push_str("then ");
            out.push_str(result);
            continue;
        }
        out.push_str("\n  unless ");
        out.push_str(condition);
        out.push('\n');
        out.push_str(&" ".repeat(SPLIT_THEN_INDENT_SPACES));
        out.push_str("then ");
        out.push_str(result);
    }
    out.push('\n');
    out
}

// =============================================================================
// Expression wrapping (soft line breaking at max_cols)
// =============================================================================

/// Indent every line after the first by `indent`.
fn indent_after_first_line(s: &str, indent: &str) -> String {
    let mut first = true;
    let mut out = String::new();
    for line in s.lines() {
        if first {
            first = false;
            out.push_str(line);
        } else {
            out.push('\n');
            out.push_str(indent);
            out.push_str(line);
        }
    }
    if s.ends_with('\n') {
        out.push('\n');
    }
    out
}

/// Format an expression with optional wrapping at arithmetic operators when over max_cols.
/// `parent_prec` is used to add parentheses when needed (pass 10 for top level).
fn format_expr_wrapped(
    expr: &Expression,
    max_cols: usize,
    indent: &str,
    parent_prec: u8,
) -> String {
    let my_prec = expression_precedence(&expr.kind);

    let wrap_in_parens = |s: String| {
        if parent_prec < 10 && my_prec < parent_prec {
            format!("({})", s)
        } else {
            s
        }
    };

    match &expr.kind {
        ExpressionKind::Arithmetic(left, op, right) => {
            let left_str = format_expr_wrapped(left.as_ref(), max_cols, indent, my_prec);
            let right_str = format_expr_wrapped(right.as_ref(), max_cols, indent, my_prec);
            let single_line = format!("{} {} {}", left_str, op, right_str);
            if single_line.len() <= max_cols && !single_line.contains('\n') {
                return wrap_in_parens(single_line);
            }
            let continued_right = indent_after_first_line(&right_str, indent);
            let continuation = format!("{}{} {}", indent, op, continued_right);
            let multi_line = format!("{}\n{}", left_str, continuation);
            wrap_in_parens(multi_line)
        }
        _ => {
            let s = expr.to_string();
            wrap_in_parens(s)
        }
    }
}

// =============================================================================
// Tests
// =============================================================================

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parsing::ast::{
        AsLemmaSource, BooleanValue, DateTimeValue, DurationUnit, TimeValue, TimezoneValue, Value,
    };
    use rust_decimal::prelude::FromStr;
    use rust_decimal::Decimal;

    /// Helper: format a Value as canonical Lemma source via AsLemmaSource.
    fn fmt_value(v: &Value) -> String {
        format!("{}", AsLemmaSource(v))
    }

    #[test]
    fn test_format_value_text_is_quoted() {
        let v = Value::Text("light".to_string());
        assert_eq!(fmt_value(&v), "\"light\"");
    }

    #[test]
    fn test_format_value_text_escapes_quotes() {
        let v = Value::Text("say \"hello\"".to_string());
        assert_eq!(fmt_value(&v), "\"say \\\"hello\\\"\"");
    }

    #[test]
    fn test_format_value_number() {
        let v = Value::Number(Decimal::from_str("42.50").unwrap());
        assert_eq!(fmt_value(&v), "42.50");
    }

    #[test]
    fn test_format_value_number_integer() {
        let v = Value::Number(Decimal::from_str("100.00").unwrap());
        assert_eq!(fmt_value(&v), "100");
    }

    #[test]
    fn test_format_value_boolean() {
        assert_eq!(fmt_value(&Value::Boolean(BooleanValue::True)), "true");
        assert_eq!(fmt_value(&Value::Boolean(BooleanValue::Yes)), "yes");
        assert_eq!(fmt_value(&Value::Boolean(BooleanValue::No)), "no");
        assert_eq!(fmt_value(&Value::Boolean(BooleanValue::Accept)), "accept");
        assert_eq!(fmt_value(&Value::Boolean(BooleanValue::Reject)), "reject");
    }

    #[test]
    fn test_format_value_scale() {
        let v = Value::Scale(Decimal::from_str("99.50").unwrap(), "eur".to_string());
        assert_eq!(fmt_value(&v), "99.50 eur");
    }

    #[test]
    fn test_format_value_duration() {
        let v = Value::Duration(Decimal::from(40), DurationUnit::Hour);
        assert_eq!(fmt_value(&v), "40 hours");
    }

    #[test]
    fn test_format_value_ratio_percent() {
        let v = Value::Ratio(
            Decimal::from_str("0.10").unwrap(),
            Some("percent".to_string()),
        );
        assert_eq!(fmt_value(&v), "10%");
    }

    #[test]
    fn test_format_value_ratio_permille() {
        let v = Value::Ratio(
            Decimal::from_str("0.005").unwrap(),
            Some("permille".to_string()),
        );
        assert_eq!(fmt_value(&v), "5%%");
    }

    #[test]
    fn test_format_value_ratio_bare() {
        let v = Value::Ratio(Decimal::from_str("0.25").unwrap(), None);
        assert_eq!(fmt_value(&v), "0.25");
    }

    #[test]
    fn test_format_value_date_only() {
        let v = Value::Date(DateTimeValue {
            year: 2024,
            month: 1,
            day: 15,
            hour: 0,
            minute: 0,
            second: 0,
            microsecond: 0,
            timezone: None,
        });
        assert_eq!(fmt_value(&v), "2024-01-15");
    }

    #[test]
    fn test_format_value_datetime_with_tz() {
        let v = Value::Date(DateTimeValue {
            year: 2024,
            month: 1,
            day: 15,
            hour: 14,
            minute: 30,
            second: 0,
            microsecond: 0,
            timezone: Some(TimezoneValue {
                offset_hours: 0,
                offset_minutes: 0,
            }),
        });
        assert_eq!(fmt_value(&v), "2024-01-15T14:30:00Z");
    }

    #[test]
    fn test_format_value_time() {
        let v = Value::Time(TimeValue {
            hour: 14,
            minute: 30,
            second: 45,
            timezone: None,
        });
        assert_eq!(fmt_value(&v), "14:30:45");
    }

    #[test]
    fn test_format_source_round_trips_text() {
        let source = r#"spec test

data name: "Alice"

rule greeting: "hello"
"#;
        let formatted =
            format_source(source, crate::parsing::source::SourceType::Volatile).unwrap();
        assert!(formatted.contains("\"Alice\""), "data text must be quoted");
        assert!(formatted.contains("\"hello\""), "rule text must be quoted");
    }

    #[test]
    fn test_format_source_preserves_percent() {
        let source = r#"spec test

data rate: 10 percent

rule tax: rate * 21%
"#;
        let formatted =
            format_source(source, crate::parsing::source::SourceType::Volatile).unwrap();
        assert!(
            formatted.contains("10%"),
            "data percent must use shorthand %, got: {}",
            formatted
        );
    }

    #[test]
    fn test_format_groups_data_preserving_order() {
        // Data are deliberately mixed: the formatter keeps all regular data together
        // in original order, aligned
        let source = r#"spec test

data income: number -> minimum 0
data filing_status: filing_status_type -> default "single"
data country: "NL"
data deductions: number -> minimum 0
data name: text

rule total: income
"#;
        let formatted =
            format_source(source, crate::parsing::source::SourceType::Volatile).unwrap();
        let data_section = formatted
            .split("rule total")
            .next()
            .unwrap()
            .split("spec test\n")
            .nth(1)
            .unwrap();
        let lines: Vec<&str> = data_section.lines().filter(|l| !l.is_empty()).collect();
        // Constrained rows: one blank line after each when more `data` follows.
        assert_eq!(lines[0], "data income: number");
        assert_eq!(lines[1], "  -> minimum 0");
        assert_eq!(lines[2], "data filing_status: filing_status_type");
        assert_eq!(lines[3], "  -> default \"single\"");
        assert_eq!(lines[4], "data country: \"NL\"");
        assert_eq!(lines[5], "data deductions: number");
        assert_eq!(lines[6], "  -> minimum 0");
        assert_eq!(lines[7], "data name: text");
    }

    #[test]
    fn test_format_groups_spec_refs_with_overrides() {
        let source = r#"spec test

data retail.quantity: 5
uses order wholesale
uses order retail
data wholesale.quantity: 100
data base_price: 50

rule total: base_price
"#;
        let formatted =
            format_source(source, crate::parsing::source::SourceType::Volatile).unwrap();
        let data_section = formatted
            .split("rule total")
            .next()
            .unwrap()
            .split("spec test\n")
            .nth(1)
            .unwrap();
        let lines: Vec<&str> = data_section.lines().filter(|l| !l.is_empty()).collect();
        assert_eq!(lines[0], "uses order wholesale");
        assert_eq!(lines[1], "data wholesale.quantity: 100");
        assert_eq!(lines[2], "uses order retail");
        assert_eq!(lines[3], "data retail.quantity: 5");
        assert_eq!(lines[4], "data base_price: 50");
    }

    #[test]
    fn test_format_source_weather_clothing_text_quoted() {
        let source = r#"spec weather_clothing

data clothing_style: text
  -> option "light"
  -> option "warm"

data temperature: number

rule clothing_layer: "light"
  unless temperature < 5 then "warm"
"#;
        let formatted =
            format_source(source, crate::parsing::source::SourceType::Volatile).unwrap();
        assert!(
            formatted.contains("\"light\""),
            "text in rule must be quoted, got: {}",
            formatted
        );
        assert!(
            formatted.contains("\"warm\""),
            "text in unless must be quoted, got: {}",
            formatted
        );
    }

    // NOTE: Default value type validation (e.g. rejecting "10 $$" as a number
    // default) is tested at the planning level in engine.rs, not here. The
    // formatter only parses — it does not validate types. Planning catches
    // invalid defaults for both primitives and named types.

    #[test]
    fn test_format_text_option_round_trips() {
        let source = r#"spec test

data status: text
  -> option "active"
  -> option "inactive"

data s: status

rule out: s
"#;
        let formatted =
            format_source(source, crate::parsing::source::SourceType::Volatile).unwrap();
        assert!(
            formatted.contains("option \"active\""),
            "text option must be quoted, got: {}",
            formatted
        );
        assert!(
            formatted.contains("option \"inactive\""),
            "text option must be quoted, got: {}",
            formatted
        );
        // Round-trip
        let reparsed = format_source(&formatted, crate::parsing::source::SourceType::Volatile);
        assert!(reparsed.is_ok(), "formatted output should re-parse");
    }

    #[test]
    fn test_format_help_round_trips() {
        let source = r#"spec test
data quantity: number -> help "Number of items to order"
rule total: quantity
"#;
        let formatted =
            format_source(source, crate::parsing::source::SourceType::Volatile).unwrap();
        assert!(
            formatted.contains("help \"Number of items to order\""),
            "help must be quoted, got: {}",
            formatted
        );
        // Round-trip
        let reparsed = format_source(&formatted, crate::parsing::source::SourceType::Volatile);
        assert!(reparsed.is_ok(), "formatted output should re-parse");
    }

    #[test]
    fn test_format_scale_type_def_round_trips() {
        let source = r#"spec test

data money: scale
  -> unit eur 1.00
  -> unit usd 1.10
  -> decimals 2
  -> minimum 0

data price: money

rule total: price
"#;
        let formatted =
            format_source(source, crate::parsing::source::SourceType::Volatile).unwrap();
        assert!(
            formatted.contains("unit eur 1.00"),
            "scale unit should not be quoted, got: {}",
            formatted
        );
        // Round-trip
        let reparsed = format_source(&formatted, crate::parsing::source::SourceType::Volatile);
        assert!(
            reparsed.is_ok(),
            "formatted output should re-parse, got: {:?}",
            reparsed
        );
    }

    #[test]
    fn test_format_expression_display_stable_round_trip() {
        let source = r#"spec test
data a: 1.00
rule r: a + 2.00 * 3
"#;
        let formatted =
            format_source(source, crate::parsing::source::SourceType::Volatile).unwrap();
        let again =
            format_source(&formatted, crate::parsing::source::SourceType::Volatile).unwrap();
        assert_eq!(
            formatted, again,
            "AST Display-based format must be idempotent under parse/format"
        );
    }

    #[test]
    fn test_format_rule_default_on_same_line_when_fits() {
        let source = "spec test\nrule r: 1\n";
        let formatted =
            format_source(source, crate::parsing::source::SourceType::Volatile).unwrap();
        assert!(
            formatted.contains("rule r: 1\n"),
            "default expr should stay on rule line when under MAX_COLS, got:\n{formatted}"
        );
    }

    #[test]
    fn test_format_rule_unless_single_line_when_short() {
        let source = r#"spec test
data a: number
data b: boolean

rule r: no
  unless a < 1 then yes
  unless b then yes
"#;
        let formatted =
            format_source(source, crate::parsing::source::SourceType::Volatile).unwrap();
        assert!(
            formatted.contains("unless a < 1 then yes")
                && formatted.contains("unless b     then yes"),
            "unless stays on one line when under MAX_COLS, got:\n{formatted}"
        );
    }
}