flowmaid 0.4.0

Mermaid-like diagram engine in pure std Rust (flowcharts + ER diagrams): hand-written parser, Sugiyama-style layout, SVG renderer, and an interactive scene API for drag-and-drop apps. Zero dependencies.
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
//! Hand-written parser for Mermaid-like diagram syntax.
//!
//! Flowchart support:
//! - Header:  `flowchart TD|TB|LR|RL|BT`  or  `graph ...`
//! - Nodes:   `A`, `A[text]`, `A(text)`, `A([text])`, `A{text}`, `A((text))`
//! - Edges:   `-->`, `---`, `-.->`, `==>`, with labels `-->|text|`
//! - Chains:  `A --> B --> C`, `;` separator, `%%` comments
//!
//! Entity-Relationship support (`erDiagram` header):
//! - Relationships: `A ||--o{ B : "label"` with the full crow's foot
//!   cardinality tokens (`||`, `|o`/`o|`, `}o`/`o{`, `}|`/`|{`) and
//!   identifying (`--`) / non-identifying (`..`) lines
//! - Entity blocks: `Name { type name [PK|FK|UK] ["comment"] }`
//!
//! Use [`parse_document`] to accept any supported diagram type;
//! [`parse`] stays flowchart-only for backwards compatibility.

use crate::model::{
    Attr, Card, Direction, Document, EdgeKind, ErDiagram, Graph, Key, NodeStyle, Relation, Shape,
};
use std::collections::HashMap;

#[derive(Debug)]
pub struct ParseError {
    pub line: usize,
    pub message: String,
}

impl std::fmt::Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "line {}: {}", self.line, self.message)
    }
}

fn err(line: usize, message: String) -> ParseError {
    ParseError { line, message }
}

/// Simple character cursor over a single line.
struct Cur<'a> {
    s: &'a str,
    pos: usize,
}

impl<'a> Cur<'a> {
    fn new(s: &'a str) -> Self {
        Cur { s, pos: 0 }
    }
    fn rest(&self) -> &'a str {
        &self.s[self.pos..]
    }
    fn at_end(&self) -> bool {
        self.pos >= self.s.len()
    }
    fn peek(&self) -> Option<char> {
        self.rest().chars().next()
    }
    fn bump(&mut self) -> Option<char> {
        let c = self.peek()?;
        self.pos += c.len_utf8();
        Some(c)
    }
    fn skip_ws(&mut self) {
        while let Some(c) = self.peek() {
            if c.is_whitespace() {
                self.bump();
            } else {
                break;
            }
        }
    }
    fn eat(&mut self, prefix: &str) -> bool {
        if self.rest().starts_with(prefix) {
            self.pos += prefix.len();
            true
        } else {
            false
        }
    }
    /// Take text up to `close` (exclusive), then skip past `close`.
    fn take_until(&mut self, close: &str) -> Option<String> {
        let idx = self.rest().find(close)?;
        let out = self.rest()[..idx].to_string();
        self.pos += idx + close.len();
        Some(out)
    }
}

/// Strip wrapping double quotes from a label, Mermaid-style: `A["odd text"]`.
fn clean_label(s: &str) -> String {
    let t = s.trim();
    if t.len() >= 2 && t.starts_with('"') && t.ends_with('"') {
        t[1..t.len() - 1].to_string()
    } else {
        t.to_string()
    }
}

/// Parse any supported Mermaid diagram type, dispatching on the
/// header line: `flowchart`/`graph` (or none) -> flowchart,
/// `erDiagram` -> ER. Other known Mermaid types produce an explicit
/// "not supported yet" error.
pub fn parse_document(source: &str) -> Result<Document, ParseError> {
    for (i, raw) in source.lines().enumerate() {
        let line = raw.trim();
        if line.is_empty() || line.starts_with("%%") {
            continue;
        }
        return match diagram_type(line) {
            Some("erDiagram") => parse_er(source, i + 1).map(Document::Er),
            Some(t) => Err(err(
                i + 1,
                format!(
                    "diagram type '{}' is not supported yet (supported: flowchart, graph, erDiagram)",
                    t
                ),
            )),
            None => parse(source).map(Document::Flowchart),
        };
    }
    Ok(Document::Flowchart(Graph::default()))
}

/// Parse a flowchart. For ER diagrams use [`parse_document`].
pub fn parse(source: &str) -> Result<Graph, ParseError> {
    let mut g = Graph::default();
    let mut header_seen = false;
    // Styling is collected during the pass and resolved at the end,
    // because Mermaid allows `classDef` to appear after its usage.
    let mut class_defs: HashMap<String, NodeStyle> = HashMap::new();
    let mut assigns: Vec<(usize, String)> = Vec::new(); // (node, class)
    let mut styles: Vec<(usize, NodeStyle)> = Vec::new(); // explicit `style` lines

    for (i, raw) in source.lines().enumerate() {
        let lineno = i + 1;
        let line = raw.trim();
        if line.is_empty() || line.starts_with("%%") {
            continue;
        }

        if !header_seen {
            header_seen = true;
            if let Some(t) = diagram_type(line) {
                let hint = if t == "erDiagram" {
                    "this parser is flowchart-only — use parse_document() or render_svg()"
                } else {
                    "not supported yet (supported: flowchart, graph, erDiagram)"
                };
                return Err(err(lineno, format!("diagram type '{}': {}", t, hint)));
            }
            let rest = strip_keyword(line, "flowchart").or_else(|| strip_keyword(line, "graph"));
            if let Some(rest) = rest {
                g.direction = match rest.trim().to_uppercase().as_str() {
                    "" | "TD" | "TB" => Direction::TD,
                    "LR" => Direction::LR,
                    "RL" => Direction::RL,
                    "BT" => Direction::BT,
                    other => {
                        return Err(err(lineno, format!("unknown direction: '{}'", other)))
                    }
                };
                continue;
            }
            // No header: use the default direction (TD) and
            // treat this line as a regular statement.
        }

        // Styling statements (`style A fill:...`, `classDef name ...`,
        // `class A,B name`). strip_keyword requires whitespace after
        // the keyword, so `class` can't shadow `classDef`.
        if let Some(rest) = strip_keyword(line, "classDef") {
            let rest = rest.trim();
            let (names, props) = rest.split_once(char::is_whitespace).ok_or_else(|| {
                err(lineno, "classDef needs a name and properties".to_string())
            })?;
            let st = parse_props(props.trim(), lineno)?;
            for name in names.split(',').filter(|n| !n.is_empty()) {
                class_defs.insert(name.to_string(), st.clone());
            }
            continue;
        }
        if let Some(rest) = strip_keyword(line, "class") {
            let rest = rest.trim();
            let (ids, name) = rest.split_once(char::is_whitespace).ok_or_else(|| {
                err(lineno, "class needs node ids and a class name".to_string())
            })?;
            for id in ids.split(',').filter(|i| !i.is_empty()) {
                let n = g.ensure_node(id.trim(), None, None);
                assigns.push((n, name.trim().to_string()));
            }
            continue;
        }
        if let Some(rest) = strip_keyword(line, "style") {
            let rest = rest.trim();
            let (id, props) = rest.split_once(char::is_whitespace).ok_or_else(|| {
                err(lineno, "style needs a node id and properties".to_string())
            })?;
            let n = g.ensure_node(id.trim(), None, None);
            styles.push((n, parse_props(props.trim(), lineno)?));
            continue;
        }

        parse_statement(&mut g, line, lineno, &mut assigns)?;
    }

    // Resolve styling: classDef layers first, explicit `style`
    // lines win. Unknown class names are ignored, mermaid-style.
    for (n, name) in assigns {
        if let Some(def) = class_defs.get(&name) {
            g.nodes[n].style.apply_over(def);
        }
    }
    for (n, st) in styles {
        g.nodes[n].style.apply_over(&st);
    }
    Ok(g)
}

/// Parse `fill:#f9f,stroke:#333,stroke-width:4px,color:#fff`.
/// Unknown properties are ignored (mermaid tolerates them too).
fn parse_props(s: &str, lineno: usize) -> Result<NodeStyle, ParseError> {
    let mut st = NodeStyle::default();
    for item in s.split(',') {
        let item = item.trim();
        if item.is_empty() {
            continue;
        }
        let Some((k, v)) = item.split_once(':') else {
            return Err(err(
                lineno,
                format!("expected 'property:value', got '{}'", item),
            ));
        };
        let v = v.trim();
        match k.trim() {
            "fill" => st.fill = Some(v.to_string()),
            "stroke" => st.stroke = Some(v.to_string()),
            "color" => st.color = Some(v.to_string()),
            "stroke-width" => {
                let n: f64 = v.trim_end_matches("px").trim().parse().map_err(|_| {
                    err(lineno, format!("invalid stroke-width: '{}'", v))
                })?;
                st.stroke_width = Some(n);
            }
            _ => {}
        }
    }
    Ok(st)
}

/// Recognise a known Mermaid diagram-type header other than
/// flowchart/graph, so we can fail with a clear message instead of
/// parsing the header as a node. Longer tokens first
/// (`stateDiagram-v2` before `stateDiagram`).
fn diagram_type(line: &str) -> Option<&'static str> {
    const TYPES: &[&str] = &[
        "erDiagram",
        "sequenceDiagram",
        "classDiagram",
        "stateDiagram-v2",
        "stateDiagram",
        "gantt",
        "pie",
        "journey",
        "mindmap",
        "timeline",
    ];
    TYPES.iter().copied().find(|t| {
        line.get(..t.len()) == Some(*t)
            && line[t.len()..].chars().next().map_or(true, char::is_whitespace)
    })
}

/// Match a header keyword only when followed by whitespace or end of
/// line, so a node named e.g. `graphics` is not mistaken for a
/// `graph` header. Uses `get` to stay safe on UTF-8 boundaries.
fn strip_keyword<'a>(line: &'a str, kw: &str) -> Option<&'a str> {
    match line.get(..kw.len()) {
        Some(head) if head.eq_ignore_ascii_case(kw) => {
            let rest = &line[kw.len()..];
            if rest.is_empty() || rest.starts_with(char::is_whitespace) {
                Some(rest)
            } else {
                None
            }
        }
        _ => None,
    }
}

fn parse_statement(
    g: &mut Graph,
    line: &str,
    lineno: usize,
    assigns: &mut Vec<(usize, String)>,
) -> Result<(), ParseError> {
    let mut cur = Cur::new(line);
    let mut prev = parse_node(&mut cur, g, lineno, assigns)?;
    loop {
        cur.skip_ws();
        if cur.at_end() {
            break;
        }
        // `;` separates statements on one line: `A-->B; C-->D`
        if cur.eat(";") {
            cur.skip_ws();
            if cur.at_end() {
                break;
            }
            prev = parse_node(&mut cur, g, lineno, assigns)?;
            continue;
        }
        let kind = parse_edge_op(&mut cur).ok_or_else(|| {
            err(
                lineno,
                format!("unknown edge operator near: '{}'", cur.rest()),
            )
        })?;
        cur.skip_ws();
        let label = if cur.eat("|") {
            let l = cur.take_until("|").ok_or_else(|| {
                err(lineno, "edge label opened with '|' but never closed".to_string())
            })?;
            Some(clean_label(&l))
        } else {
            None
        };
        let next = parse_node(&mut cur, g, lineno, assigns)?;
        g.add_edge(prev, next, label, kind);
        prev = next;
    }
    Ok(())
}

fn parse_node(
    cur: &mut Cur<'_>,
    g: &mut Graph,
    lineno: usize,
    assigns: &mut Vec<(usize, String)>,
) -> Result<usize, ParseError> {
    cur.skip_ws();
    let start = cur.pos;
    while let Some(c) = cur.peek() {
        if c.is_alphanumeric() || c == '_' {
            cur.bump();
        } else {
            break;
        }
    }
    if cur.pos == start {
        return Err(err(
            lineno,
            format!("expected a node id, found: '{}'", cur.rest()),
        ));
    }
    let id = cur.s[start..cur.pos].to_string();

    // Check order matters: two-character openers first.
    let parsed: Option<(Shape, String)> = if cur.eat("((") {
        Some((Shape::Circle, close(cur, "))", lineno)?))
    } else if cur.eat("([") {
        Some((Shape::Stadium, close(cur, "])", lineno)?))
    } else if cur.eat("[") {
        Some((Shape::Rect, close(cur, "]", lineno)?))
    } else if cur.eat("(") {
        Some((Shape::Rounded, close(cur, ")", lineno)?))
    } else if cur.eat("{") {
        Some((Shape::Diamond, close(cur, "}", lineno)?))
    } else {
        None
    };

    let (shape, label) = match parsed {
        Some((s, l)) => (Some(s), Some(clean_label(&l))),
        None => (None, None),
    };
    let n = g.ensure_node(&id, label, shape);

    // `A:::className` — inline class assignment shorthand.
    if cur.eat(":::") {
        let start = cur.pos;
        while let Some(c) = cur.peek() {
            if c.is_alphanumeric() || c == '_' || c == '-' {
                cur.bump();
            } else {
                break;
            }
        }
        if cur.pos == start {
            return Err(err(lineno, "expected a class name after ':::'".to_string()));
        }
        assigns.push((n, cur.s[start..cur.pos].to_string()));
    }
    Ok(n)
}

fn close(cur: &mut Cur<'_>, closer: &str, lineno: usize) -> Result<String, ParseError> {
    // Quoted label: `A["odd [text]"]` — quotes protect bracket characters.
    if cur.rest().starts_with('"') {
        cur.bump();
        let inner = cur
            .take_until("\"")
            .ok_or_else(|| err(lineno, "unclosed label quote".to_string()))?;
        cur.skip_ws();
        if !cur.eat(closer) {
            return Err(err(lineno, format!("closing '{}' not found", closer)));
        }
        return Ok(inner);
    }
    cur.take_until(closer)
        .ok_or_else(|| err(lineno, format!("closing '{}' not found", closer)))
}

/// Recognise an edge operator and advance the cursor. Tolerant of
/// extra length (`--->`, `-..->`, `===>`).
fn parse_edge_op(cur: &mut Cur<'_>) -> Option<EdgeKind> {
    let rest = cur.rest();

    // Dotted: -.->  or  -..->
    if rest.starts_with("-.") {
        let after = &rest[2..];
        let dots = after.chars().take_while(|&c| c == '.').count();
        let tail = &after[dots..];
        if tail.starts_with("->") {
            cur.pos += 2 + dots + 2;
            return Some(EdgeKind::Dotted);
        }
        return None;
    }

    // Thick: ==>  or  ===>
    if rest.starts_with("==") {
        let eqs = rest.chars().take_while(|&c| c == '=').count();
        let tail = &rest[eqs..];
        if tail.starts_with('>') {
            cur.pos += eqs + 1;
            return Some(EdgeKind::Thick);
        }
        return None;
    }

    // Regular arrow (-->) or plain line (---)
    if rest.starts_with('-') {
        let dashes = rest.chars().take_while(|&c| c == '-').count();
        let tail = &rest[dashes..];
        if dashes >= 2 && tail.starts_with('>') {
            cur.pos += dashes + 1;
            return Some(EdgeKind::Arrow);
        }
        if dashes >= 3 {
            cur.pos += dashes;
            return Some(EdgeKind::Open);
        }
    }
    None
}

// ---------------------------------------------------------------
// Entity-Relationship (`erDiagram`)
// ---------------------------------------------------------------

/// Parse the body of an `erDiagram`. `header_line` is the
/// 1-indexed line of the `erDiagram` header; everything before and
/// including it is skipped.
fn parse_er(source: &str, header_line: usize) -> Result<ErDiagram, ParseError> {
    let mut d = ErDiagram::default();
    // Entity currently being filled, when inside a `{ ... }` block.
    let mut open: Option<(usize, usize)> = None; // (entity, opening line)
    for (i, raw) in source.lines().enumerate() {
        let lineno = i + 1;
        if lineno <= header_line {
            continue;
        }
        let line = raw.trim();
        if line.is_empty() || line.starts_with("%%") {
            continue;
        }

        if let Some((ent, _)) = open {
            if line == "}" {
                open = None;
                continue;
            }
            let attr = parse_attr(line, lineno)?;
            d.entities[ent].attrs.push(attr);
            continue;
        }

        if line == "}" {
            return Err(err(lineno, "'}' without an open entity block".to_string()));
        }
        // Entity block start: `Name {`
        if let Some(head) = line.strip_suffix('{') {
            let name = head.trim();
            if !name.is_empty() && name.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '-') {
                open = Some((d.ensure_entity(name), lineno));
                continue;
            }
            return Err(err(lineno, format!("invalid entity name before '{{': '{}'", name)));
        }
        parse_er_statement(&mut d, line, lineno)?;
    }
    if let Some((ent, opened)) = open {
        return Err(err(
            opened,
            format!(
                "entity block '{}' is never closed with '}}'",
                d.entities[ent].name
            ),
        ));
    }
    Ok(d)
}

/// One top-level ER line: either a bare entity declaration or a
/// relationship `A <card><line><card> B : "label"`.
fn parse_er_statement(d: &mut ErDiagram, line: &str, lineno: usize) -> Result<(), ParseError> {
    let mut cur = Cur::new(line);
    let a = parse_er_name(&mut cur, lineno)?;
    cur.skip_ws();
    if cur.at_end() {
        d.ensure_entity(&a);
        return Ok(());
    }
    let (card_from, identifying, card_to) = parse_rel_op(&mut cur).ok_or_else(|| {
        err(
            lineno,
            format!("unknown relationship operator near: '{}'", cur.rest()),
        )
    })?;
    cur.skip_ws();
    let b = parse_er_name(&mut cur, lineno)?;
    cur.skip_ws();
    let label = if cur.eat(":") {
        let l = cur.rest().trim();
        if l.is_empty() {
            None
        } else {
            Some(clean_label(l))
        }
    } else if cur.at_end() {
        None
    } else {
        return Err(err(
            lineno,
            format!("unexpected text after relationship: '{}'", cur.rest()),
        ));
    };
    let from = d.ensure_entity(&a);
    let to = d.ensure_entity(&b);
    d.relations.push(Relation {
        from,
        to,
        card_from,
        card_to,
        identifying,
        label,
    });
    Ok(())
}

fn parse_er_name(cur: &mut Cur<'_>, lineno: usize) -> Result<String, ParseError> {
    cur.skip_ws();
    let start = cur.pos;
    while let Some(c) = cur.peek() {
        if c.is_alphanumeric() || c == '_' || c == '-' {
            cur.bump();
        } else {
            break;
        }
    }
    if cur.pos == start {
        return Err(err(
            lineno,
            format!("expected an entity name, found: '{}'", cur.rest()),
        ));
    }
    Ok(cur.s[start..cur.pos].to_string())
}

/// Crow's foot relationship operator, e.g. `||--o{` or `}o..o|`.
/// The token adjacent to each entity is that entity's cardinality.
fn parse_rel_op(cur: &mut Cur<'_>) -> Option<(Card, bool, Card)> {
    const LEFT: &[(&str, Card)] = &[
        ("||", Card::One),
        ("|o", Card::ZeroOne),
        ("}o", Card::ZeroMany),
        ("}|", Card::OneMany),
    ];
    const RIGHT: &[(&str, Card)] = &[
        ("||", Card::One),
        ("o|", Card::ZeroOne),
        ("o{", Card::ZeroMany),
        ("|{", Card::OneMany),
    ];
    let rest = cur.rest();
    let (lt, lc) = LEFT.iter().find(|(t, _)| rest.starts_with(t))?;
    let after_left = &rest[lt.len()..];
    let identifying = if after_left.starts_with("--") {
        true
    } else if after_left.starts_with("..") {
        false
    } else {
        return None;
    };
    let after_line = &after_left[2..];
    let (rt, rc) = RIGHT.iter().find(|(t, _)| after_line.starts_with(t))?;
    cur.pos += lt.len() + 2 + rt.len();
    Some((*lc, identifying, *rc))
}

/// One attribute row: `type name [PK|FK|UK]... ["comment"]`.
/// The comment runs from the first `"` to the last `"`, so it may
/// freely contain commas, parentheses, and single quotes.
fn parse_attr(line: &str, lineno: usize) -> Result<Attr, ParseError> {
    let (head, comment) = match line.find('"') {
        Some(q0) => {
            let q1 = line.rfind('"').unwrap();
            if q1 <= q0 {
                return Err(err(lineno, "unclosed attribute comment quote".to_string()));
            }
            (line[..q0].trim_end(), Some(line[q0 + 1..q1].to_string()))
        }
        None => (line, None),
    };
    let mut toks = head.split_whitespace();
    let ty = toks
        .next()
        .ok_or_else(|| err(lineno, "expected an attribute type".to_string()))?
        .to_string();
    let name = toks
        .next()
        .ok_or_else(|| err(lineno, format!("expected an attribute name after type '{}'", ty)))?
        .to_string();
    let mut keys = Vec::new();
    for t in toks {
        match t.trim_end_matches(',') {
            "PK" => keys.push(Key::Pk),
            "FK" => keys.push(Key::Fk),
            "UK" => keys.push(Key::Uk),
            other => {
                return Err(err(
                    lineno,
                    format!("unknown attribute key: '{}' (expected PK, FK, or UK)", other),
                ))
            }
        }
    }
    Ok(Attr {
        ty,
        name,
        keys,
        comment,
    })
}

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

    #[test]
    fn basic_parse() {
        let g = parse("flowchart TD\nA[Start] --> B{Check?}\nB -->|yes| C((Done))\n").unwrap();
        assert_eq!(g.nodes.len(), 3);
        assert_eq!(g.edges.len(), 2);
        assert_eq!(g.nodes[1].shape, Shape::Diamond);
        assert_eq!(g.nodes[2].shape, Shape::Circle);
        assert_eq!(g.edges[1].label.as_deref(), Some("yes"));
    }

    #[test]
    fn chains_and_semicolons() {
        let g = parse("graph LR\nA --> B --> C; D -.-> A\n").unwrap();
        assert_eq!(g.nodes.len(), 4);
        assert_eq!(g.edges.len(), 3);
        assert_eq!(g.edges[2].kind, EdgeKind::Dotted);
    }

    #[test]
    fn quoted_labels() {
        let g = parse("A[\"odd [text]?\"] --> B").unwrap();
        assert_eq!(g.nodes.len(), 2);
        assert_eq!(g.nodes[0].label, "odd [text]?");
    }

    #[test]
    fn errors_carry_line_numbers() {
        let e = parse("flowchart TD\nA --> \n").unwrap_err();
        assert_eq!(e.line, 2);
    }

    #[test]
    fn unsupported_diagram_types_get_explicit_errors() {
        for src in ["sequenceDiagram\nA->>B: hi", "gantt\ntitle x", "stateDiagram-v2\n[*] --> A"] {
            for res in [
                parse(src).map(|_| ()),
                parse_document(src).map(|_| ()),
            ] {
                let e = res.unwrap_err();
                assert_eq!(e.line, 1);
                assert!(
                    e.message.contains("not supported yet"),
                    "message should say the type is unsupported: {}",
                    e.message
                );
            }
        }
        // The flowchart-only `parse` refuses erDiagram with a pointer
        // to the right entry point.
        let e = parse("erDiagram\nA ||--o{ B : has").unwrap_err();
        assert!(e.message.contains("parse_document"), "{}", e.message);
        // A node that merely starts with a type name is still a node.
        let g = parse("pies[Pie Chart] --> B").unwrap();
        assert_eq!(g.nodes[0].id, "pies");
    }

    // ----------------------------- ER -----------------------------

    fn er(src: &str) -> ErDiagram {
        match parse_document(src).unwrap() {
            Document::Er(d) => d,
            other => panic!("expected an ER document, got {:?}", other),
        }
    }

    #[test]
    fn er_fixture_parses_with_expected_counts() {
        let d = er(include_str!("../examples/er.mmd"));
        let counts: Vec<(&str, usize)> = d
            .entities
            .iter()
            .map(|e| (e.name.as_str(), e.attrs.len()))
            .collect();
        assert_eq!(
            counts,
            [
                ("categories", 9),
                ("questions", 11),
                ("schedules", 13),
                ("settings", 9)
            ]
        );
        assert_eq!(d.relations.len(), 1);
        let r = &d.relations[0];
        assert_eq!(d.entities[r.from].name, "categories");
        assert_eq!(d.entities[r.to].name, "questions");
        assert_eq!(r.card_from, Card::One);
        assert_eq!(r.card_to, Card::ZeroMany);
        assert!(r.identifying);
        assert_eq!(r.label.as_deref(), Some("has"));
    }

    #[test]
    fn er_type_tokens_with_parens_stay_whole() {
        let d = er("erDiagram\nT {\n  varchar(255) name \"not null\"\n  varchar(20) code\n}");
        assert_eq!(d.entities[0].attrs[0].ty, "varchar(255)");
        assert_eq!(d.entities[0].attrs[1].ty, "varchar(20)");
    }

    #[test]
    fn er_comment_survives_commas_parens_and_single_quotes() {
        let d = er("erDiagram\nT {\n  varchar(20) difficulty \"not null, default 'medium' (see docs)\"\n}");
        assert_eq!(
            d.entities[0].attrs[0].comment.as_deref(),
            Some("not null, default 'medium' (see docs)")
        );
    }

    #[test]
    fn er_keys_parse_and_relation_only_entities_exist() {
        let d = er("erDiagram\nA ||..|{ B\nA {\n  uuid id PK\n  uuid b_id FK\n}");
        assert_eq!(d.entities[0].attrs[0].keys, vec![Key::Pk]);
        assert_eq!(d.entities[0].attrs[1].keys, vec![Key::Fk]);
        // B exists only through the relationship: empty table.
        assert_eq!(d.entities[1].name, "B");
        assert!(d.entities[1].attrs.is_empty());
        assert!(!d.relations[0].identifying);
        assert_eq!(d.relations[0].card_to, Card::OneMany);
        assert_eq!(d.relations[0].label, None);
    }

    #[test]
    fn er_errors_have_line_numbers() {
        // Unclosed entity block reports the opening line.
        let e = parse_document("erDiagram\nA {\n  uuid id\n").unwrap_err();
        assert_eq!(e.line, 2);
        // Stray closing brace.
        let e = parse_document("erDiagram\n}\n").unwrap_err();
        assert_eq!(e.line, 2);
        // Bad relationship operator.
        let e = parse_document("erDiagram\nA >>-- B\n").unwrap_err();
        assert_eq!(e.line, 2);
    }

    #[test]
    fn style_classdef_and_triple_colon() {
        let g = parse(
            "flowchart TD\n\
             A[Server] --> B{Ok?}\n\
             B --> C:::hot\n\
             style A fill:#f9f,stroke:#333,stroke-width:4px\n\
             classDef hot fill:#ffe3e3,stroke:#e03131,color:#c92a2a\n\
             class B hot\n\
             style B stroke:#000\n",
        )
        .unwrap();
        // Explicit style line.
        assert_eq!(g.nodes[0].style.fill.as_deref(), Some("#f9f"));
        assert_eq!(g.nodes[0].style.stroke_width, Some(4.0));
        // classDef via `class`, then `style` overrides just the stroke.
        assert_eq!(g.nodes[1].style.fill.as_deref(), Some("#ffe3e3"));
        assert_eq!(g.nodes[1].style.stroke.as_deref(), Some("#000"));
        assert_eq!(g.nodes[1].style.color.as_deref(), Some("#c92a2a"));
        // ::: shorthand, with classDef declared later in the file.
        assert_eq!(g.nodes[2].style.fill.as_deref(), Some("#ffe3e3"));
        // Unknown property is ignored, bad property syntax errors.
        assert!(parse("A --> B\nstyle A rounded").is_err());
        assert!(parse("A --> B\nstyle A glow:heavy,fill:#fff").unwrap().nodes[0]
            .style
            .fill
            .is_some());
    }

    #[test]
    fn custom_fill_reaches_the_svg() {
        let svg = crate::render_svg("A[X] --> B\nstyle A fill:#123456,color:#ffffff").unwrap();
        assert!(svg.contains("fill=\"#123456\""));
        assert!(svg.contains("fill=\"#ffffff\">X</text>"));
    }

    #[test]
    fn keyword_like_id_is_not_a_header() {
        // Previously: "graphics" was mistaken for the "graph" header
        // plus direction "ics[...]" -> error.
        let g = parse("graphics[Graphics] --> B").unwrap();
        assert_eq!(g.nodes.len(), 2);
        assert_eq!(g.nodes[0].id, "graphics");
        assert_eq!(g.direction, crate::model::Direction::TD);
    }
}