domainforge-core 0.13.0

Rust core library implementing the SEA DSL primitives and validation engine.
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
//! Core formatter implementation.

use crate::formatter::config::FormatConfig;
use crate::parser::ast::{
    parse_source, Ast, AstNode, FileMetadata, ImportDecl, ImportItem, ImportSpecifier,
};
use crate::policy::Expression;
use std::fmt;

/// Error type for formatting operations.
#[derive(Debug)]
pub enum FormatError {
    /// Failed to parse the source code.
    ParseError(String),
    /// Internal formatting error.
    InternalError(String),
}

impl fmt::Display for FormatError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            FormatError::ParseError(msg) => write!(f, "Parse error: {}", msg),
            FormatError::InternalError(msg) => write!(f, "Internal error: {}", msg),
        }
    }
}

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

/// Format SEA source code.
///
/// Parses the source, then pretty-prints it with consistent formatting.
///
/// # Arguments
///
/// * `source` - The SEA source code to format
/// * `config` - Formatting configuration options
///
/// # Returns
///
/// The formatted source code, or an error if parsing fails.
///
/// # Example
///
/// ```rust,ignore
/// use domainforge_core::formatter::{format, FormatConfig};
///
/// let source = r#"Entity   "Foo"  in    bar"#;
/// let formatted = format(source, FormatConfig::default()).unwrap();
/// assert_eq!(formatted, "Entity \"Foo\" in bar\n");
/// ```
pub fn format(source: &str, config: FormatConfig) -> Result<String, FormatError> {
    // Check if we should preserve comments
    if config.preserve_comments {
        format_preserving_comments(source, config)
    } else {
        format_without_comments(source, config)
    }
}

/// Format SEA source code without preserving comments.
fn format_without_comments(source: &str, config: FormatConfig) -> Result<String, FormatError> {
    let ast = parse_source(source).map_err(|e| FormatError::ParseError(e.to_string()))?;

    let mut formatter = Formatter::new(config, None);
    formatter.format_ast(&ast);

    Ok(formatter.output)
}

/// Format SEA source code while preserving comments.
///
/// This function:
/// 1. Extracts all comments from the source with their positions
/// 2. Parses and formats the code
/// 3. Re-inserts comments at appropriate positions
pub fn format_preserving_comments(
    source: &str,
    config: FormatConfig,
) -> Result<String, FormatError> {
    use crate::formatter::comments::CommentedSource;

    let commented = CommentedSource::new(source);
    let ast = parse_source(source).map_err(|e| FormatError::ParseError(e.to_string()))?;

    let mut formatter = Formatter::new(config, Some(commented));
    formatter.format_ast(&ast);

    Ok(formatter.output)
}

/// Internal formatter state.
struct Formatter {
    config: FormatConfig,
    output: String,
    indent_level: usize,
    /// Optional source with comments for preservation
    commented_source: Option<crate::formatter::comments::CommentedSource>,
}

impl Formatter {
    fn new(
        config: FormatConfig,
        commented_source: Option<crate::formatter::comments::CommentedSource>,
    ) -> Self {
        Self {
            config,
            output: String::new(),
            indent_level: 0,
            commented_source,
        }
    }

    /// Format the entire AST.
    fn format_ast(&mut self, ast: &Ast) {
        // Output file header comments if we have them
        // Clone to avoid borrow conflict with self.write()
        let header_comments: Vec<_> = self
            .commented_source
            .as_ref()
            .map(|cs| cs.file_header_comments.clone())
            .unwrap_or_default();

        for comment in &header_comments {
            self.write("// ");
            self.write(&comment.text);
            self.newline();
        }
        if !header_comments.is_empty() {
            self.newline();
        }

        // Format file header
        self.format_file_metadata(&ast.metadata);

        // Format declarations
        for (i, decl) in ast.declarations.iter().enumerate() {
            if i > 0 || !ast.metadata.imports.is_empty() || ast.metadata.namespace.is_some() {
                self.newline();
            }
            self.format_declaration(&decl.node);
        }

        // Ensure trailing newline
        if self.config.trailing_newline && !self.output.ends_with('\n') {
            self.output.push('\n');
        }
    }

    /// Format file-level metadata (annotations and imports).
    fn format_file_metadata(&mut self, meta: &FileMetadata) {
        // Annotations in canonical order
        if let Some(ref ns) = meta.namespace {
            self.write("@namespace ");
            self.write_string_literal(ns);
            self.newline();
        }
        if let Some(ref version) = meta.version {
            self.write("@version ");
            self.write_string_literal(version);
            self.newline();
        }
        if let Some(ref owner) = meta.owner {
            self.write("@owner ");
            self.write_string_literal(owner);
            self.newline();
        }
        if let Some(ref profile) = meta.profile {
            self.write("@profile ");
            self.write_string_literal(profile);
            self.newline();
        }

        // Blank line after annotations if any and before imports
        if (meta.namespace.is_some()
            || meta.version.is_some()
            || meta.owner.is_some()
            || meta.profile.is_some())
            && !meta.imports.is_empty()
        {
            self.newline();
        }

        // Imports (sorted if configured)
        let mut imports = meta.imports.clone();
        if self.config.sort_imports {
            imports.sort_by(|a, b| a.from_module.cmp(&b.from_module));
        }

        for import in &imports {
            self.format_import(import);
            self.newline();
        }
    }

    /// Format an import declaration.
    fn format_import(&mut self, import: &ImportDecl) {
        self.write("import ");
        match &import.specifier {
            ImportSpecifier::Named(items) => {
                self.write("{ ");
                for (i, item) in items.iter().enumerate() {
                    if i > 0 {
                        self.write(", ");
                    }
                    self.format_import_item(item);
                }
                self.write(" }");
            }
            ImportSpecifier::Wildcard(alias) => {
                self.write("* as ");
                self.write(alias);
            }
        }
        self.write(" from ");
        self.write_string_literal(&import.from_module);
    }

    /// Format an import item.
    fn format_import_item(&mut self, item: &ImportItem) {
        self.write(&item.name);
        if let Some(ref alias) = item.alias {
            self.write(" as ");
            self.write(alias);
        }
    }

    /// Format a declaration node.
    fn format_declaration(&mut self, node: &AstNode) {
        match node {
            AstNode::Export(inner) => {
                self.write("export ");
                self.format_declaration(&inner.node);
            }
            AstNode::Entity {
                name,
                version,
                annotations,
                domain,
            } => {
                self.write("Entity ");
                self.write_string_literal(name);
                if let Some(v) = version {
                    self.write(" v");
                    self.write(v);
                }
                // Format annotations
                if let Some(replaces) = annotations.get("replaces") {
                    if let Some(s) = replaces.as_str() {
                        self.newline();
                        self.indent();
                        self.write_indent();
                        self.write("@replaces ");
                        // Parse the replaces value which may include version
                        if s.contains(" v") {
                            let parts: Vec<&str> = s.splitn(2, " v").collect();
                            self.write_string_literal(parts[0]);
                            if parts.len() > 1 {
                                self.write(" v");
                                self.write(parts[1]);
                            }
                        } else {
                            self.write_string_literal(s);
                        }
                        self.dedent();
                    }
                }
                if let Some(changes) = annotations.get("changes") {
                    if let Some(arr) = changes.as_array() {
                        self.newline();
                        self.indent();
                        self.write_indent();
                        self.write("@changes [");
                        for (i, change) in arr.iter().enumerate() {
                            if i > 0 {
                                self.write(", ");
                            }
                            if let Some(s) = change.as_str() {
                                self.write_string_literal(s);
                            }
                        }
                        self.write("]");
                        self.dedent();
                    }
                }
                if let Some(d) = domain {
                    self.write(" in ");
                    self.write(d);
                }
                self.newline();
            }
            AstNode::Resource {
                name,
                annotations,
                unit_name,
                domain,
            } => {
                self.write("Resource ");
                self.write_string_literal(name);
                // Format annotations
                if let Some(replaces) = annotations.get("replaces") {
                    if let Some(s) = replaces.as_str() {
                        self.newline();
                        self.indent();
                        self.write_indent();
                        self.write("@replaces ");
                        // Parse the replaces value which may include version
                        if s.contains(" v") {
                            let parts: Vec<&str> = s.splitn(2, " v").collect();
                            self.write_string_literal(parts[0]);
                            if parts.len() > 1 {
                                self.write(" v");
                                self.write(parts[1]);
                            }
                        } else {
                            self.write_string_literal(s);
                        }
                        self.dedent();
                    }
                }
                if let Some(changes) = annotations.get("changes") {
                    if let Some(arr) = changes.as_array() {
                        self.newline();
                        self.indent();
                        self.write_indent();
                        self.write("@changes [");
                        for (i, change) in arr.iter().enumerate() {
                            if i > 0 {
                                self.write(", ");
                            }
                            if let Some(s) = change.as_str() {
                                self.write_string_literal(s);
                            }
                        }
                        self.write("]");
                        self.dedent();
                    }
                }
                if let Some(u) = unit_name {
                    self.write(" ");
                    self.write(u);
                }
                if let Some(d) = domain {
                    self.write(" in ");
                    self.write(d);
                }
                self.newline();
            }
            AstNode::Flow {
                resource_name,
                annotations,
                from_entity,
                to_entity,
                quantity,
            } => {
                self.write("Flow ");
                self.write_string_literal(resource_name);
                // Format annotations
                if let Some(replaces) = annotations.get("replaces") {
                    if let Some(s) = replaces.as_str() {
                        self.newline();
                        self.indent();
                        self.write_indent();
                        self.write("@replaces ");
                        if s.contains(" v") {
                            let parts: Vec<&str> = s.splitn(2, " v").collect();
                            self.write_string_literal(parts[0]);
                            if parts.len() > 1 {
                                self.write(" v");
                                self.write(parts[1]);
                            }
                        } else {
                            self.write_string_literal(s);
                        }
                        self.dedent();
                    }
                }
                if let Some(changes) = annotations.get("changes") {
                    if let Some(arr) = changes.as_array() {
                        self.newline();
                        self.indent();
                        self.write_indent();
                        self.write("@changes [");
                        for (i, change) in arr.iter().enumerate() {
                            if i > 0 {
                                self.write(", ");
                            }
                            if let Some(s) = change.as_str() {
                                self.write_string_literal(s);
                            }
                        }
                        self.write("]");
                        self.dedent();
                    }
                }
                self.write(" from ");
                self.write_string_literal(from_entity);
                self.write(" to ");
                self.write_string_literal(to_entity);
                if let Some(q) = quantity {
                    self.write(" quantity ");
                    self.write(&q.to_string());
                }
                self.newline();
            }
            AstNode::Pattern { name, regex } => {
                self.write("Pattern ");
                self.write_string_literal(name);
                self.write(" matches ");
                self.write_string_literal(regex);
                self.newline();
            }
            AstNode::Role { name, domain } => {
                self.write("Role ");
                self.write_string_literal(name);
                if let Some(d) = domain {
                    self.write(" in ");
                    self.write(d);
                }
                self.newline();
            }
            AstNode::Relation {
                name,
                subject_role,
                predicate,
                object_role,
                via_flow,
            } => {
                self.write("Relation ");
                self.write_string_literal(name);
                self.newline();
                self.indent();
                self.write_indent();
                self.write("subject: ");
                self.write_string_literal(subject_role);
                self.newline();
                self.write_indent();
                self.write("predicate: ");
                self.write_string_literal(predicate);
                self.newline();
                self.write_indent();
                self.write("object: ");
                self.write_string_literal(object_role);
                if let Some(flow) = via_flow {
                    self.newline();
                    self.write_indent();
                    self.write("via: flow ");
                    self.write_string_literal(flow);
                }
                self.dedent();
                self.newline();
            }
            AstNode::Dimension { name } => {
                self.write("Dimension ");
                self.write_string_literal(name);
                self.newline();
            }
            AstNode::UnitDeclaration {
                symbol,
                dimension,
                factor,
                base_unit,
            } => {
                self.write("Unit ");
                self.write_string_literal(symbol);
                self.write(" of ");
                self.write_string_literal(dimension);
                self.write(" factor ");
                self.write(&factor.to_string());
                self.write(" base ");
                self.write_string_literal(base_unit);
                self.newline();
            }
            AstNode::Policy {
                name,
                version,
                metadata,
                expression,
            } => {
                self.write("Policy ");
                self.write(name);
                if let (Some(kind), Some(modality), Some(priority)) =
                    (&metadata.kind, &metadata.modality, metadata.priority)
                {
                    self.write(" per ");
                    self.write(&format!("{}", kind));
                    self.write(" ");
                    self.write(&format!("{}", modality));
                    self.write(" priority ");
                    self.write(&priority.to_string());
                }
                // Policy annotations
                if let Some(ref rationale) = metadata.rationale {
                    self.newline();
                    self.indent();
                    self.write_indent();
                    self.write("@rationale ");
                    self.write_string_literal(rationale);
                    self.dedent();
                }
                if !metadata.tags.is_empty() {
                    self.newline();
                    self.indent();
                    self.write_indent();
                    self.write("@tags [");
                    for (i, tag) in metadata.tags.iter().enumerate() {
                        if i > 0 {
                            self.write(", ");
                        }
                        self.write_string_literal(tag);
                    }
                    self.write("]");
                    self.dedent();
                }
                if let Some(v) = version {
                    self.newline();
                    self.indent();
                    self.write_indent();
                    self.write("v");
                    self.write(v);
                    self.dedent();
                }
                self.newline();
                self.indent();
                self.write_indent();
                self.write("as: ");
                self.format_expression(expression);
                self.dedent();
                self.newline();
            }
            AstNode::Instance {
                name,
                entity_type,
                fields,
            } => {
                self.write("Instance ");
                self.write(name);
                self.write(" of ");
                self.write_string_literal(entity_type);
                if !fields.is_empty() {
                    self.write(" {");
                    self.newline();
                    self.indent();
                    let mut sorted_fields: Vec<_> = fields.iter().collect();
                    sorted_fields.sort_by_key(|(k, _)| *k);
                    for (i, (field_name, expr)) in sorted_fields.iter().enumerate() {
                        self.write_indent();
                        self.write(field_name);
                        self.write(": ");
                        self.format_expression(expr);
                        if i < sorted_fields.len() - 1 {
                            self.write(",");
                        }
                        self.newline();
                    }
                    self.dedent();
                    self.write_indent();
                    self.write("}");
                }
                self.newline();
            }
            AstNode::ConceptChange {
                name,
                from_version,
                to_version,
                migration_policy,
                breaking_change,
            } => {
                self.write("ConceptChange ");
                self.write_string_literal(name);
                self.newline();
                self.indent();
                self.write_indent();
                self.write("@from_version v");
                self.write(from_version);
                self.newline();
                self.write_indent();
                self.write("@to_version v");
                self.write(to_version);
                self.newline();
                self.write_indent();
                self.write("@migration_policy ");
                self.write(migration_policy);
                self.newline();
                self.write_indent();
                self.write("@breaking_change ");
                self.write(if *breaking_change { "true" } else { "false" });
                self.dedent();
                self.newline();
            }
            AstNode::Metric {
                name,
                expression,
                metadata,
            } => {
                self.write("Metric ");
                self.write_string_literal(name);
                self.write(" as:");
                self.newline();
                self.indent();
                self.write_indent();
                self.format_expression(expression);
                // Metric annotations
                if let Some(ref interval) = metadata.refresh_interval {
                    self.newline();
                    self.write_indent();
                    self.write("@refresh_interval ");
                    self.write(&interval.num_seconds().to_string());
                    self.write(" \"seconds\"");
                }
                if let Some(ref unit) = metadata.unit {
                    self.newline();
                    self.write_indent();
                    self.write("@unit ");
                    self.write_string_literal(unit);
                }
                if let Some(threshold) = &metadata.threshold {
                    self.newline();
                    self.write_indent();
                    self.write("@threshold ");
                    self.write(&threshold.to_string());
                }
                if let Some(ref severity) = metadata.severity {
                    self.newline();
                    self.write_indent();
                    self.write("@severity ");
                    self.write_string_literal(&format!("{:?}", severity).to_lowercase());
                }
                if let Some(target) = &metadata.target {
                    self.newline();
                    self.write_indent();
                    self.write("@target ");
                    self.write(&target.to_string());
                }
                if let Some(ref window) = metadata.window {
                    self.newline();
                    self.write_indent();
                    self.write("@window ");
                    self.write(&window.num_seconds().to_string());
                    self.write(" \"seconds\"");
                }
                self.dedent();
                self.newline();
            }
            AstNode::MappingDecl {
                name,
                target,
                rules,
            } => {
                self.write("Mapping ");
                self.write_string_literal(name);
                self.write(" for ");
                self.write(&format!("{}", target).to_lowercase());
                self.write(" {");
                self.newline();
                self.indent();
                for rule in rules {
                    self.write_indent();
                    self.write(&rule.primitive_type);
                    self.write(" ");
                    self.write_string_literal(&rule.primitive_name);
                    self.write(" -> ");
                    self.write(&rule.target_type);
                    self.write(" { ");
                    let mut first = true;
                    for (k, v) in &rule.fields {
                        if !first {
                            self.write(", ");
                        }
                        self.write_string_literal(k);
                        self.write(": ");
                        self.write(&v.to_string());
                        first = false;
                    }
                    self.write(" }");
                    self.newline();
                }
                self.dedent();
                self.write_indent();
                self.write("}");
                self.newline();
            }
            AstNode::ProjectionDecl {
                name,
                target,
                overrides,
            } => {
                self.write("Projection ");
                self.write_string_literal(name);
                self.write(" for ");
                self.write(&format!("{}", target).to_lowercase());
                self.write(" {");
                self.newline();
                self.indent();
                for over in overrides {
                    self.write_indent();
                    self.write(&over.primitive_type);
                    self.write(" ");
                    self.write_string_literal(&over.primitive_name);
                    self.write(" { ");
                    let mut first = true;
                    for (k, v) in &over.fields {
                        if !first {
                            self.write(", ");
                        }
                        self.write_string_literal(k);
                        self.write(": ");
                        self.write(&v.to_string());
                        first = false;
                    }
                    self.write(" }");
                    self.newline();
                }
                self.dedent();
                self.write_indent();
                self.write("}");
                self.newline();
            }
        }
    }

    /// Format an expression using the Expression's Display implementation.
    ///
    /// The Expression type has a proper Display that outputs SEA-compatible syntax.
    fn format_expression(&mut self, expr: &Expression) {
        self.write(&format!("{}", expr));
    }

    // Helper methods

    fn write(&mut self, s: &str) {
        self.output.push_str(s);
    }

    fn write_string_literal(&mut self, s: &str) {
        self.output.push('"');
        // Escape special characters
        for c in s.chars() {
            match c {
                '"' => self.output.push_str("\\\""),
                '\\' => self.output.push_str("\\\\"),
                '\n' => self.output.push_str("\\n"),
                '\r' => self.output.push_str("\\r"),
                '\t' => self.output.push_str("\\t"),
                _ => self.output.push(c),
            }
        }
        self.output.push('"');
    }

    fn newline(&mut self) {
        self.output.push('\n');
    }

    fn write_indent(&mut self) {
        let indent = self.config.indent_string();
        for _ in 0..self.indent_level {
            self.output.push_str(&indent);
        }
    }

    fn indent(&mut self) {
        self.indent_level += 1;
    }

    fn dedent(&mut self) {
        if self.indent_level > 0 {
            self.indent_level -= 1;
        }
    }
}

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

    #[test]
    fn test_format_entity_basic() {
        let input = r#"Entity "Foo""#;
        let result = format(input, FormatConfig::default()).unwrap();
        assert!(result.contains("Entity \"Foo\""));
        assert!(result.ends_with('\n'));
    }

    #[test]
    fn test_format_entity_with_domain() {
        let input = r#"Entity   "Bar"   in   test"#;
        let result = format(input, FormatConfig::default()).unwrap();
        assert!(result.contains("Entity \"Bar\" in test"));
    }

    #[test]
    fn test_format_resource() {
        let input = r#"Resource "Money" USD in finance"#;
        let result = format(input, FormatConfig::default()).unwrap();
        assert!(result.contains("Resource \"Money\" USD in finance"));
    }

    #[test]
    fn test_format_flow() {
        let input = r#"Flow "Money" from "A" to "B" quantity 100"#;
        let result = format(input, FormatConfig::default()).unwrap();
        assert!(result.contains("Flow \"Money\" from \"A\" to \"B\" quantity 100"));
    }

    #[test]
    fn test_format_idempotent() {
        let input = r#"Entity "Test" in foo"#;
        let once = format(input, FormatConfig::default()).unwrap();
        let twice = format(&once, FormatConfig::default()).unwrap();
        assert_eq!(once, twice, "Formatting should be idempotent");
    }

    #[test]
    fn test_format_multiple_declarations() {
        let input = r#"
Entity "A"
Entity "B"
Resource "R" units
"#;
        let result = format(input, FormatConfig::default()).unwrap();
        assert!(result.contains("Entity \"A\""));
        assert!(result.contains("Entity \"B\""));
        assert!(result.contains("Resource \"R\""));
    }

    #[test]
    fn test_format_with_tabs() {
        let input = r#"
Relation "Test"
    subject: "A"
    predicate: "rel"
    object: "B"
"#;
        let config = FormatConfig::default().with_tabs();
        let result = format(input, config).unwrap();
        assert!(result.contains('\t'), "Should use tabs for indentation");
    }

    #[test]
    fn test_format_imports_sorted() {
        let input = r#"
import { B } from "z.sea"
import { A } from "a.sea"
Entity "Test"
"#;
        let config = FormatConfig::default();
        let result = format(input, config).unwrap();
        // A should come before B in sorted output
        let a_pos = result.find("a.sea").unwrap();
        let z_pos = result.find("z.sea").unwrap();
        assert!(a_pos < z_pos, "Imports should be sorted alphabetically");
    }

    #[test]
    fn test_format_policy_with_expression() {
        let input = r#"Policy test as: x = 5"#;
        let result = format(input, FormatConfig::default()).unwrap();
        // Should contain the expression, not Debug format
        assert!(result.contains("Policy test"));
        assert!(result.contains("as:"));
        // Expression should use Display format, not Debug
        assert!(!result.contains("Binary {"), "Should not use Debug format");
    }

    #[test]
    fn test_format_instance_with_fields() {
        let input = r#"
Instance test_user of "User" {
    name: "Alice",
    age: 30
}
"#;
        let result = format(input, FormatConfig::default()).unwrap();
        assert!(result.contains("Instance test_user of \"User\""));
        assert!(result.contains("name:"));
        assert!(result.contains("age:"));
    }

    #[test]
    fn test_format_preserves_header_comments() {
        let input = r#"// This is a header comment
// Second line
Entity "Foo"
"#;
        let result = format(input, FormatConfig::default()).unwrap();
        assert!(result.contains("// This is a header comment"));
        assert!(result.contains("// Second line"));
        assert!(result.contains("Entity \"Foo\""));
    }

    #[test]
    fn test_format_without_comments() {
        let input = r#"// Comment
Entity "Foo"
"#;
        let config = FormatConfig {
            preserve_comments: false,
            ..Default::default()
        };
        let result = format(input, config).unwrap();
        // Comments should not be preserved when disabled
        assert!(!result.contains("// Comment"));
        assert!(result.contains("Entity \"Foo\""));
    }
}