orrery-parser 0.1.1

Parser for the Orrery diagram language
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
use orrery_core::{
    identifier::Id,
    semantic::{Block, DiagramKind, Element, LayoutEngine, NoteAlign},
};
use orrery_parser::{ElaborateConfig, parse};

#[test]
fn test_simple_component_diagram() {
    let source = r#"
        diagram component;
        box: Rectangle;
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    assert_eq!(diagram.kind(), DiagramKind::Component);

    let elements = diagram.scope().elements();
    assert_eq!(elements.len(), 1);

    match &elements[0] {
        Element::Node(node) => {
            assert_eq!(node.id(), Id::new("box"));
            assert_eq!(node.display_text(), "box");
            assert!(matches!(node.block(), Block::None));
        }
        _ => panic!("Expected Node element"),
    }
}

#[test]
fn test_simple_sequence_diagram() {
    let source = r#"
        diagram sequence;
        client: Rectangle;
        server: Rectangle;
        client -> server: "Request";
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    assert_eq!(diagram.kind(), DiagramKind::Sequence);

    let elements = diagram.scope().elements();
    assert_eq!(elements.len(), 3);

    // First element: client node
    match &elements[0] {
        Element::Node(node) => {
            assert_eq!(node.id(), Id::new("client"));
        }
        _ => panic!("Expected Node element for client"),
    }

    // Second element: server node
    match &elements[1] {
        Element::Node(node) => {
            assert_eq!(node.id(), Id::new("server"));
        }
        _ => panic!("Expected Node element for server"),
    }

    // Third element: relation
    match &elements[2] {
        Element::Relation(relation) => {
            assert_eq!(relation.source(), Id::new("client"));
            assert_eq!(relation.target(), Id::new("server"));
            assert_eq!(
                relation.text().map(|t| t.content().to_string()),
                Some("Request".to_string())
            );
        }
        _ => panic!("Expected Relation element"),
    }
}

#[test]
fn test_node_with_display_name() {
    let source = r#"
        diagram component;
        svc as "User Service": Rectangle;
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    let elements = diagram.scope().elements();
    assert_eq!(elements.len(), 1);

    match &elements[0] {
        Element::Node(node) => {
            assert_eq!(node.id(), Id::new("svc"));
            assert_eq!(node.display_text(), "User Service");
        }
        _ => panic!("Expected Node element"),
    }
}

#[test]
fn test_with_type_definitions() {
    let source = r#"
        diagram component;
        type Button = Rectangle[fill_color="blue"];
        submit: Button;
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    let elements = diagram.scope().elements();
    assert_eq!(elements.len(), 1);

    match &elements[0] {
        Element::Node(node) => {
            assert_eq!(node.id(), Id::new("submit"));
        }
        _ => panic!("Expected Node element"),
    }
}

#[test]
fn test_with_relations() {
    let source = r#"
        diagram component;
        a: Rectangle;
        b: Rectangle;
        a -> b;
        b <- a;
        a <-> b;
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    let elements = diagram.scope().elements();
    assert_eq!(elements.len(), 5); // 2 nodes + 3 relations

    // Check nodes
    assert!(matches!(&elements[0], Element::Node(n) if n.id() == Id::new("a")));
    assert!(matches!(&elements[1], Element::Node(n) if n.id() == Id::new("b")));

    // Check relations
    match &elements[2] {
        Element::Relation(r) => {
            assert_eq!(r.source(), Id::new("a"));
            assert_eq!(r.target(), Id::new("b"));
        }
        _ => panic!("Expected Relation"),
    }

    match &elements[3] {
        Element::Relation(r) => {
            assert_eq!(r.source(), Id::new("b"));
            assert_eq!(r.target(), Id::new("a"));
        }
        _ => panic!("Expected Relation"),
    }

    match &elements[4] {
        Element::Relation(r) => {
            assert_eq!(r.source(), Id::new("a"));
            assert_eq!(r.target(), Id::new("b"));
        }
        _ => panic!("Expected Relation"),
    }
}

#[test]
fn test_syntax_error() {
    let source = r#"
        diagram component
        missing_semicolon: Rectangle;
    "#;

    let result = parse(source, ElaborateConfig::default());
    assert!(result.is_err(), "Should fail on syntax error");

    let err = result.unwrap_err();
    assert!(!err.diagnostics().is_empty());
    let diag = &err.diagnostics()[0];
    assert!(!diag.message().is_empty());
    assert!(!diag.labels().is_empty() && !diag.labels()[0].span().is_empty());
}

#[test]
fn test_with_custom_config() {
    let source = r#"
        diagram component;
        box: Rectangle;
    "#;

    let config = ElaborateConfig::new(LayoutEngine::Sugiyama, LayoutEngine::Basic);
    let diagram = parse(source, config).expect("Failed to parse");

    assert_eq!(diagram.layout_engine(), LayoutEngine::Sugiyama);
}

#[test]
fn test_diagram_layout_attribute() {
    let source = r#"
        diagram component [layout_engine="sugiyama"];
        box: Rectangle;
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    // Diagram-level attribute overrides config
    assert_eq!(diagram.layout_engine(), LayoutEngine::Sugiyama);
}

#[test]
fn test_with_notes() {
    let source = r#"
        diagram sequence;
        client: Rectangle;
        note [on=[client]]: "Important note";
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    let elements = diagram.scope().elements();
    assert_eq!(elements.len(), 2);

    match &elements[1] {
        Element::Note(note) => {
            assert_eq!(note.content(), "Important note");
            assert_eq!(note.on().len(), 1);
            assert_eq!(note.on()[0], Id::new("client"));
            assert_eq!(note.align(), NoteAlign::Over); // Default for sequence diagrams
        }
        _ => panic!("Expected Note element"),
    }
}

#[test]
fn test_note_with_alignment() {
    let source = r#"
        diagram sequence;
        client: Rectangle;
        note [on=[client], align="left"]: "Left note";
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    let elements = diagram.scope().elements();
    match &elements[1] {
        Element::Note(note) => {
            assert_eq!(note.align(), NoteAlign::Left);
        }
        _ => panic!("Expected Note element"),
    }
}

#[test]
fn test_with_fragments() {
    let source = r#"
        diagram sequence;
        client: Rectangle;
        server: Rectangle;

        opt "Optional section" {
            client -> server: "Maybe";
        };
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    let elements = diagram.scope().elements();
    assert_eq!(elements.len(), 3); // 2 nodes + 1 fragment

    match &elements[2] {
        Element::Fragment(fragment) => {
            assert_eq!(fragment.operation(), "opt");
            assert_eq!(fragment.sections().len(), 1);

            let section = &fragment.sections()[0];
            assert_eq!(section.title(), Some("Optional section"));
            assert_eq!(section.elements().len(), 1);

            match &section.elements()[0] {
                Element::Relation(r) => {
                    assert_eq!(r.source(), Id::new("client"));
                    assert_eq!(r.target(), Id::new("server"));
                }
                _ => panic!("Expected Relation inside fragment"),
            }
        }
        _ => panic!("Expected Fragment element"),
    }
}

#[test]
fn test_alt_fragment_with_sections() {
    let source = r#"
        diagram sequence;
        client: Rectangle;
        server: Rectangle;

        alt "Success" {
            client -> server: "OK";
        } else "Failure" {
            client -> server: "Error";
        };
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    let elements = diagram.scope().elements();

    match &elements[2] {
        Element::Fragment(fragment) => {
            assert_eq!(fragment.operation(), "alt");
            assert_eq!(fragment.sections().len(), 2);

            assert_eq!(fragment.sections()[0].title(), Some("Success"));
            assert_eq!(fragment.sections()[1].title(), Some("Failure"));
        }
        _ => panic!("Expected Fragment element"),
    }
}

#[test]
fn test_nested_components() {
    let source = r#"
        diagram component;
        container: Rectangle {
            child1: Rectangle;
            child2: Rectangle;
        };
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    let elements = diagram.scope().elements();
    assert_eq!(elements.len(), 1);

    match &elements[0] {
        Element::Node(node) => {
            assert_eq!(node.id(), Id::new("container"));

            match node.block() {
                Block::Scope(scope) => {
                    assert_eq!(scope.elements().len(), 2);

                    match &scope.elements()[0] {
                        Element::Node(child) => {
                            // Nested children have qualified IDs
                            assert_eq!(child.id(), Id::new("container::child1"));
                        }
                        _ => panic!("Expected child Node"),
                    }

                    match &scope.elements()[1] {
                        Element::Node(child) => {
                            assert_eq!(child.id(), Id::new("container::child2"));
                        }
                        _ => panic!("Expected child Node"),
                    }
                }
                _ => panic!("Expected Block::Scope"),
            }
        }
        _ => panic!("Expected Node element"),
    }
}

#[test]
fn test_activation() {
    let source = r#"
        diagram sequence;
        client: Rectangle;
        server: Rectangle;

        activate client {
            client -> server: "Request";
        };
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    let elements = diagram.scope().elements();
    assert_eq!(elements.len(), 5); // 2 nodes + activate + relation + deactivate

    match &elements[2] {
        Element::Activate(activate) => {
            assert_eq!(activate.component(), Id::new("client"));
        }
        _ => panic!("Expected Activate element"),
    }

    match &elements[3] {
        Element::Relation(r) => {
            assert_eq!(r.source(), Id::new("client"));
            assert_eq!(r.target(), Id::new("server"));
        }
        _ => panic!("Expected Relation element"),
    }

    match &elements[4] {
        Element::Deactivate(id) => {
            assert_eq!(*id, Id::new("client"));
        }
        _ => panic!("Expected Deactivate element"),
    }
}

#[test]
fn test_relation_with_label() {
    let source = r#"
        diagram component;
        a: Rectangle;
        b: Rectangle;
        a -> b: "connects to";
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    let elements = diagram.scope().elements();

    match &elements[2] {
        Element::Relation(r) => {
            let text = r.text().expect("Should have label");
            assert_eq!(text.content(), "connects to");
        }
        _ => panic!("Expected Relation"),
    }
}

#[test]
fn test_cross_level_relation() {
    let source = r#"
        diagram component;
        parent: Rectangle {
            child: Rectangle;
        };
        external: Rectangle;
        parent::child -> external;
    "#;

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    let elements = diagram.scope().elements();
    assert_eq!(elements.len(), 3); // parent, external, relation

    match &elements[2] {
        Element::Relation(r) => {
            // The source should be the fully qualified path
            assert_eq!(r.source(), Id::new("parent::child"));
            assert_eq!(r.target(), Id::new("external"));
        }
        _ => panic!("Expected Relation"),
    }
}

#[test]
fn test_empty_diagram() {
    let source = "diagram component;";

    let diagram = parse(source, ElaborateConfig::default()).expect("Failed to parse");

    assert_eq!(diagram.kind(), DiagramKind::Component);
    assert!(diagram.scope().elements().is_empty());
}