kymostudio-core 0.4.6

Prompt it. See it appear. Watch it animate. (Core)
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
//! Text emitters for the flowchart IR: [`to_mermaid`], [`to_d2`], [`to_dot`].
//!
//! Each turns a positionless [`Flowchart`] back into a source DSL — the target
//! lays the graph out itself, so no geometry is involved. These are the "spokes"
//! that make `mmd → {mermaid, d2, dot}` a parse-then-emit with the IR as the hub:
//!
//! - [`to_mermaid`] is the inverse of the importer (round-trip / normalize).
//! - [`to_d2`] / [`to_dot`] target node-edge DSLs whose model matches the IR
//!   directly; shapes map nearly 1:1 (D2 `cylinder`/`hexagon`, DOT `cylinder`/
//!   `hexagon`), with stadium→`oval`/rounded-box where there is no exact glyph.
//!
//! Output is deterministic: declaration order is preserved (the IR's `Vec`s),
//! and HashMaps are used only for lookup, never iteration.

use super::{Direction, FlowEdge, FlowNode, Flowchart};
use crate::model::Shape;
use std::collections::{HashMap, HashSet};

// ── Mermaid (round-trip) ─────────────────────────────────────────────────────

/// Emit the IR as Mermaid `flowchart` source — the inverse of `mermaid::parse`.
pub fn to_mermaid(fc: &Flowchart) -> String {
    let dir = match fc.direction {
        Direction::Tb => "TD",
        Direction::Bt => "BT",
        Direction::Lr => "LR",
        Direction::Rl => "RL",
    };
    let mut out = format!("flowchart {dir}\n");
    // Declare every node (with its shape) in IR order, then mark subgraph
    // membership with bare-id refs inside `subgraph … end`. This preserves
    // declaration order, so `mmd → IR → mmd → IR` is a fixpoint (a node listed
    // inside a subgraph keeps the shape it was declared with — Mermaid semantics).
    for n in &fc.nodes {
        out.push_str(&format!("  {}\n", mmd_node(n)));
    }
    for sg in &fc.subgraphs {
        if sg.title.is_empty() {
            out.push_str(&format!("  subgraph {}\n", sg.id));
        } else {
            out.push_str(&format!(
                "  subgraph {} [{}]\n",
                sg.id,
                mmd_quote(&sg.title)
            ));
        }
        for m in &sg.members {
            out.push_str(&format!("    {m}\n"));
        }
        out.push_str("  end\n");
    }
    for e in &fc.edges {
        out.push_str(&format!("  {} {} {}\n", e.src, mmd_op(e), e.dst));
    }
    out
}

fn mmd_node(n: &FlowNode) -> String {
    let (open, close) = match n.shape {
        Shape::Circle => ("((", "))"),
        Shape::Diamond => ("{", "}"),
        Shape::Hex => ("{{", "}}"),
        Shape::Cylinder => ("[(", ")]"),
        Shape::Badge => ("([", "])"),
        _ => ("[", "]"), // box & anything without a flowchart glyph
    };
    format!("{}{}{}{}", n.id, open, mmd_quote(&n.label), close)
}

fn mmd_op(e: &FlowEdge) -> String {
    let op = match (e.dashed, e.no_arrow) {
        (true, true) => "-.-",
        (true, false) => "-.->",
        (false, true) => "---",
        (false, false) => "-->",
    };
    if e.label.is_empty() {
        op.to_string()
    } else {
        format!("{op}|{}|", e.label)
    }
}

/// Mermaid quotes a label (containing spaces/delimiters) with `"…"`; inner `"`
/// collapse to `'` (the importer strips one surrounding quote pair).
fn mmd_quote(s: &str) -> String {
    format!("\"{}\"", s.replace('"', "'"))
}

// ── D2 ───────────────────────────────────────────────────────────────────────

/// Emit the IR as [D2](https://d2lang.com) source. Subgraphs become containers
/// (`g: "title" { … }`) and members are referenced by their qualified `g.id`.
pub fn to_d2(fc: &Flowchart) -> String {
    let dir = match fc.direction {
        Direction::Lr => "right",
        Direction::Rl => "left",
        Direction::Tb => "down",
        Direction::Bt => "up",
    };
    let mut out = format!("direction: {dir}\n");

    // member id → qualified `sub.id` (D2 references container children by path).
    let mut qual: HashMap<&str, String> = HashMap::new();
    for sg in &fc.subgraphs {
        for m in &sg.members {
            qual.insert(m.as_str(), format!("{}.{}", sg.id, m));
        }
    }
    let q = |id: &str| qual.get(id).cloned().unwrap_or_else(|| id.to_string());
    let by_id: HashMap<&str, &FlowNode> = fc.nodes.iter().map(|n| (n.id.as_str(), n)).collect();
    let mut in_sub: HashSet<&str> = HashSet::new();

    for sg in &fc.subgraphs {
        out.push_str(&format!("{}: {} {{\n", sg.id, d2_quote(&sg.title)));
        for m in &sg.members {
            in_sub.insert(m.as_str());
            if let Some(n) = by_id.get(m.as_str()) {
                out.push_str(&format!("  {}\n", d2_node(&n.id, n)));
            }
        }
        out.push_str("}\n");
    }
    for n in &fc.nodes {
        if !in_sub.contains(n.id.as_str()) {
            out.push_str(&format!("{}\n", d2_node(&n.id, n)));
        }
    }
    for e in &fc.edges {
        let conn = if e.no_arrow { "--" } else { "->" };
        let mut line = format!("{} {} {}", q(&e.src), conn, q(&e.dst));
        if !e.label.is_empty() {
            line.push_str(&format!(": {}", d2_quote(&e.label)));
        }
        if e.dashed {
            line.push_str(" { style.stroke-dash: 3 }");
        }
        out.push_str(&line);
        out.push('\n');
    }
    out
}

fn d2_node(local: &str, n: &FlowNode) -> String {
    let label = d2_quote(&n.label);
    match d2_shape(n.shape) {
        Some(sh) => format!("{local}: {label} {{ shape: {sh} }}"),
        None => format!("{local}: {label}"), // box → default rectangle
    }
}

fn d2_shape(s: Shape) -> Option<&'static str> {
    match s {
        Shape::Circle => Some("circle"),
        Shape::Diamond => Some("diamond"),
        Shape::Hex => Some("hexagon"),
        Shape::Cylinder => Some("cylinder"),
        Shape::Badge => Some("oval"), // stadium ≈ oval (no exact D2 glyph)
        _ => None,
    }
}

fn d2_quote(s: &str) -> String {
    format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\""))
}

// ── Graphviz DOT ─────────────────────────────────────────────────────────────

/// Emit the IR as Graphviz [DOT](https://graphviz.org/doc/info/lang.html).
/// Subgraphs become `cluster_*` subgraphs; node membership is positional (a
/// node's statement inside a cluster places it there).
pub fn to_dot(fc: &Flowchart) -> String {
    let rankdir = match fc.direction {
        Direction::Lr => "LR",
        Direction::Rl => "RL",
        Direction::Tb => "TB",
        Direction::Bt => "BT",
    };
    let mut out = String::from("digraph G {\n");
    out.push_str(&format!("  rankdir={rankdir};\n"));
    out.push_str("  node [fontsize=12];\n");

    let by_id: HashMap<&str, &FlowNode> = fc.nodes.iter().map(|n| (n.id.as_str(), n)).collect();
    let mut in_sub: HashSet<&str> = HashSet::new();

    for sg in &fc.subgraphs {
        out.push_str(&format!("  subgraph cluster_{} {{\n", sg.id));
        if !sg.title.is_empty() {
            out.push_str(&format!("    label={};\n", dot_quote(&sg.title)));
        }
        for m in &sg.members {
            in_sub.insert(m.as_str());
            if let Some(n) = by_id.get(m.as_str()) {
                out.push_str(&format!("    {} [{}];\n", n.id, dot_attrs(n)));
            }
        }
        out.push_str("  }\n");
    }
    for n in &fc.nodes {
        if !in_sub.contains(n.id.as_str()) {
            out.push_str(&format!("  {} [{}];\n", n.id, dot_attrs(n)));
        }
    }
    for e in &fc.edges {
        let mut attrs: Vec<String> = Vec::new();
        if !e.label.is_empty() {
            attrs.push(format!("label={}", dot_quote(&e.label)));
        }
        if e.dashed {
            attrs.push("style=dashed".to_string());
        }
        if e.no_arrow {
            attrs.push("dir=none".to_string());
        }
        let tail = if attrs.is_empty() {
            String::new()
        } else {
            format!(" [{}]", attrs.join(", "))
        };
        out.push_str(&format!("  {} -> {}{};\n", e.src, e.dst, tail));
    }
    out.push_str("}\n");
    out
}

fn dot_attrs(n: &FlowNode) -> String {
    let (shape, extra) = match n.shape {
        Shape::Circle => ("circle", ""),
        Shape::Diamond => ("diamond", ""),
        Shape::Hex => ("hexagon", ""),
        Shape::Cylinder => ("cylinder", ""),
        Shape::Badge => ("box", ", style=rounded"), // stadium ≈ rounded box
        _ => ("box", ""),
    };
    format!("label={}, shape={}{}", dot_quote(&n.label), shape, extra)
}

fn dot_quote(s: &str) -> String {
    format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\""))
}

#[cfg(test)]
mod tests {
    use super::{to_d2, to_dot, to_mermaid};
    use crate::flowchart::{Direction, FlowEdge, FlowNode, Flowchart, Subgraph};
    use crate::model::Shape;

    fn n(id: &str, label: &str, shape: Shape) -> FlowNode {
        FlowNode {
            id: id.into(),
            label: label.into(),
            shape,
        }
    }
    fn e(src: &str, dst: &str, label: &str, dashed: bool, no_arrow: bool) -> FlowEdge {
        FlowEdge {
            src: src.into(),
            dst: dst.into(),
            label: label.into(),
            dashed,
            no_arrow,
        }
    }
    fn fc(
        dir: Direction,
        nodes: Vec<FlowNode>,
        edges: Vec<FlowEdge>,
        subgraphs: Vec<Subgraph>,
    ) -> Flowchart {
        Flowchart {
            direction: dir,
            nodes,
            edges,
            subgraphs,
        }
    }
    fn one(shape: Shape) -> Flowchart {
        fc(Direction::Tb, vec![n("A", "Lbl", shape)], vec![], vec![])
    }

    // ── Mermaid ──────────────────────────────────────────────────────────
    #[test]
    fn mermaid_shape_wrappers() {
        assert!(to_mermaid(&one(Shape::Box)).contains("A[\"Lbl\"]"));
        assert!(to_mermaid(&one(Shape::Circle)).contains("A((\"Lbl\"))"));
        assert!(to_mermaid(&one(Shape::Diamond)).contains("A{\"Lbl\"}"));
        assert!(to_mermaid(&one(Shape::Hex)).contains("A{{\"Lbl\"}}"));
        assert!(to_mermaid(&one(Shape::Cylinder)).contains("A[(\"Lbl\")]"));
        assert!(to_mermaid(&one(Shape::Badge)).contains("A([\"Lbl\"])"));
    }

    #[test]
    fn mermaid_edge_operators_and_label() {
        let g = fc(
            Direction::Tb,
            vec![n("A", "A", Shape::Box), n("B", "B", Shape::Box)],
            vec![
                e("A", "B", "", false, false), // -->
                e("A", "B", "", false, true),  // ---
                e("A", "B", "", true, false),  // -.->
                e("A", "B", "", true, true),   // -.-
                e("A", "B", "yes", false, false),
            ],
            vec![],
        );
        let out = to_mermaid(&g);
        assert!(out.contains("A --> B"));
        assert!(out.contains("A --- B"));
        assert!(out.contains("A -.-> B"));
        assert!(out.contains("A -.- B"));
        assert!(out.contains("A -->|yes| B"));
    }

    #[test]
    fn mermaid_direction_header_and_quote() {
        assert!(to_mermaid(&fc(Direction::Lr, vec![], vec![], vec![])).starts_with("flowchart LR"));
        assert!(to_mermaid(&fc(Direction::Bt, vec![], vec![], vec![])).starts_with("flowchart BT"));
        // Inner double-quotes collapse to single quotes.
        assert!(to_mermaid(&one_label("a\"b")).contains("A[\"a'b\"]"));
    }
    fn one_label(label: &str) -> Flowchart {
        fc(
            Direction::Tb,
            vec![n("A", label, Shape::Box)],
            vec![],
            vec![],
        )
    }

    #[test]
    fn mermaid_subgraph_membership_order_preserved() {
        let g = fc(
            Direction::Tb,
            vec![n("S", "S", Shape::Box), n("A", "A", Shape::Box)],
            vec![],
            vec![Subgraph {
                parent: None,
                id: "G".into(),
                title: "Grp".into(),
                members: vec!["A".into()],
            }],
        );
        let out = to_mermaid(&g);
        // All nodes declared first (in IR order), then membership-only refs.
        assert!(out.find("S[\"S\"]").unwrap() < out.find("subgraph G [\"Grp\"]").unwrap());
        assert!(out.contains("subgraph G [\"Grp\"]\n    A\n  end"));
    }

    // ── D2 ───────────────────────────────────────────────────────────────
    #[test]
    fn d2_direction_and_shapes() {
        assert!(to_d2(&fc(Direction::Lr, vec![], vec![], vec![])).starts_with("direction: right"));
        assert!(to_d2(&fc(Direction::Rl, vec![], vec![], vec![])).starts_with("direction: left"));
        assert!(to_d2(&one(Shape::Diamond)).contains("A: \"Lbl\" { shape: diamond }"));
        assert!(to_d2(&one(Shape::Cylinder)).contains("shape: cylinder"));
        assert!(to_d2(&one(Shape::Badge)).contains("shape: oval"));
        // box → default rectangle, no shape block.
        let boxed = to_d2(&one(Shape::Box));
        assert!(boxed.contains("A: \"Lbl\"") && !boxed.contains("shape:"));
    }

    #[test]
    fn d2_edges_connector_dash_label() {
        let g = fc(
            Direction::Tb,
            vec![n("A", "A", Shape::Box), n("B", "B", Shape::Box)],
            vec![
                e("A", "B", "lbl", true, false),
                e("A", "B", "", false, true),
            ],
            vec![],
        );
        let out = to_d2(&g);
        assert!(out.contains("A -> B: \"lbl\" { style.stroke-dash: 3 }"));
        assert!(out.contains("A -- B")); // no-arrow connector
    }

    #[test]
    fn d2_container_qualifies_member_edge() {
        let g = fc(
            Direction::Tb,
            vec![n("A", "A", Shape::Box), n("B", "B", Shape::Box)],
            vec![e("A", "B", "", false, false)],
            vec![Subgraph {
                parent: None,
                id: "G".into(),
                title: "T".into(),
                members: vec!["B".into()],
            }],
        );
        let out = to_d2(&g);
        assert!(out.contains("G: \"T\" {"));
        assert!(out.contains("A -> G.B")); // member referenced by qualified path
    }

    // ── DOT ──────────────────────────────────────────────────────────────
    #[test]
    fn dot_rankdir_and_shapes() {
        assert!(to_dot(&fc(Direction::Lr, vec![], vec![], vec![])).contains("rankdir=LR;"));
        assert!(to_dot(&one(Shape::Diamond)).contains("shape=diamond"));
        assert!(to_dot(&one(Shape::Hex)).contains("shape=hexagon"));
        assert!(to_dot(&one(Shape::Badge)).contains("shape=box, style=rounded"));
    }

    #[test]
    fn dot_edge_attrs_and_escaping() {
        let g = fc(
            Direction::Tb,
            vec![n("A", "A", Shape::Box), n("B", "say \"hi\"", Shape::Box)],
            vec![e("A", "B", "go", true, false), e("A", "B", "", false, true)],
            vec![],
        );
        let out = to_dot(&g);
        assert!(out.contains("A -> B [label=\"go\", style=dashed];"));
        assert!(out.contains("A -> B [dir=none];"));
        assert!(out.contains("label=\"say \\\"hi\\\"\"")); // quotes escaped
    }

    #[test]
    fn dot_cluster_holds_members() {
        let g = fc(
            Direction::Tb,
            vec![n("A", "A", Shape::Box)],
            vec![],
            vec![Subgraph {
                parent: None,
                id: "G".into(),
                title: "T".into(),
                members: vec!["A".into()],
            }],
        );
        let out = to_dot(&g);
        assert!(out.contains("subgraph cluster_G {"));
        assert!(out.contains("label=\"T\";"));
    }

    #[test]
    fn empty_graph_is_well_formed() {
        let empty = fc(Direction::Tb, vec![], vec![], vec![]);
        assert_eq!(to_mermaid(&empty), "flowchart TD\n");
        assert_eq!(to_d2(&empty), "direction: down\n");
        assert!(to_dot(&empty).starts_with("digraph G {") && to_dot(&empty).ends_with("}\n"));
    }
}