mermaid-text 0.43.0

Render Mermaid diagrams as Unicode box-drawing text — no browser, no image protocols, pure Rust
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
//! Diagram-type detection from the first non-blank line of Mermaid source.

use crate::Error;

/// The diagram types that this crate can handle.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagramKind {
    /// `graph <direction>` or `flowchart <direction>` diagrams.
    Flowchart,
    /// `sequenceDiagram` diagrams.
    Sequence,
    /// `stateDiagram` and `stateDiagram-v2` diagrams. Both keywords share the
    /// same grammar in upstream Mermaid; only the JS renderer differs.
    State,
    /// `pie` charts. Render as a horizontal bar chart in text mode.
    Pie,
    /// `erDiagram` (entity-relationship). Render as labelled entity
    /// boxes joined by cardinality-tagged relationship lines.
    Er,
    /// `classDiagram` (UML class diagram). Render as a layered box-table
    /// diagram with boxes per class and labelled relationship lines.
    Class,
    /// `journey` (user-journey) diagrams. Render as a section/task tree
    /// with filled-star satisfaction scores per step.
    Journey,
    /// `gantt` diagrams. Render as a horizontal bar chart with one row
    /// per task aligned to a date axis (Phase 1 — no status tags,
    /// excludes, or milestones).
    Gantt,
    /// `timeline` diagrams. Render as a vertical bullet-on-a-wire flow with
    /// one row per time period and event text hanging off bullet connectors
    /// (Phase 1 — no custom themes or colour, no `&` relationship links).
    Timeline,
    /// `gitGraph` diagrams. Render as a lane-based commit graph with
    /// branches as vertical columns and commits flowing top-to-bottom
    /// (Phase 1 — no custom themes, orientation, or extended commit types).
    GitGraph,
    /// `mindmap` diagrams. Render as a vertical tree with the root in a
    /// rounded box at the top and children branching below with `├──` / `└──`
    /// connectors (Phase 1 — no shape variants or icons).
    Mindmap,
    /// `quadrantChart` diagrams. Render as a 2x2 priority matrix with labeled
    /// quadrants and proportionally-placed data points (Phase 1 — no custom
    /// point styling or background colours).
    QuadrantChart,
    /// `requirementDiagram` diagrams. Render as labeled requirement and element
    /// boxes with relationship arcs listed below (Phase 1 — vertical stacking,
    /// no graphical relationship lines).
    RequirementDiagram,
    /// `sankey-beta` (and `sankey`) diagrams. Render as a grouped-arrow list
    /// with source nodes as headers and indented arcs showing flow values
    /// (Phase 1 — grouped list layout; true proportional sankey with
    /// Sugiyama band routing is planned for a future phase).
    Sankey,
    /// `xychart-beta` (and `xychart`) diagrams. Render as a bar/line chart
    /// with a categorical or numeric x-axis and a numeric y-axis
    /// (Phase 1 — last bar and last line series only; horizontal orientation
    /// is parsed but rendered vertically; no custom colours or point styling).
    XyChart,
    /// `block-beta` (or `block`) diagrams. Render as a fixed-width grid of
    /// rectangle blocks with an edge summary below (Phase 1 — rectangles only,
    /// no nested blocks, no vertical spanning, no custom colours).
    BlockDiagram,
    /// `architecture-beta` (or `architecture`) diagrams. Render as labeled
    /// group boxes containing service boxes with a connection summary below
    /// (Phase 1 — no icon rendering, no spatial edge routing, no junction nodes).
    Architecture,
    /// `packet-beta` (or `packet`) diagrams. Render as a 32-bit-wide row table
    /// with field labels occupying their bit ranges and a bit-number ruler above
    /// each row (Phase 1 — fixed 32-bit row width; no custom colours).
    Packet,
}

/// Detect the kind of Mermaid diagram described by `input`.
///
/// Only the first non-blank, non-comment line is examined.  Lines beginning
/// with `%%` are treated as comments and skipped.
///
/// # Arguments
///
/// * `input` — the Mermaid source string (need not be fully valid)
///
/// # Returns
///
/// The detected [`DiagramKind`] on success.
///
/// # Errors
///
/// Returns [`Error::EmptyInput`] if `input` contains no non-blank lines.
/// Returns [`Error::UnsupportedDiagram`] if the diagram type is not supported.
///
/// # Examples
///
/// ```
/// use mermaid_text::detect::{detect, DiagramKind};
///
/// assert_eq!(detect("graph LR\nA-->B").unwrap(), DiagramKind::Flowchart);
/// assert_eq!(detect("flowchart TD\nA-->B").unwrap(), DiagramKind::Flowchart);
/// assert_eq!(detect("sequenceDiagram\nA->>B: hi").unwrap(), DiagramKind::Sequence);
/// assert_eq!(detect("pie title Pets").unwrap(), DiagramKind::Pie);
/// assert_eq!(detect("gantt\ntitle Roadmap").unwrap(), DiagramKind::Gantt);
/// assert_eq!(detect("timeline\n2002 : LinkedIn").unwrap(), DiagramKind::Timeline);
/// assert_eq!(detect("gitGraph\ncommit").unwrap(), DiagramKind::GitGraph);
/// assert_eq!(detect("mindmap\n  root").unwrap(), DiagramKind::Mindmap);
/// assert_eq!(detect("quadrantChart\nA: [0.5, 0.5]").unwrap(), DiagramKind::QuadrantChart);
/// assert_eq!(detect("requirementDiagram\n").unwrap(), DiagramKind::RequirementDiagram);
/// assert_eq!(detect("sankey-beta\nA,B,1.0").unwrap(), DiagramKind::Sankey);
/// assert_eq!(detect("xychart-beta\nbar [1,2,3]").unwrap(), DiagramKind::XyChart);
/// assert_eq!(detect("xychart\nbar [1,2,3]").unwrap(), DiagramKind::XyChart);
/// assert_eq!(detect("block-beta\n    A B").unwrap(), DiagramKind::BlockDiagram);
/// assert_eq!(detect("block\n    A B").unwrap(), DiagramKind::BlockDiagram);
/// assert_eq!(detect("architecture-beta\n    service s(server)[S]").unwrap(), DiagramKind::Architecture);
/// assert_eq!(detect("architecture\n    service s(server)[S]").unwrap(), DiagramKind::Architecture);
/// assert_eq!(detect("packet-beta\n    0-15: \"Src\"").unwrap(), DiagramKind::Packet);
/// assert_eq!(detect("packet\n    0-15: \"Src\"").unwrap(), DiagramKind::Packet);
/// assert!(detect("").is_err());
/// ```
pub fn detect(input: &str) -> Result<DiagramKind, Error> {
    let first = input
        .lines()
        .map(str::trim)
        .find(|l| !l.is_empty() && !l.starts_with("%%"))
        .ok_or(Error::EmptyInput)?;

    // The first token (before any whitespace) determines the diagram type.
    let keyword = first.split_whitespace().next().unwrap_or(first);

    match keyword.to_lowercase().as_str() {
        "graph" | "flowchart" => Ok(DiagramKind::Flowchart),
        "sequencediagram" => Ok(DiagramKind::Sequence),
        "statediagram" | "statediagram-v2" => Ok(DiagramKind::State),
        "pie" => Ok(DiagramKind::Pie),
        "erdiagram" => Ok(DiagramKind::Er),
        "classdiagram" => Ok(DiagramKind::Class),
        "journey" => Ok(DiagramKind::Journey),
        "gantt" => Ok(DiagramKind::Gantt),
        "timeline" => Ok(DiagramKind::Timeline),
        // gitGraph is camelCase in Mermaid spec; match case-insensitively for
        // resilience against linters/formatters that normalise the keyword.
        "gitgraph" => Ok(DiagramKind::GitGraph),
        "mindmap" => Ok(DiagramKind::Mindmap),
        "quadrantchart" => Ok(DiagramKind::QuadrantChart),
        // requirementDiagram is camelCase in the Mermaid spec; match
        // case-insensitively for robustness against formatting tools.
        "requirementdiagram" => Ok(DiagramKind::RequirementDiagram),
        // `sankey-beta` is the official Mermaid keyword; `sankey` is also
        // accepted as a relaxed alias for resilience against linters.
        "sankey-beta" | "sankey" => Ok(DiagramKind::Sankey),
        // `xychart-beta` is the official Mermaid keyword; `xychart` is also
        // accepted as a relaxed alias for resilience against linters.
        "xychart-beta" | "xychart" => Ok(DiagramKind::XyChart),
        // `block-beta` is the official Mermaid keyword; `block` is accepted
        // as a relaxed alias for resilience against linters.
        "block-beta" | "block" => Ok(DiagramKind::BlockDiagram),
        // `architecture-beta` is the official Mermaid keyword; `architecture`
        // is accepted as a relaxed alias for resilience against linters.
        "architecture-beta" | "architecture" => Ok(DiagramKind::Architecture),
        // `packet-beta` is the official Mermaid keyword; `packet` is accepted
        // as a relaxed alias for resilience against linters.
        "packet-beta" | "packet" => Ok(DiagramKind::Packet),
        other => Err(Error::UnsupportedDiagram(other.to_string())),
    }
}

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

    #[test]
    fn detects_graph_keyword() {
        assert_eq!(detect("graph LR\nA-->B").unwrap(), DiagramKind::Flowchart);
    }

    #[test]
    fn detects_flowchart_keyword() {
        assert_eq!(
            detect("flowchart TD\nA-->B").unwrap(),
            DiagramKind::Flowchart
        );
    }

    #[test]
    fn empty_returns_error() {
        assert!(matches!(detect(""), Err(Error::EmptyInput)));
        assert!(matches!(detect("   \n  "), Err(Error::EmptyInput)));
    }

    #[test]
    fn unknown_type_returns_error() {
        // A truly unsupported type returns UnsupportedDiagram.
        assert!(matches!(
            detect("unknownDiagram title Roadmap"),
            Err(Error::UnsupportedDiagram(_))
        ));
    }

    #[test]
    fn detects_gantt_keyword() {
        assert_eq!(detect("gantt\ntitle A Plan").unwrap(), DiagramKind::Gantt);
        // Case-insensitive.
        assert_eq!(detect("Gantt").unwrap(), DiagramKind::Gantt);
    }

    #[test]
    fn detects_pie_keyword() {
        assert_eq!(
            detect("pie title Pets\n\"Dogs\" : 5").unwrap(),
            DiagramKind::Pie
        );
        assert_eq!(detect("PIE\n\"X\" : 1").unwrap(), DiagramKind::Pie);
    }

    #[test]
    fn skips_comment_lines() {
        assert_eq!(
            detect("%% This is a comment\ngraph LR\nA-->B").unwrap(),
            DiagramKind::Flowchart
        );
    }

    #[test]
    fn detects_state_diagram_keyword() {
        assert_eq!(
            detect("stateDiagram\n[*] --> A").unwrap(),
            DiagramKind::State
        );
    }

    #[test]
    fn detects_state_diagram_v2_keyword() {
        assert_eq!(
            detect("stateDiagram-v2\n[*] --> A").unwrap(),
            DiagramKind::State
        );
    }

    #[test]
    fn state_diagram_keyword_is_case_insensitive() {
        assert_eq!(
            detect("StateDiagram-V2\n[*] --> A").unwrap(),
            DiagramKind::State
        );
    }

    #[test]
    fn detects_class_diagram_keyword() {
        assert_eq!(
            detect("classDiagram\nclass Animal").unwrap(),
            DiagramKind::Class
        );
        // Case-insensitive.
        assert_eq!(detect("ClassDiagram").unwrap(), DiagramKind::Class);
    }

    #[test]
    fn detects_journey_keyword() {
        assert_eq!(
            detect("journey\ntitle My Day").unwrap(),
            DiagramKind::Journey
        );
        // Case-insensitive.
        assert_eq!(detect("Journey").unwrap(), DiagramKind::Journey);
    }

    #[test]
    fn detects_timeline_keyword() {
        assert_eq!(
            detect("timeline\n2002 : LinkedIn").unwrap(),
            DiagramKind::Timeline
        );
        // Case-insensitive.
        assert_eq!(detect("Timeline").unwrap(), DiagramKind::Timeline);
    }

    #[test]
    fn detects_git_graph_keyword() {
        assert_eq!(detect("gitGraph\ncommit").unwrap(), DiagramKind::GitGraph);
        // Case-insensitive match: "gitgraph" lowercased.
        assert_eq!(detect("GitGraph").unwrap(), DiagramKind::GitGraph);
    }

    #[test]
    fn detects_mindmap_keyword() {
        assert_eq!(detect("mindmap\n  root").unwrap(), DiagramKind::Mindmap);
        // Case-insensitive.
        assert_eq!(detect("Mindmap").unwrap(), DiagramKind::Mindmap);
    }

    #[test]
    fn detects_quadrant_chart_keyword() {
        assert_eq!(
            detect("quadrantChart\nA: [0.5, 0.5]").unwrap(),
            DiagramKind::QuadrantChart
        );
        // Case-insensitive.
        assert_eq!(detect("QuadrantChart").unwrap(), DiagramKind::QuadrantChart);
    }

    #[test]
    fn detects_requirement_diagram_keyword() {
        assert_eq!(
            detect("requirementDiagram\n").unwrap(),
            DiagramKind::RequirementDiagram
        );
        // Case-insensitive.
        assert_eq!(
            detect("RequirementDiagram").unwrap(),
            DiagramKind::RequirementDiagram
        );
    }

    #[test]
    fn detects_sankey_beta_keyword() {
        assert_eq!(detect("sankey-beta\nA,B,1.0").unwrap(), DiagramKind::Sankey);
        // Relaxed alias.
        assert_eq!(detect("sankey\nA,B,1.0").unwrap(), DiagramKind::Sankey);
    }

    #[test]
    fn detects_sankey_keyword_case_insensitive() {
        // The keyword contains a hyphen; case-insensitive matching is via
        // `to_lowercase()` on the split token, so test both.
        assert_eq!(detect("Sankey-Beta\nA,B,1.0").unwrap(), DiagramKind::Sankey);
        assert_eq!(detect("Sankey\nA,B,1.0").unwrap(), DiagramKind::Sankey);
    }

    #[test]
    fn detects_xychart_beta_keyword() {
        assert_eq!(
            detect("xychart-beta\nbar [1,2,3]").unwrap(),
            DiagramKind::XyChart
        );
        // Alias without `-beta`.
        assert_eq!(detect("xychart\nbar [1]").unwrap(), DiagramKind::XyChart);
    }

    #[test]
    fn detects_xychart_keyword_case_insensitive() {
        // Case-insensitive matching via `to_lowercase()` on the split token.
        assert_eq!(
            detect("XyChart-Beta\nbar [1]").unwrap(),
            DiagramKind::XyChart
        );
        assert_eq!(detect("XYCHART\nbar [1]").unwrap(), DiagramKind::XyChart);
    }

    #[test]
    fn detects_block_beta_keyword() {
        assert_eq!(
            detect("block-beta\n    A B").unwrap(),
            DiagramKind::BlockDiagram
        );
        // Relaxed alias without `-beta`.
        assert_eq!(detect("block\n    A B").unwrap(), DiagramKind::BlockDiagram);
    }

    #[test]
    fn detects_block_beta_keyword_case_insensitive() {
        assert_eq!(
            detect("Block-Beta\n    A").unwrap(),
            DiagramKind::BlockDiagram
        );
        assert_eq!(detect("BLOCK\n    A").unwrap(), DiagramKind::BlockDiagram);
    }

    #[test]
    fn detects_architecture_beta_keyword() {
        assert_eq!(
            detect("architecture-beta\n    service s(server)[S]").unwrap(),
            DiagramKind::Architecture
        );
        // Relaxed alias.
        assert_eq!(
            detect("architecture\n    service s(server)[S]").unwrap(),
            DiagramKind::Architecture
        );
    }

    #[test]
    fn detects_architecture_beta_keyword_case_insensitive() {
        assert_eq!(
            detect("Architecture-Beta\n    service s(server)[S]").unwrap(),
            DiagramKind::Architecture
        );
        assert_eq!(
            detect("ARCHITECTURE\n    service s(server)[S]").unwrap(),
            DiagramKind::Architecture
        );
    }

    #[test]
    fn detects_packet_beta_keyword() {
        assert_eq!(
            detect("packet-beta\n    0-15: \"Src\"").unwrap(),
            DiagramKind::Packet
        );
        // Relaxed alias without `-beta`.
        assert_eq!(
            detect("packet\n    0-15: \"Src\"").unwrap(),
            DiagramKind::Packet
        );
    }

    #[test]
    fn detects_packet_beta_keyword_case_insensitive() {
        assert_eq!(
            detect("Packet-Beta\n    0-15: \"Src\"").unwrap(),
            DiagramKind::Packet
        );
        assert_eq!(
            detect("PACKET\n    0-15: \"Src\"").unwrap(),
            DiagramKind::Packet
        );
    }
}