mmdflux 2.3.0

Render Mermaid diagrams as Unicode text, ASCII, SVG, and MMDS JSON.
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
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
//! Shared style types and Mermaid style-statement parsing.

use std::error::Error;
use std::fmt;

use serde::{Serialize, Serializer};

/// Parsed CSS-like style properties for a diagram node (fill, stroke, color).
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct NodeStyle {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub fill: Option<ColorToken>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stroke: Option<ColorToken>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub color: Option<ColorToken>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub font_style: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub font_weight: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stroke_width: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stroke_dasharray: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub rx: Option<String>,
}

impl NodeStyle {
    pub fn is_empty(&self) -> bool {
        self.fill.is_none()
            && self.stroke.is_none()
            && self.color.is_none()
            && self.font_style.is_none()
            && self.font_weight.is_none()
            && self.stroke_width.is_none()
            && self.stroke_dasharray.is_none()
            && self.rx.is_none()
    }

    pub fn with_fill(mut self, fill: ColorToken) -> Self {
        self.fill = Some(fill);
        self
    }

    pub fn with_stroke(mut self, stroke: ColorToken) -> Self {
        self.stroke = Some(stroke);
        self
    }

    pub fn with_color(mut self, color: ColorToken) -> Self {
        self.color = Some(color);
        self
    }

    pub fn merge(&self, overlay: &Self) -> Self {
        Self {
            fill: overlay.fill.clone().or_else(|| self.fill.clone()),
            stroke: overlay.stroke.clone().or_else(|| self.stroke.clone()),
            color: overlay.color.clone().or_else(|| self.color.clone()),
            font_style: overlay
                .font_style
                .clone()
                .or_else(|| self.font_style.clone()),
            font_weight: overlay
                .font_weight
                .clone()
                .or_else(|| self.font_weight.clone()),
            stroke_width: overlay
                .stroke_width
                .clone()
                .or_else(|| self.stroke_width.clone()),
            stroke_dasharray: overlay
                .stroke_dasharray
                .clone()
                .or_else(|| self.stroke_dasharray.clone()),
            rx: overlay.rx.clone().or_else(|| self.rx.clone()),
        }
    }
}

/// Parsed CSS-like style properties for a diagram edge.
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize)]
pub struct EdgeStyle {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stroke: Option<ColorToken>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stroke_width: Option<String>,
}

impl EdgeStyle {
    pub fn is_empty(&self) -> bool {
        self.stroke.is_none() && self.stroke_width.is_none()
    }

    pub fn merge(&self, overlay: &Self) -> Self {
        Self {
            stroke: overlay.stroke.clone().or_else(|| self.stroke.clone()),
            stroke_width: overlay
                .stroke_width
                .clone()
                .or_else(|| self.stroke_width.clone()),
        }
    }
}

/// A CSS color value (hex, named, rgb, etc.) parsed from a Mermaid style statement.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColorToken {
    raw: String,
    rgb: Option<(u8, u8, u8)>,
}

impl ColorToken {
    pub fn parse(raw: &str) -> Result<Self, ColorTokenParseError> {
        let raw = raw.trim();
        if raw.is_empty() {
            return Err(ColorTokenParseError::Empty);
        }

        Ok(Self {
            raw: raw.to_string(),
            rgb: parse_hex_color(raw).or_else(|| named_color_rgb(raw)),
        })
    }

    pub fn raw(&self) -> &str {
        &self.raw
    }

    pub fn to_rgb(&self) -> Option<(u8, u8, u8)> {
        self.rgb
    }
}

impl Serialize for ColorToken {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(&self.raw)
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ColorTokenParseError {
    Empty,
}

impl fmt::Display for ColorTokenParseError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ColorTokenParseError::Empty => write!(f, "color token cannot be empty"),
        }
    }
}

impl Error for ColorTokenParseError {}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedNodeStyleDeclaration {
    pub style: NodeStyle,
    pub issues: Vec<NodeStyleIssue>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedEdgeStyleDeclaration {
    pub style: EdgeStyle,
    pub issues: Vec<EdgeStyleIssue>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedNodeStyleDirective {
    pub node_id: String,
    pub style: NodeStyle,
    pub issues: Vec<NodeStyleIssue>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum NodeStyleIssue {
    UnsupportedProperty { property: String },
    UnsupportedColorSyntax { property: String, value: String },
    MalformedDeclaration { declaration: String },
}

impl NodeStyleIssue {
    pub fn message(&self) -> String {
        match self {
            NodeStyleIssue::UnsupportedProperty { property } => format!(
                "style property '{}' is not supported; supported properties are fill, stroke, and color",
                property
            ),
            NodeStyleIssue::UnsupportedColorSyntax { property, value } => format!(
                "style property '{}' uses unsupported color syntax '{}'; supported color formats are #rgb, #rrggbb, and named colors",
                property, value
            ),
            NodeStyleIssue::MalformedDeclaration { declaration } => format!(
                "style declaration '{}' must use key:value syntax",
                declaration
            ),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum EdgeStyleIssue {
    UnsupportedProperty { property: String },
    UnsupportedColorSyntax { property: String, value: String },
    MalformedDeclaration { declaration: String },
    InvalidLinkIndex { token: String },
}

impl EdgeStyleIssue {
    pub fn message(&self) -> String {
        match self {
            EdgeStyleIssue::UnsupportedProperty { property } => format!(
                "linkStyle property '{}' is not supported; supported properties are stroke and stroke-width",
                property
            ),
            EdgeStyleIssue::UnsupportedColorSyntax { property, value } => format!(
                "linkStyle property '{}' uses unsupported color syntax '{}'; supported color formats are #rgb, #rrggbb, and named colors",
                property, value
            ),
            EdgeStyleIssue::MalformedDeclaration { declaration } => format!(
                "linkStyle declaration '{}' must use key:value syntax",
                declaration
            ),
            EdgeStyleIssue::InvalidLinkIndex { token } => format!(
                "linkStyle target '{}' is not a valid index; expected a non-negative integer or 'default'",
                token
            ),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LinkStyleTarget {
    Default,
    Indices(Vec<usize>),
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ParsedLinkStyleDirective {
    pub target: LinkStyleTarget,
    pub style: EdgeStyle,
    pub issues: Vec<EdgeStyleIssue>,
}

pub fn parse_node_style_statement(raw: &str) -> Option<ParsedNodeStyleDirective> {
    let trimmed = raw.trim();
    let rest = strip_keyword(trimmed, "style")?.trim_start();
    if rest.is_empty() {
        return None;
    }

    let mut parts = rest.splitn(2, char::is_whitespace);
    let node_id = parts.next()?.trim();
    if node_id.is_empty() {
        return None;
    }

    let declarations = parts.next().unwrap_or("").trim();
    let parsed = parse_node_style_declarations(declarations);
    Some(ParsedNodeStyleDirective {
        node_id: node_id.to_string(),
        style: parsed.style,
        issues: parsed.issues,
    })
}

/// Reassemble comma-split fragments: if a fragment has no `:` it's part of the
/// previous value (e.g. `stroke-dasharray:5,3` → one declaration, not two).
fn reassemble_declarations(raw: &str) -> Vec<String> {
    let mut result: Vec<String> = Vec::new();
    for part in raw.split(',') {
        if part.contains(':') || result.is_empty() {
            result.push(part.to_string());
        } else {
            // Append to previous declaration's value.
            if let Some(last) = result.last_mut() {
                last.push(',');
                last.push_str(part);
            }
        }
    }
    result
}

pub(crate) fn parse_node_style_declarations(raw: &str) -> ParsedNodeStyleDeclaration {
    let mut style = NodeStyle::default();
    let mut issues = Vec::new();

    // Reassemble comma-separated fragments: fragments without `:` are part of
    // the previous declaration's value (e.g. `stroke-dasharray:5,3`).
    let declarations = reassemble_declarations(raw);

    for declaration in &declarations {
        let declaration = declaration.trim();
        if declaration.is_empty() {
            continue;
        }

        let Some((key, value)) = declaration.split_once(':') else {
            issues.push(NodeStyleIssue::MalformedDeclaration {
                declaration: declaration.to_string(),
            });
            continue;
        };

        let property = key.trim().to_ascii_lowercase();
        let value = value.trim();
        if value.is_empty() {
            issues.push(NodeStyleIssue::MalformedDeclaration {
                declaration: declaration.to_string(),
            });
            continue;
        }

        // String-valued properties (non-color).
        match property.as_str() {
            "font-style" => {
                style.font_style = Some(value.to_string());
                continue;
            }
            "font-weight" => {
                style.font_weight = Some(value.to_string());
                continue;
            }
            "stroke-width" => {
                style.stroke_width = Some(value.to_string());
                continue;
            }
            "stroke-dasharray" => {
                style.stroke_dasharray = Some(value.to_string());
                continue;
            }
            "rx" => {
                style.rx = Some(value.to_string());
                continue;
            }
            _ => {}
        }

        // Color-valued properties.
        let token = match ColorToken::parse(value) {
            Ok(token) => token,
            Err(_) => {
                issues.push(NodeStyleIssue::MalformedDeclaration {
                    declaration: declaration.to_string(),
                });
                continue;
            }
        };

        match property.as_str() {
            "fill" => {
                if token.to_rgb().is_none() {
                    issues.push(NodeStyleIssue::UnsupportedColorSyntax {
                        property: property.clone(),
                        value: token.raw().to_string(),
                    });
                }
                style.fill = Some(token);
            }
            "stroke" => {
                if token.to_rgb().is_none() {
                    issues.push(NodeStyleIssue::UnsupportedColorSyntax {
                        property: property.clone(),
                        value: token.raw().to_string(),
                    });
                }
                style.stroke = Some(token);
            }
            "color" => {
                if token.to_rgb().is_none() {
                    issues.push(NodeStyleIssue::UnsupportedColorSyntax {
                        property: property.clone(),
                        value: token.raw().to_string(),
                    });
                }
                style.color = Some(token);
            }
            _ => issues.push(NodeStyleIssue::UnsupportedProperty { property }),
        }
    }

    ParsedNodeStyleDeclaration { style, issues }
}

pub(crate) fn parse_edge_style_declarations(raw: &str) -> ParsedEdgeStyleDeclaration {
    let mut style = EdgeStyle::default();
    let mut issues = Vec::new();

    let declarations = reassemble_declarations(raw);

    for declaration in &declarations {
        let declaration = declaration.trim();
        if declaration.is_empty() {
            continue;
        }

        let Some((key, value)) = declaration.split_once(':') else {
            issues.push(EdgeStyleIssue::MalformedDeclaration {
                declaration: declaration.to_string(),
            });
            continue;
        };

        let property = key.trim().to_ascii_lowercase();
        let value = value.trim();
        if value.is_empty() {
            issues.push(EdgeStyleIssue::MalformedDeclaration {
                declaration: declaration.to_string(),
            });
            continue;
        }

        match property.as_str() {
            "stroke-width" => {
                style.stroke_width = Some(value.to_string());
            }
            "stroke" => match ColorToken::parse(value) {
                Ok(token) => {
                    if token.to_rgb().is_none() {
                        issues.push(EdgeStyleIssue::UnsupportedColorSyntax {
                            property: property.clone(),
                            value: token.raw().to_string(),
                        });
                    }
                    style.stroke = Some(token);
                }
                Err(_) => {
                    issues.push(EdgeStyleIssue::MalformedDeclaration {
                        declaration: declaration.to_string(),
                    });
                }
            },
            _ => issues.push(EdgeStyleIssue::UnsupportedProperty { property }),
        }
    }

    ParsedEdgeStyleDeclaration { style, issues }
}

pub fn parse_linkstyle_statement(raw: &str) -> Option<ParsedLinkStyleDirective> {
    let trimmed = raw.trim();
    let rest = strip_keyword(trimmed, "linkStyle")?.trim_start();
    if rest.is_empty() {
        return None;
    }

    let mut parts = rest.splitn(2, char::is_whitespace);
    let target_raw = parts.next()?.trim();
    if target_raw.is_empty() {
        return None;
    }

    let (target, mut target_issues) = parse_link_style_target(target_raw)?;
    let declarations = parts.next().unwrap_or("").trim();
    if declarations.is_empty() {
        return None;
    }
    let parsed = parse_edge_style_declarations(declarations);
    target_issues.extend(parsed.issues);

    Some(ParsedLinkStyleDirective {
        target,
        style: parsed.style,
        issues: target_issues,
    })
}

/// Parsed `classDef className fill:#f9f,stroke:#333` directive.
pub struct ParsedClassDefDirective {
    /// The class name.
    pub class_name: String,
    /// Resolved style properties.
    pub style: NodeStyle,
    /// Issues found during parsing (unsupported properties, etc.).
    pub issues: Vec<NodeStyleIssue>,
}

/// Parse a `classDef className fill:#f9f,stroke:#333` statement.
///
/// Supports multi-class syntax: `classDef a,b fill:#f9f` registers both `a` and `b`.
pub fn parse_classdef_statement(raw: &str) -> Option<ParsedClassDefDirective> {
    parse_classdef_statement_multi(raw).into_iter().next()
}

/// Parse a `classDef` statement, returning one directive per class name.
///
/// `classDef a,b fill:#f9f` returns two directives (for `a` and `b`) sharing
/// the same style and issues.
pub fn parse_classdef_statement_multi(raw: &str) -> Vec<ParsedClassDefDirective> {
    let trimmed = raw.trim();
    let Some(rest) = strip_keyword(trimmed, "classDef") else {
        return Vec::new();
    };
    let rest = rest.trim_start();
    if rest.is_empty() {
        return Vec::new();
    }

    let mut parts = rest.splitn(2, char::is_whitespace);
    let class_names_raw = match parts.next() {
        Some(s) if !s.trim().is_empty() => s.trim(),
        _ => return Vec::new(),
    };

    let declarations = parts.next().unwrap_or("").trim();
    let parsed = parse_node_style_declarations(declarations);

    class_names_raw
        .split(',')
        .filter(|name| !name.is_empty())
        .map(|name| ParsedClassDefDirective {
            class_name: name.trim().to_string(),
            style: parsed.style.clone(),
            issues: parsed.issues.clone(),
        })
        .collect()
}

/// Parsed `class nodeA,nodeB className` directive.
pub struct ParsedClassApplyDirective {
    /// Node IDs to apply the class to.
    pub node_ids: Vec<String>,
    /// The class name to apply.
    pub class_name: String,
}

/// Parse a `class nodeA,nodeB className` statement.
pub fn parse_class_apply_statement(raw: &str) -> Option<ParsedClassApplyDirective> {
    let trimmed = raw.trim();

    // Reject "classDef" lines (keyword prefix overlap).
    if strip_keyword(trimmed, "classDef").is_some() {
        return None;
    }

    let rest = strip_keyword(trimmed, "class")?.trim_start();
    if rest.is_empty() {
        return None;
    }

    // Last whitespace-delimited token is the class name.
    let last_space = rest.rfind(char::is_whitespace)?;
    let node_list = rest[..last_space].trim();
    let class_name = rest[last_space..].trim();
    if class_name.is_empty() || node_list.is_empty() {
        return None;
    }

    let node_ids: Vec<String> = node_list
        .split(',')
        .map(|s| s.trim().to_string())
        .filter(|s| !s.is_empty())
        .collect();
    if node_ids.is_empty() {
        return None;
    }

    Some(ParsedClassApplyDirective {
        node_ids,
        class_name: class_name.to_string(),
    })
}

fn strip_keyword<'a>(input: &'a str, keyword: &str) -> Option<&'a str> {
    if input.len() < keyword.len() || !input.is_char_boundary(keyword.len()) {
        return None;
    }

    let (prefix, rest) = input.split_at(keyword.len());
    if !prefix.eq_ignore_ascii_case(keyword) {
        return None;
    }

    if let Some(next) = rest.chars().next()
        && (next.is_alphanumeric() || next == '_')
    {
        return None;
    }

    Some(rest)
}

fn parse_link_style_target(raw: &str) -> Option<(LinkStyleTarget, Vec<EdgeStyleIssue>)> {
    if raw.eq_ignore_ascii_case("default") {
        return Some((LinkStyleTarget::Default, Vec::new()));
    }

    let mut indices = Vec::new();
    let mut issues = Vec::new();
    for part in raw.split(',') {
        let trimmed = part.trim();
        if trimmed.is_empty() {
            continue;
        }
        match trimmed.parse::<usize>() {
            Ok(idx) => indices.push(idx),
            Err(_) => issues.push(EdgeStyleIssue::InvalidLinkIndex {
                token: trimmed.to_string(),
            }),
        }
    }
    if indices.is_empty() {
        None
    } else {
        Some((LinkStyleTarget::Indices(indices), issues))
    }
}

fn parse_hex_color(raw: &str) -> Option<(u8, u8, u8)> {
    let hex = raw.strip_prefix('#')?;
    match hex.len() {
        3 => {
            let mut digits = hex.chars();
            let r = digits.next()?.to_digit(16)? as u8;
            let g = digits.next()?.to_digit(16)? as u8;
            let b = digits.next()?.to_digit(16)? as u8;
            Some((r * 17, g * 17, b * 17))
        }
        6 => Some((
            u8::from_str_radix(&hex[0..2], 16).ok()?,
            u8::from_str_radix(&hex[2..4], 16).ok()?,
            u8::from_str_radix(&hex[4..6], 16).ok()?,
        )),
        _ => None,
    }
}

fn named_color_rgb(raw: &str) -> Option<(u8, u8, u8)> {
    match raw.to_ascii_lowercase().as_str() {
        "black" => Some((0, 0, 0)),
        "white" => Some((255, 255, 255)),
        "red" => Some((255, 0, 0)),
        "green" => Some((0, 128, 0)),
        "blue" => Some((0, 0, 255)),
        "yellow" => Some((255, 255, 0)),
        "cyan" => Some((0, 255, 255)),
        "magenta" => Some((255, 0, 255)),
        "gray" | "grey" => Some((128, 128, 128)),
        "orange" => Some((255, 165, 0)),
        "purple" => Some((128, 0, 128)),
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::{
        ColorToken, LinkStyleTarget, NodeStyle, NodeStyleIssue, parse_class_apply_statement,
        parse_classdef_statement, parse_classdef_statement_multi, parse_linkstyle_statement,
        parse_node_style_declarations, parse_node_style_statement,
    };

    #[test]
    fn node_style_merge_is_property_level_last_write_wins() {
        let base = NodeStyle::default()
            .with_fill(ColorToken::parse("#ffeeaa").unwrap())
            .with_stroke(ColorToken::parse("#333").unwrap());
        let overlay = NodeStyle::default()
            .with_color(ColorToken::parse("#111").unwrap())
            .with_stroke(ColorToken::parse("#555").unwrap());

        let merged = base.merge(&overlay);

        assert_eq!(merged.fill.unwrap().raw(), "#ffeeaa");
        assert_eq!(merged.stroke.unwrap().raw(), "#555");
        assert_eq!(merged.color.unwrap().raw(), "#111");
    }

    #[test]
    fn color_token_parses_hex_and_named_colors_for_ansi_resolution() {
        let short_hex = ColorToken::parse("#abc").unwrap();
        let long_hex = ColorToken::parse("#aabbcc").unwrap();
        let named = ColorToken::parse("red").unwrap();

        assert_eq!(short_hex.to_rgb().unwrap(), (170, 187, 204));
        assert_eq!(long_hex.to_rgb().unwrap(), (170, 187, 204));
        assert_eq!(named.to_rgb().unwrap(), (255, 0, 0));
    }

    #[test]
    fn parse_node_style_statement_collects_supported_properties_and_issues() {
        let parsed =
            parse_node_style_statement("style A fill:#fff,stroke-width:4px,color:var(--accent)")
                .unwrap();

        assert_eq!(parsed.node_id, "A");
        assert_eq!(parsed.style.fill.as_ref().unwrap().raw(), "#fff");
        assert!(parsed.style.stroke.is_none());
        assert_eq!(parsed.style.color.as_ref().unwrap().raw(), "var(--accent)");
        assert_eq!(parsed.style.stroke_width.as_deref(), Some("4px"));
        assert!(
            !parsed
                .issues
                .contains(&NodeStyleIssue::UnsupportedProperty {
                    property: "stroke-width".to_string(),
                })
        );
        assert!(
            parsed
                .issues
                .contains(&NodeStyleIssue::UnsupportedColorSyntax {
                    property: "color".to_string(),
                    value: "var(--accent)".to_string(),
                })
        );
    }

    #[test]
    fn parse_node_style_declarations_is_last_write_wins_per_property() {
        let parsed = parse_node_style_declarations("fill:#aaa,stroke:#111,fill:#bbb");

        assert_eq!(parsed.style.fill.as_ref().unwrap().raw(), "#bbb");
        assert_eq!(parsed.style.stroke.as_ref().unwrap().raw(), "#111");
        assert!(parsed.issues.is_empty());
    }

    // --- classDef parsing ---

    #[test]
    fn parse_classdef_basic() {
        let result = parse_classdef_statement("classDef highlight fill:#ff0,stroke:#333");
        assert!(result.is_some());
        let parsed = result.unwrap();
        assert_eq!(parsed.class_name, "highlight");
        assert_eq!(parsed.style.fill.as_ref().unwrap().raw(), "#ff0");
        assert_eq!(parsed.style.stroke.as_ref().unwrap().raw(), "#333");
        assert!(parsed.issues.is_empty());
    }

    #[test]
    fn parse_classdef_single_property() {
        let result = parse_classdef_statement("classDef err fill:#f00");
        let parsed = result.unwrap();
        assert_eq!(parsed.class_name, "err");
        assert!(parsed.style.fill.is_some());
        assert!(parsed.style.stroke.is_none());
    }

    #[test]
    fn parse_classdef_default_class_name() {
        let result = parse_classdef_statement("classDef default fill:#fff");
        let parsed = result.unwrap();
        assert_eq!(parsed.class_name, "default");
    }

    #[test]
    fn parse_classdef_missing_body() {
        let result = parse_classdef_statement("classDef highlight");
        assert!(result.is_some());
        let parsed = result.unwrap();
        assert_eq!(parsed.class_name, "highlight");
        assert!(parsed.style.is_empty());
    }

    #[test]
    fn parse_classdef_no_class_name() {
        let result = parse_classdef_statement("classDef ");
        assert!(result.is_none());
    }

    #[test]
    fn parse_classdef_not_classdef() {
        assert!(parse_classdef_statement("style A fill:#f00").is_none());
        assert!(parse_classdef_statement("class A highlight").is_none());
    }

    #[test]
    fn parse_classdef_unsupported_property_reported() {
        let result = parse_classdef_statement("classDef foo fill:#f00,font-size:14px");
        let parsed = result.unwrap();
        assert!(parsed.style.fill.is_some());
        assert!(!parsed.issues.is_empty());
    }

    #[test]
    fn parse_classdef_case_insensitive() {
        assert!(parse_classdef_statement("CLASSDEF foo fill:#f00").is_some());
        assert!(parse_classdef_statement("ClassDef foo fill:#f00").is_some());
    }

    #[test]
    fn parse_classdef_multi_class_names() {
        let results = parse_classdef_statement_multi("classDef a,b fill:#f00");
        assert_eq!(results.len(), 2);
        assert_eq!(results[0].class_name, "a");
        assert_eq!(results[1].class_name, "b");
        assert_eq!(results[0].style.fill.as_ref().unwrap().raw(), "#f00");
        assert_eq!(results[1].style.fill.as_ref().unwrap().raw(), "#f00");
    }

    #[test]
    fn parse_classdef_multi_single_name_returns_one() {
        let results = parse_classdef_statement_multi("classDef highlight fill:#ff0");
        assert_eq!(results.len(), 1);
        assert_eq!(results[0].class_name, "highlight");
    }

    // --- class apply parsing ---

    #[test]
    fn parse_class_apply_single_node() {
        let result = parse_class_apply_statement("class A highlight");
        assert!(result.is_some());
        let parsed = result.unwrap();
        assert_eq!(parsed.node_ids, vec!["A"]);
        assert_eq!(parsed.class_name, "highlight");
    }

    #[test]
    fn parse_class_apply_multiple_nodes() {
        let result = parse_class_apply_statement("class A,B,C highlight");
        let parsed = result.unwrap();
        assert_eq!(parsed.node_ids, vec!["A", "B", "C"]);
        assert_eq!(parsed.class_name, "highlight");
    }

    #[test]
    fn parse_class_apply_with_spaces() {
        let result = parse_class_apply_statement("class A, B, C highlight");
        let parsed = result.unwrap();
        assert_eq!(parsed.node_ids, vec!["A", "B", "C"]);
        assert_eq!(parsed.class_name, "highlight");
    }

    #[test]
    fn parse_class_apply_empty_input() {
        assert!(parse_class_apply_statement("class ").is_none());
        assert!(parse_class_apply_statement("class").is_none());
    }

    #[test]
    fn parse_class_apply_only_class_name_no_nodes() {
        assert!(parse_class_apply_statement("class highlight").is_none());
    }

    #[test]
    fn parse_class_apply_not_class() {
        assert!(parse_class_apply_statement("classDef foo fill:#f00").is_none());
        assert!(parse_class_apply_statement("style A fill:#f00").is_none());
    }

    #[test]
    fn parse_linkstyle_statement_extracts_targets_and_supported_properties() {
        let parsed =
            parse_linkstyle_statement("linkStyle 0,2 stroke:#f00,stroke-width:4px").unwrap();

        assert_eq!(parsed.target, LinkStyleTarget::Indices(vec![0, 2]));
        assert_eq!(parsed.style.stroke.as_ref().unwrap().raw(), "#f00");
        assert_eq!(parsed.style.stroke_width.as_deref(), Some("4px"));
    }

    #[test]
    fn parse_linkstyle_statement_supports_default_target() {
        let parsed = parse_linkstyle_statement("linkStyle default stroke:#999").unwrap();

        assert_eq!(parsed.target, LinkStyleTarget::Default);
        assert_eq!(parsed.style.stroke.as_ref().unwrap().raw(), "#999");
    }

    #[test]
    fn parse_linkstyle_invalid_index_reports_issue() {
        let parsed = parse_linkstyle_statement("linkStyle nope,1 stroke:#f00").unwrap();
        assert_eq!(parsed.target, LinkStyleTarget::Indices(vec![1]));
        assert_eq!(parsed.issues.len(), 1);
        assert!(
            parsed.issues[0].message().contains("nope"),
            "should report the invalid token: {}",
            parsed.issues[0].message()
        );
    }

    #[test]
    fn parse_linkstyle_all_invalid_indices_returns_none() {
        assert!(parse_linkstyle_statement("linkStyle nope stroke:#f00").is_none());
    }
}