ariel-rs 0.2.0

A faithful Rust port of Mermaid JS — headless SVG diagram rendering without a browser
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
// Faithful Rust port of mermaid/src/diagrams/venn/parser/venn.jison + vennDB.ts
//
// Grammar (venn / vennDiagram):
//   venn-beta | vennDiagram
//   [title <text>]
//   set  <id> ["label"] [: <size>]
//   union <id1>, <id2>[, ...] ["label"] [: <size>]
//   text  <sets...> <id> ["label"]
//   style <sets...> property: value[, property: value]*

use std::collections::{HashMap, HashSet};

#[derive(Debug, Clone)]
pub struct VennSet {
    /// Single identifier — e.g. "A"
    pub id: String,
    pub label: Option<String>,
}

#[derive(Debug, Clone)]
pub struct VennIntersection {
    /// Two-or-more identifiers that form the intersection key, sorted.
    pub sets: Vec<String>,
    pub label: Option<String>,
}

#[derive(Debug, Clone)]
pub struct VennTextNode {
    #[allow(dead_code)]
    pub sets: Vec<String>, // sorted set identifiers this text belongs to
    #[allow(dead_code)]
    pub id: String,
    #[allow(dead_code)]
    pub label: Option<String>,
}

#[derive(Debug, Clone)]
pub struct VennStyleEntry {
    pub targets: Vec<String>, // sorted set identifiers
    pub styles: HashMap<String, String>,
}

#[derive(Debug)]
pub struct VennDiagram {
    pub title: Option<String>,
    pub sets: Vec<VennSet>,
    pub intersections: Vec<VennIntersection>,
    #[allow(dead_code)]
    pub text_nodes: Vec<VennTextNode>,
    pub style_entries: Vec<VennStyleEntry>,
}

// ─── Parser ───────────────────────────────────────────────────────────────────

pub fn parse(input: &str) -> crate::error::ParseResult<VennDiagram> {
    let mut title: Option<String> = None;
    let mut sets: Vec<VennSet> = Vec::new();
    let mut intersections: Vec<VennIntersection> = Vec::new();
    let mut text_nodes: Vec<VennTextNode> = Vec::new();
    let mut style_entries: Vec<VennStyleEntry> = Vec::new();
    let mut known_sets: HashSet<String> = HashSet::new();

    let mut in_header = true;

    for raw in input.lines() {
        let line = if let Some(p) = raw.find("%%") {
            &raw[..p]
        } else {
            raw
        };
        let trimmed = line.trim();
        if trimmed.is_empty() {
            continue;
        }

        // Header
        if in_header {
            if trimmed.starts_with("venn") {
                in_header = false;
            }
            continue;
        }

        // title
        if let Some(rest) = trimmed
            .strip_prefix("title ")
            .or_else(|| trimmed.strip_prefix("title\t"))
        {
            // Venn title preserves quotes — Mermaid JS does not strip them.
            title = Some(rest.trim().to_string());
            continue;
        }
        if trimmed == "title" {
            title = Some(String::new());
            continue;
        }

        // accTitle / accDescr – skip
        if trimmed.starts_with("accTitle") || trimmed.starts_with("accDescr") {
            continue;
        }

        // set id ["label"] [: size]
        if let Some(rest) = trimmed
            .strip_prefix("set ")
            .or_else(|| trimmed.strip_prefix("set\t"))
        {
            let (id, label, _size) = parse_set_line(rest.trim());
            let norm_id = normalize_text(&id);
            known_sets.insert(norm_id.clone());
            sets.push(VennSet {
                id: norm_id,
                label: label.map(|l| normalize_text(&l)),
            });
            continue;
        }

        // union id1, id2[, ...] ["label"] [: size]
        if let Some(rest) = trimmed
            .strip_prefix("union ")
            .or_else(|| trimmed.strip_prefix("union\t"))
        {
            let (ids, label, _size) = parse_union_line(rest.trim());
            let mut norm_ids: Vec<String> = ids.iter().map(|id| normalize_text(id)).collect();
            norm_ids.sort();
            intersections.push(VennIntersection {
                sets: norm_ids,
                label: label.map(|l| normalize_text(&l)),
            });
            continue;
        }

        // text sets... id ["label"]
        if let Some(rest) = trimmed
            .strip_prefix("text ")
            .or_else(|| trimmed.strip_prefix("text\t"))
        {
            if let Some(tn) = parse_text_line(rest.trim()) {
                text_nodes.push(tn);
            }
            continue;
        }

        // style sets... property: value[, ...]
        if let Some(rest) = trimmed
            .strip_prefix("style ")
            .or_else(|| trimmed.strip_prefix("style\t"))
        {
            if let Some(se) = parse_style_line(rest.trim()) {
                style_entries.push(se);
            }
            continue;
        }
    }

    crate::error::ParseResult::ok(VennDiagram {
        title,
        sets,
        intersections,
        text_nodes,
        style_entries,
    })
}

// ─── Line parsers ─────────────────────────────────────────────────────────────

/// Parse "id ["label"] [: size]"
fn parse_set_line(s: &str) -> (String, Option<String>, Option<f64>) {
    parse_id_label_size(s)
}

/// Parse "id1, id2[, ...] ["label"] [: size]"
/// Returns (ids, label, size)
fn parse_union_line(s: &str) -> (Vec<String>, Option<String>, Option<f64>) {
    // Identifiers are comma-separated, terminated by an optional bracket label or colon+number
    // Strategy: split on commas, last item may contain "[label]" or ": size"

    // Find where the bracket label or colon-size starts after the identifiers
    // We look for the first '[' or ':' that is NOT part of an identifier
    let (ids_part, rest) = split_ids_from_rest(s);

    let ids: Vec<String> = ids_part
        .split(',')
        .map(|p| p.trim().trim_matches('"').to_string())
        .filter(|p| !p.is_empty())
        .collect();

    let (label, size) = parse_label_size(rest.trim());
    (ids, label, size)
}

/// Split "id1, id2 [label] : size" into ("id1, id2", "[label] : size")
/// Identifiers are alphanumeric + underscores. Stop at '[' or at ':' not inside an identifier.
fn split_ids_from_rest(s: &str) -> (&str, &str) {
    let bytes = s.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        let c = bytes[i] as char;
        if c == '[' || c == '"' {
            // Start of label
            return (&s[..i], &s[i..]);
        }
        if c == ':' {
            // Check next char — if it's a digit/space it's a size separator
            // But it might also be part of the identifier list (not typical)
            return (&s[..i], &s[i..]);
        }
        i += 1;
    }
    (s, "")
}

/// Parse "id ["label"] [: size]" returning (id, label, size)
fn parse_id_label_size(s: &str) -> (String, Option<String>, Option<f64>) {
    // id is first token (space or bracket terminates it)
    let id_end = s
        .find(|c: char| c.is_whitespace() || c == '[' || c == ':')
        .unwrap_or(s.len());
    let id = s[..id_end].trim().trim_matches('"').to_string();
    let rest = s[id_end..].trim();
    let (label, size) = parse_label_size(rest);
    (id, label, size)
}

/// Parse `["label"] [: size]` from a string that already has the id consumed.
fn parse_label_size(s: &str) -> (Option<String>, Option<f64>) {
    let mut label: Option<String> = None;
    let mut size: Option<f64> = None;
    let mut rest = s;

    // Optional bracket label
    if rest.starts_with('[') || rest.starts_with('"') {
        if let Some(label_str) = extract_bracket_label(&mut rest) {
            label = Some(normalize_text(&label_str));
        }
        rest = rest.trim();
    }

    // Optional ": size"
    if let Some(after_colon) = rest.strip_prefix(':') {
        size = after_colon.trim().parse::<f64>().ok();
    }

    (label, size)
}

/// Extract a `["label"]` or `[label]` or `"label"` from the start of *s,
/// advancing s past it.
fn extract_bracket_label(s: &mut &str) -> Option<String> {
    let t = s.trim_start();
    if t.starts_with('[') {
        if let Some(end) = t.find(']') {
            let inner = t[1..end].trim().trim_matches('"').to_string();
            *s = &t[end + 1..];
            return if inner.is_empty() { None } else { Some(inner) };
        }
    } else if let Some(inner_start) = t.strip_prefix('"') {
        if let Some(end) = inner_start.find('"') {
            let inner = inner_start[..end].to_string();
            *s = &inner_start[end + 1..];
            return if inner.is_empty() { None } else { Some(inner) };
        }
    }
    None
}

/// Parse "id1, id2, ... text_id ["label"]"
fn parse_text_line(s: &str) -> Option<VennTextNode> {
    // The text line format from the jison grammar:
    //   TEXT identifierList id [bracketLabel]
    // where identifierList is comma-separated set IDs.
    // We split on commas for the set list; the last token before any '[' is the text node id.

    // Find the bracket label first
    let (body, bracket_label) = if let Some(bi) = s.find('[') {
        let lbl = extract_bracket_label_from_str(&s[bi..]);
        (&s[..bi], lbl)
    } else {
        (s, None)
    };

    let tokens: Vec<&str> = body
        .split(',')
        .map(str::trim)
        .filter(|t| !t.is_empty())
        .collect();
    if tokens.is_empty() {
        return None;
    }

    // Last token is the text node id; rest are set identifiers
    let (sets_tokens, id_token) = if tokens.len() == 1 {
        (tokens.as_slice(), tokens[0])
    } else {
        (&tokens[..tokens.len() - 1], tokens[tokens.len() - 1])
    };

    // The id_token itself might be space-separated: "setA setB textId"
    // Try to split it further
    let id_parts: Vec<&str> = id_token.split_whitespace().collect();
    let (extra_sets, actual_id) = if id_parts.len() > 1 {
        (
            &id_parts[..id_parts.len() - 1],
            id_parts[id_parts.len() - 1],
        )
    } else {
        (&id_parts[..0], id_parts[0])
    };

    let mut sets: Vec<String> = sets_tokens
        .iter()
        .flat_map(|t| t.split_whitespace())
        .map(normalize_text)
        .collect();
    for es in extra_sets {
        sets.push(normalize_text(es));
    }
    sets.sort();

    let id = normalize_text(actual_id);
    let label = bracket_label.map(|l| normalize_text(&l));

    Some(VennTextNode { sets, id, label })
}

fn extract_bracket_label_from_str(s: &str) -> Option<String> {
    let t = s.trim();
    if t.starts_with('[') {
        if let Some(end) = t.find(']') {
            let inner = t[1..end].trim().trim_matches('"').to_string();
            return if inner.is_empty() { None } else { Some(inner) };
        }
    }
    None
}

/// Parse "id1, id2, ... prop: value[, prop: value]*"
fn parse_style_line(s: &str) -> Option<VennStyleEntry> {
    // Strategy: the style properties begin at the first "word: value" pair.
    // Identifiers come before them separated by commas.
    // A CSS property key does not contain spaces; it does contain alphanumerics and hyphens.
    // We detect the boundary where a token followed by ':' is a style property key.

    // Split on commas first, then identify which tokens are CSS properties
    let parts: Vec<&str> = s.split(',').map(str::trim).collect();
    let mut set_ids: Vec<String> = Vec::new();
    let mut styles: HashMap<String, String> = HashMap::new();
    let mut in_styles = false;

    let mut i = 0;
    while i < parts.len() {
        let part = parts[i];
        if !in_styles {
            // Check if this part contains a CSS property (key: value)
            if let Some(cp) = part.find(':') {
                let key = part[..cp].trim();
                // CSS property key: no spaces, only alnum and hyphens
                let is_css_key = key.chars().all(|c| c.is_alphanumeric() || c == '-')
                    && !key.is_empty()
                    && !key.contains(' ');
                if is_css_key && !set_ids.is_empty() {
                    in_styles = true;
                    let val = normalize_style_val(part[cp + 1..].trim());
                    styles.insert(key.to_string(), val);
                    i += 1;
                    continue;
                } else {
                    // It's a set id that happens to look like "A: size" — treat as set ID only
                    set_ids.push(normalize_text(
                        part.split(':').next().unwrap_or(part).trim(),
                    ));
                }
            } else {
                set_ids.push(normalize_text(part));
            }
        } else {
            // We're in the styles section
            if let Some(cp) = part.find(':') {
                let key = part[..cp].trim().to_string();
                let val = normalize_style_val(part[cp + 1..].trim());
                styles.insert(key, val);
            }
        }
        i += 1;
    }

    set_ids.sort();
    let targets: Vec<String> = set_ids.into_iter().filter(|s| !s.is_empty()).collect();
    if targets.is_empty() {
        return None;
    }

    Some(VennStyleEntry { targets, styles })
}

fn normalize_text(s: &str) -> String {
    let trimmed = s.trim();
    if trimmed.len() >= 2 && trimmed.starts_with('"') && trimmed.ends_with('"') {
        trimmed[1..trimmed.len() - 1].to_string()
    } else {
        trimmed.to_string()
    }
}

fn normalize_style_val(s: &str) -> String {
    normalize_text(s)
}