jigs-map 0.1.0

HTML map generator for jigs pipelines
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
//! Mermaid flowchart rendering of the live jig inventory.
//!
//! Mermaid renders statically — there is no expand/collapse. To keep the
//! output readable we flatten the pipeline to leaf jigs: each composite jig
//! becomes a `subgraph` labelled with its name, containing its leaf
//! descendants. Edges connect leaves only, so a chain like
//! `a.then(composite).then(b)` becomes `a → first_leaf(composite)` and
//! `last_leaf(composite) → b`, with `composite`'s own internals laid out
//! inside its subgraph.

use jigs_core::{ChainKind, JigMeta};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::fmt::Write;

type Index = BTreeMap<&'static str, &'static JigMeta>;

/// Render the pipeline rooted at `entry` (or the first registered jig if
/// `None`) as a Mermaid `flowchart TD` document, without any surrounding
/// markdown fence.
pub fn to_mermaid(entry: Option<&str>) -> String {
    let all: Index = jigs_core::all_jigs().map(|m| (m.name, m)).collect();
    let entry = entry
        .map(str::to_string)
        .or_else(|| all.keys().next().map(|s| s.to_string()))
        .unwrap_or_default();
    render(&all, &entry)
}

/// Render the pipeline as a Markdown document with a Mermaid code fence,
/// suitable for committing alongside the HTML map.
pub fn to_markdown(entry: Option<&str>, title: &str) -> String {
    let mut s = String::new();
    writeln!(s, "# {title}\n").ok();
    writeln!(s, "```mermaid").ok();
    s.push_str(&to_mermaid(entry));
    writeln!(s, "```").ok();
    s
}

fn render(all: &Index, entry: &str) -> String {
    let mut out = String::from("flowchart TD\n");
    if entry.is_empty() {
        return out;
    }
    let mut leaves = Vec::new();
    collect_leaves(entry, all, &mut HashSet::new(), &mut leaves);
    for name in &leaves {
        let (open, close, label) = node_visual(name, all);
        writeln!(out, "  {name}{open}\"{label}\"{close}").ok();
    }
    out.push('\n');
    let homes = compute_leaf_homes(entry, all);
    emit(&mut out, all, entry, &homes, &mut HashSet::new(), 0, true);
    out.push_str(
        "\n  %% shape legend: rect = Request → Request, rhombus = switching (Request → Response/Branch), stadium = Response → Response\n",
    );
    out
}

/// Returns true if `m` is a fork dispatcher: every chain entry was added
/// via `fork!`. We treat such jigs as decision points rather than as
/// linear pipelines.
fn is_fork_dispatcher(m: &JigMeta) -> bool {
    !m.chain.is_empty() && m.chain.iter().all(|c| c.kind == ChainKind::Fork)
}

/// For each leaf jig that appears as a fork-arm of one or more
/// dispatchers, pick the shallowest such dispatcher in the inclusion
/// tree. That dispatcher becomes the leaf's "home" — the only place the
/// leaf will be re-mentioned inside a subgraph block. Shared leaves
/// (e.g., a `not_found` reused across dispatchers) end up at their LCA
/// instead of being silently captured by the first dispatcher that
/// references them.
fn compute_leaf_homes<'a>(entry: &'a str, all: &'a Index) -> HashMap<&'a str, &'a str> {
    let mut depths: HashMap<&'a str, usize> = HashMap::new();
    let mut leaf_parents: HashMap<&'a str, Vec<&'a str>> = HashMap::new();
    walk_for_homes(entry, all, 0, &mut depths, &mut leaf_parents);
    leaf_parents
        .into_iter()
        .filter_map(|(leaf, parents)| {
            parents
                .into_iter()
                .min_by_key(|p| depths.get(p).copied().unwrap_or(usize::MAX))
                .map(|p| (leaf, p))
        })
        .collect()
}

fn walk_for_homes<'a>(
    name: &'a str,
    all: &'a Index,
    depth: usize,
    depths: &mut HashMap<&'a str, usize>,
    leaf_parents: &mut HashMap<&'a str, Vec<&'a str>>,
) {
    if depths.contains_key(name) {
        return;
    }
    depths.insert(name, depth);
    let Some(m) = all.get(name) else { return };
    if m.chain.is_empty() {
        return;
    }
    for c in m.chain {
        if c.kind == ChainKind::Fork {
            let is_leaf = all.get(c.name).is_none_or(|cm| cm.chain.is_empty());
            if is_leaf {
                leaf_parents.entry(c.name).or_default().push(name);
            }
        }
        walk_for_homes(c.name, all, depth + 1, depths, leaf_parents);
    }
}

/// Pick the mermaid shape and label for a leaf node. Categories:
///   * Request → Request   ⇒ rectangle  `[..]`
///   * Request → switch    ⇒ rhombus    `{..}`  (Response, Branch, Pending)
///   * Response → Response ⇒ stadium    `([..])`
///
/// Externals or unrecognised pairs render as a flag/asymmetric shape `>..]`.
fn node_visual(name: &str, all: &Index) -> (&'static str, &'static str, String) {
    let Some(m) = all.get(name) else {
        return (">", "]", format!("{name}<br/><i>external</i>"));
    };
    let async_prefix = if m.is_async { "async " } else { "" };
    let (open, close, sig) = match (m.input, m.kind) {
        ("Request", "Request") => ("[", "]", "req → req"),
        ("Request", "Response") => ("{", "}", "req → res"),
        ("Request", "Branch") => ("{", "}", "req → branch"),
        ("Request", "Pending") => ("{", "}", "req → async"),
        ("Response", "Response") => ("([", "])", "res → res"),
        _ => ("[", "]", "?"),
    };
    (
        open,
        close,
        format!("{name}<br/><i>{async_prefix}{sig}</i>"),
    )
}

/// Walk the pipeline and accumulate the names of every leaf (chain-less)
/// jig that participates, so each can be declared exactly once.
fn collect_leaves(name: &str, all: &Index, seen: &mut HashSet<String>, out: &mut Vec<String>) {
    if !seen.insert(name.to_string()) {
        return;
    }
    match all.get(name) {
        Some(m) if !m.chain.is_empty() => {
            for c in m.chain {
                collect_leaves(c.name, all, seen, out);
            }
        }
        _ => out.push(name.to_string()),
    }
}

fn emit(
    out: &mut String,
    all: &Index,
    name: &str,
    homes: &HashMap<&str, &str>,
    seen: &mut HashSet<String>,
    depth: usize,
    is_root: bool,
) {
    if !seen.insert(name.to_string()) {
        return;
    }
    let Some(m) = all.get(name) else { return };
    if m.chain.is_empty() {
        return;
    }
    let pad = "  ".repeat(depth);
    if !is_root {
        writeln!(out, "{pad}subgraph {name} [\"{name}\"]").ok();
        writeln!(out, "{pad}  direction TB").ok();
    }
    let edge_pad = if is_root {
        String::new()
    } else {
        format!("{pad}  ")
    };
    for w in m.chain.windows(2) {
        // Sibling fork arms don't flow into each other — exactly one runs.
        if w[0].kind == ChainKind::Fork && w[1].kind == ChainKind::Fork {
            continue;
        }
        let from = last_leaf(w[0].name, all, &mut HashSet::new());
        let to = first_leaf(w[1].name, all, &mut HashSet::new());
        writeln!(out, "{edge_pad}{from} --> {to}").ok();
    }
    for c in m.chain {
        match all.get(c.name) {
            Some(cm) if !cm.chain.is_empty() => {
                emit(out, all, c.name, homes, seen, depth + 1, false);
            }
            // Re-mention the leaf only inside its assigned home — the
            // shallowest dispatcher that references it as an arm. This
            // lets mermaid place shared leaves at their LCA rather
            // than capturing them in the first dispatcher visited.
            _ if c.kind == ChainKind::Fork && homes.get(c.name).copied() == Some(name) => {
                writeln!(out, "{edge_pad}{}", c.name).ok();
            }
            _ => {}
        }
    }
    if !is_root {
        writeln!(out, "{pad}end").ok();
    }
}

fn first_leaf<'a>(name: &'a str, all: &'a Index, seen: &mut HashSet<&'a str>) -> &'a str {
    if !seen.insert(name) {
        return name;
    }
    match all.get(name) {
        Some(m) if !m.chain.is_empty() => {
            // Fork dispatchers have no single "first" leaf — the first thing
            // the request meets is the dispatcher subgraph border. Stop here
            // so the bridge edge lands on the subgraph, not on one arm.
            if is_fork_dispatcher(m) {
                name
            } else {
                first_leaf(m.chain[0].name, all, seen)
            }
        }
        _ => name,
    }
}

fn last_leaf<'a>(name: &'a str, all: &'a Index, seen: &mut HashSet<&'a str>) -> &'a str {
    if !seen.insert(name) {
        return name;
    }
    match all.get(name) {
        Some(m) if !m.chain.is_empty() => {
            if is_fork_dispatcher(m) {
                name
            } else {
                last_leaf(m.chain[m.chain.len() - 1].name, all, seen)
            }
        }
        _ => name,
    }
}

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

    fn fake(items: Vec<JigMeta>) -> Index {
        items
            .into_iter()
            .map(|m| {
                let r: &'static JigMeta = Box::leak(Box::new(m));
                (r.name, r)
            })
            .collect()
    }

    fn then_chain(names: &[&'static str]) -> &'static [ChainStep] {
        let v: Vec<ChainStep> = names
            .iter()
            .map(|n| ChainStep {
                name: n,
                kind: ChainKind::Then,
            })
            .collect();
        Box::leak(v.into_boxed_slice())
    }

    fn fork_chain(names: &[&'static str]) -> &'static [ChainStep] {
        let v: Vec<ChainStep> = names
            .iter()
            .map(|n| ChainStep {
                name: n,
                kind: ChainKind::Fork,
            })
            .collect();
        Box::leak(v.into_boxed_slice())
    }

    fn meta(name: &'static str, kind: &'static str, chain: &[&'static str]) -> JigMeta {
        meta_full(name, "Request", kind, false, then_chain(chain))
    }
    fn meta_full(
        name: &'static str,
        input: &'static str,
        kind: &'static str,
        is_async: bool,
        chain: &'static [ChainStep],
    ) -> JigMeta {
        JigMeta {
            name,
            file: "t.rs",
            line: 1,
            kind,
            input,
            is_async,
            chain,
        }
    }

    #[test]
    fn entry_is_not_a_floating_node() {
        let all = fake(vec![
            meta("root", "Response", &["a", "b"]),
            meta("a", "Request", &[]),
            meta("b", "Branch", &[]),
        ]);
        let m = render(&all, "root");
        assert!(
            !m.contains("root["),
            "entry should not be declared as a leaf node"
        );
        assert!(!m.contains("subgraph root"), "entry should not be wrapped");
        assert!(m.contains("a --> b"));
    }

    #[test]
    fn composite_child_inlines_into_parent_chain() {
        let all = fake(vec![
            meta("root", "Response", &["a", "sub", "b"]),
            meta("a", "Request", &[]),
            meta("b", "Response", &[]),
            meta("sub", "Response", &["x", "y"]),
            meta("x", "Request", &[]),
            meta("y", "Response", &[]),
        ]);
        let m = render(&all, "root");
        // edges from parent chain hop to the first/last leaf of the composite
        assert!(m.contains("a --> x"), "{m}");
        assert!(m.contains("y --> b"), "{m}");
        // composite is its own subgraph
        assert!(m.contains("subgraph sub"));
        assert!(m.contains("x --> y"));
        // composite is never declared as a leaf node
        assert!(!m.contains("sub[\""));
    }

    #[test]
    fn shape_varies_by_category() {
        let all = fake(vec![
            meta_full(
                "root",
                "Request",
                "Response",
                false,
                then_chain(&["a", "b", "c"]),
            ),
            meta_full("a", "Request", "Request", false, then_chain(&[])),
            meta_full("b", "Request", "Branch", false, then_chain(&[])),
            meta_full("c", "Response", "Response", false, then_chain(&[])),
        ]);
        let m = render(&all, "root");
        assert!(m.contains("a[\"a"), "Request→Request should be a rect: {m}");
        assert!(m.contains("b{\"b"), "switching should be a rhombus: {m}");
        assert!(
            m.contains("c([\"c"),
            "Response→Response should be a stadium: {m}"
        );
        assert!(m.contains("req → req"));
        assert!(m.contains("req → branch"));
        assert!(m.contains("res → res"));
    }

    #[test]
    fn async_prefix_in_label() {
        let all = fake(vec![
            meta_full("root", "Request", "Response", false, then_chain(&["a"])),
            meta_full("a", "Request", "Request", true, then_chain(&[])),
        ]);
        let m = render(&all, "root");
        assert!(m.contains("async req → req"), "async marker missing: {m}");
    }

    #[test]
    fn leaf_fork_arms_are_placed_inside_parent_subgraph() {
        let all = fake(vec![
            meta("entry", "Response", &["router"]),
            meta_full(
                "router",
                "Request",
                "Response",
                false,
                fork_chain(&["a", "b"]),
            ),
            meta("a", "Response", &[]),
            meta("b", "Response", &[]),
        ]);
        let m = render(&all, "entry");
        let after = m.split("subgraph router").nth(1).unwrap_or("");
        let block = after.split("end").next().unwrap_or("");
        assert!(block.contains("a"), "router subgraph should contain a: {m}");
        assert!(block.contains("b"), "router subgraph should contain b: {m}");
    }

    #[test]
    fn shared_leaf_arm_lives_at_lca() {
        // `not_found` is a fork arm of both inner dispatchers. The shallowest
        // dispatcher that references it should be the only one that captures
        // it visually.
        let all = fake(vec![
            meta("entry", "Response", &["outer"]),
            meta_full(
                "outer",
                "Request",
                "Response",
                false,
                fork_chain(&["inner_a", "inner_b", "not_found"]),
            ),
            meta_full(
                "inner_a",
                "Request",
                "Response",
                false,
                fork_chain(&["leaf_a", "not_found"]),
            ),
            meta_full(
                "inner_b",
                "Request",
                "Response",
                false,
                fork_chain(&["leaf_b", "not_found"]),
            ),
            meta("leaf_a", "Response", &[]),
            meta("leaf_b", "Response", &[]),
            meta("not_found", "Response", &[]),
        ]);
        let m = render(&all, "entry");
        // inner_a should not re-mention not_found (its inner block ends before
        // the matching `end`).
        let inner_a_block = m.split("subgraph inner_a").nth(1).unwrap_or("");
        let inner_a_block = inner_a_block.split("end").next().unwrap_or("");
        assert!(
            !inner_a_block.contains("not_found"),
            "not_found should NOT live inside inner_a: {m}"
        );
        // outer should re-mention not_found before its own closing `end`.
        let outer_block = m.split("subgraph outer").nth(1).unwrap_or("");
        let outer_block = outer_block
            .split("\n  end\n\n  %%")
            .next()
            .unwrap_or(outer_block);
        assert!(
            outer_block.contains("not_found"),
            "not_found should live inside outer (LCA): {m}"
        );
        assert!(inner_a_block.contains("leaf_a"), "{m}");
    }

    #[test]
    fn fork_arms_are_siblings_no_inter_arm_edges() {
        let all = fake(vec![
            meta_full(
                "router",
                "Request",
                "Response",
                false,
                fork_chain(&["a", "b", "c"]),
            ),
            meta("a", "Response", &[]),
            meta("b", "Response", &[]),
            meta("c", "Response", &[]),
        ]);
        let m = render(&all, "router");
        assert!(!m.contains("a --> b"), "no edge between fork siblings: {m}");
        assert!(!m.contains("b --> c"), "no edge between fork siblings: {m}");
    }

    #[test]
    fn no_styling_emitted() {
        let all = fake(vec![
            meta("root", "Response", &["g"]),
            meta("g", "Branch", &[]),
        ]);
        let m = render(&all, "root");
        assert!(!m.contains("classDef"));
        assert!(!m.contains("class g"));
    }
}