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
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
/// A lightweight SVG string builder.
///
/// Replaces manual `String::push_str()` chains with a composable API
/// that tracks nesting and reduces intermediate allocations.
pub struct SvgWriter {
    buf: String,
}

impl SvgWriter {
    /// Create a new empty writer with optional pre-allocated capacity.
    pub fn with_capacity(cap: usize) -> Self {
        Self {
            buf: String::with_capacity(cap),
        }
    }

    /// Create a new empty writer.
    pub fn new() -> Self {
        Self::with_capacity(1024)
    }

    /// Push a raw string slice — use sparingly; prefer typed methods.
    pub fn raw(&mut self, s: &str) -> &mut Self {
        self.buf.push_str(s);
        self
    }

    /// Open an SVG element with attributes, write children, close it.
    #[allow(dead_code)]
    pub fn elem(
        &mut self,
        tag: &str,
        attrs: &[(&str, &str)],
        children: impl FnOnce(&mut Self),
    ) -> &mut Self {
        self.buf.push('<');
        self.buf.push_str(tag);
        for (k, v) in attrs {
            self.buf.push(' ');
            self.buf.push_str(k);
            self.buf.push_str("=\"");
            self.buf.push_str(v);
            self.buf.push('"');
        }
        self.buf.push('>');
        children(self);
        self.buf.push_str("</");
        self.buf.push_str(tag);
        self.buf.push('>');
        self
    }

    /// Write a self-closing element: `<tag attr="val"/>`.
    #[allow(dead_code)]
    pub fn void_elem(&mut self, tag: &str, attrs: &[(&str, &str)]) -> &mut Self {
        self.buf.push('<');
        self.buf.push_str(tag);
        for (k, v) in attrs {
            self.buf.push(' ');
            self.buf.push_str(k);
            self.buf.push_str("=\"");
            self.buf.push_str(v);
            self.buf.push('"');
        }
        self.buf.push_str("/>");
        self
    }

    /// Consume the writer and return the accumulated SVG string.
    pub fn finish(self) -> String {
        self.buf
    }
}

impl Default for SvgWriter {
    fn default() -> Self {
        Self::new()
    }
}

impl std::fmt::Write for SvgWriter {
    fn write_str(&mut self, s: &str) -> std::fmt::Result {
        self.buf.push_str(s);
        Ok(())
    }
}

/// Convert HTML named entities to their Unicode equivalents.
/// SVG is XML and only supports &amp; &lt; &gt; &apos; &quot; natively.
/// All other HTML entities must be converted to Unicode characters.
#[allow(dead_code)]
pub fn html_entities_to_unicode(s: &str) -> String {
    // Common HTML entities used in Mermaid diagram labels
    static ENTITIES: &[(&str, &str)] = &[
        ("&laquo;", "\u{00AB}"),  // «
        ("&raquo;", "\u{00BB}"),  // »
        ("&lsaquo;", "\u{2039}"), //        ("&rsaquo;", "\u{203A}"), //        ("&nbsp;", "\u{00A0}"),   // non-breaking space
        ("&mdash;", "\u{2014}"),  //        ("&ndash;", "\u{2013}"),  //        ("&hellip;", "\u{2026}"), //        ("&copy;", "\u{00A9}"),   // ©
        ("&reg;", "\u{00AE}"),    // ®
        ("&trade;", "\u{2122}"),  //        ("&deg;", "\u{00B0}"),    // °
        ("&plusmn;", "\u{00B1}"), // ±
        ("&times;", "\u{00D7}"),  // ×
        ("&divide;", "\u{00F7}"), // ÷
        ("&frac12;", "\u{00BD}"), // ½
        ("&frac14;", "\u{00BC}"), // ¼
        ("&frac34;", "\u{00BE}"), // ¾
        ("&alpha;", "\u{03B1}"),  // α
        ("&beta;", "\u{03B2}"),   // β
        ("&gamma;", "\u{03B3}"),  // γ
        ("&delta;", "\u{03B4}"),  // δ
        ("&pi;", "\u{03C0}"),     // π
        ("&sigma;", "\u{03C3}"),  // σ
        ("&mu;", "\u{03BC}"),     // μ
        ("&Omega;", "\u{03A9}"),  // Ω
        ("&larr;", "\u{2190}"),   //        ("&rarr;", "\u{2192}"),   //        ("&uarr;", "\u{2191}"),   //        ("&darr;", "\u{2193}"),   //        ("&harr;", "\u{2194}"),   //        ("&check;", "\u{2713}"),  //        ("&cross;", "\u{2717}"),  //        ("&bull;", "\u{2022}"),   //        ("&prime;", "\u{2032}"),  //        ("&infin;", "\u{221E}"),  //        ("&ne;", "\u{2260}"),     //        ("&le;", "\u{2264}"),     //        ("&ge;", "\u{2265}"),     //        ("&asymp;", "\u{2248}"),  //                                  // XML builtins — leave as-is (SVG handles these natively)
                                  // &amp; &lt; &gt; &quot; &apos; — do NOT replace these
    ];

    let mut result = s.to_string();
    for (entity, unicode) in ENTITIES {
        result = result.replace(entity, unicode);
    }
    result
}

// ── Rounded-corner polyline path ─────────────────────────────────────────────

/// Render waypoints as straight lines with rounded corners at each bend.
/// Interior waypoints get a small cubic bezier arc of radius `r` instead of
/// a sharp angle, keeping all straight segments but smoothing direction changes.
#[allow(dead_code)]
pub fn rounded_path(pts: &[(f64, f64)], r: f64) -> String {
    let n = pts.len();
    if n == 0 {
        return String::new();
    }
    if n == 1 {
        return format!("M{:.3},{:.3}", pts[0].0, pts[0].1);
    }
    if n == 2 {
        return format!(
            "M{:.3},{:.3}L{:.3},{:.3}",
            pts[0].0, pts[0].1, pts[1].0, pts[1].1
        );
    }

    let unit = |dx: f64, dy: f64| -> (f64, f64) {
        let len = (dx * dx + dy * dy).sqrt();
        if len < 1e-9 {
            (0.0, 0.0)
        } else {
            (dx / len, dy / len)
        }
    };

    let mut d = format!("M{:.3},{:.3}", pts[0].0, pts[0].1);

    for i in 1..n - 1 {
        let (ax, ay) = pts[i - 1];
        let (bx, by) = pts[i];
        let (cx, cy) = pts[i + 1];

        // Direction vectors into and out of the corner
        let (ux, uy) = unit(bx - ax, by - ay);
        let (vx, vy) = unit(cx - bx, cy - by);

        // Distance to corner limited by half the shorter segment
        let in_len = ((bx - ax) * (bx - ax) + (by - ay) * (by - ay)).sqrt();
        let out_len = ((cx - bx) * (cx - bx) + (cy - by) * (cy - by)).sqrt();
        let cr = r.min(in_len / 2.0).min(out_len / 2.0);

        // Approach point (on incoming segment, r before corner)
        let p1x = bx - ux * cr;
        let p1y = by - uy * cr;
        // Departure point (on outgoing segment, r after corner)
        let p2x = bx + vx * cr;
        let p2y = by + vy * cr;

        // Line to approach point, then cubic bezier through corner
        d.push_str(&format!(
            "L{:.3},{:.3}C{:.3},{:.3},{:.3},{:.3},{:.3},{:.3}",
            p1x,
            p1y,
            bx,
            by, // CP1 = corner
            bx,
            by, // CP2 = corner
            p2x,
            p2y,
        ));
    }

    // Final straight segment to last point
    let last = pts[n - 1];
    d.push_str(&format!("L{:.3},{:.3}", last.0, last.1));
    d
}

/// Convenience wrapper with a default corner radius of 5px.
#[allow(dead_code)]
pub fn smooth_bezier_path(pts: &[(f64, f64)]) -> String {
    rounded_path(pts, 5.0)
}

pub fn curve_basis_path(pts: &[(f64, f64)]) -> String {
    let n = pts.len();
    if n == 0 {
        return String::new();
    }
    if n == 1 {
        return format!("M{:.3},{:.3}", pts[0].0, pts[0].1);
    }
    if n == 2 {
        return format!(
            "M{:.3},{:.3}L{:.3},{:.3}",
            pts[0].0, pts[0].1, pts[1].0, pts[1].1
        );
    }
    // Extend with phantom endpoints: [P0, P0, P1, ..., Pn-1, Pn-1]
    let mut e: Vec<(f64, f64)> = Vec::with_capacity(n + 2);
    e.push(pts[0]);
    e.extend_from_slice(pts);
    e.push(pts[n - 1]);

    // B-spline helper: (A + 4B + C) / 6
    let bs = |a: (f64, f64), b: (f64, f64), c: (f64, f64)| -> (f64, f64) {
        ((a.0 + 4.0 * b.0 + c.0) / 6.0, (a.1 + 4.0 * b.1 + c.1) / 6.0)
    };

    // Start exactly at first point, line to B-spline start of segment 0
    let bs0 = bs(e[0], e[1], e[2]);
    let mut d = format!("M{:.3},{:.3}L{:.3},{:.3}", pts[0].0, pts[0].1, bs0.0, bs0.1);

    // Cubic bezier segments through the extended points
    for i in 0..n - 1 {
        let (p0, p1, p2, p3) = (e[i], e[i + 1], e[i + 2], e[i + 3]);
        let cp1 = ((2.0 * p1.0 + p2.0) / 3.0, (2.0 * p1.1 + p2.1) / 3.0);
        let cp2 = ((p1.0 + 2.0 * p2.0) / 3.0, (p1.1 + 2.0 * p2.1) / 3.0);
        let end = bs(p1, p2, p3);
        let _ = p0; // p0 used only for phantom, not needed for cp calculation
        d.push_str(&format!(
            "C{:.3},{:.3},{:.3},{:.3},{:.3},{:.3}",
            cp1.0, cp1.1, cp2.0, cp2.1, end.0, end.1
        ));
    }

    // End exactly at last point
    d.push_str(&format!("L{:.3},{:.3}", pts[n - 1].0, pts[n - 1].1));
    d
}

// ── Generic SVG element builders ─────────────────────────────────────────────
// Phase-1 additions: not yet wired into diagram renderers (Phase 2 will do
// that migration). `dead_code` is suppressed per-item until then.

/// Render an SVG attribute string from key-value pairs.
///
/// Returns a space-leading string like ` foo="bar" baz="qux"` so it can be
/// concatenated directly into an element tag. Returns an empty string when
/// `pairs` is empty.
#[allow(dead_code)]
pub fn svg_attrs(pairs: &[(&str, &str)]) -> String {
    pairs
        .iter()
        .map(|(k, v)| format!(r#" {k}="{v}""#))
        .collect()
}

/// Render a `<rect>` element.
///
/// `x`, `y`, `w`, `h` set the geometry. Pass extra SVG attributes via `attrs`,
/// e.g. `&[("fill", "blue"), ("rx", "4")]`.
#[allow(dead_code)]
pub fn rect(x: f64, y: f64, w: f64, h: f64, attrs: &[(&str, &str)]) -> String {
    let extra = svg_attrs(attrs);
    format!(r#"<rect x="{x}" y="{y}" width="{w}" height="{h}"{extra}/>"#)
}

/// Render a `<circle>` element.
///
/// `cx`, `cy` are the centre coordinates; `r` is the radius. Pass extra SVG
/// attributes via `attrs`, e.g. `&[("fill", "#333"), ("stroke", "none")]`.
#[allow(dead_code)]
pub fn circle(cx: f64, cy: f64, r: f64, attrs: &[(&str, &str)]) -> String {
    let extra = svg_attrs(attrs);
    format!(r#"<circle cx="{cx}" cy="{cy}" r="{r}"{extra}/>"#)
}

/// Render a `<text>` element with optional text content.
///
/// `x`, `y` position the anchor point. `content` is the text body (may be
/// empty). Pass extra SVG attributes via `attrs`, e.g.
/// `&[("text-anchor", "middle"), ("fill", "#000")]`.
#[allow(dead_code)]
pub fn text(x: f64, y: f64, content: &str, attrs: &[(&str, &str)]) -> String {
    let extra = svg_attrs(attrs);
    format!(r#"<text x="{x}" y="{y}"{extra}>{content}</text>"#)
}

/// Render a `<line>` element.
///
/// Pass extra SVG attributes via `attrs`, e.g.
/// `&[("stroke", "#999"), ("stroke-width", "1")]`.
#[allow(dead_code)]
pub fn line(x1: f64, y1: f64, x2: f64, y2: f64, attrs: &[(&str, &str)]) -> String {
    let extra = svg_attrs(attrs);
    format!(r#"<line x1="{x1}" y1="{y1}" x2="{x2}" y2="{y2}"{extra}/>"#)
}

/// Render a `<g>` group element wrapping `children`.
///
/// Pass group-level SVG attributes via `attrs`, e.g.
/// `&[("class", "node"), ("transform", "translate(10,20)")]`.
/// `children` is inserted verbatim between the opening and closing tags.
#[allow(dead_code)]
pub fn g(attrs: &[(&str, &str)], children: &str) -> String {
    let extra = svg_attrs(attrs);
    format!(r#"<g{extra}>{children}</g>"#)
}

/// Render a `<path>` element.
///
/// `d` is the path data string. Pass extra SVG attributes via `attrs`, e.g.
/// `&[("fill", "none"), ("stroke", "black")]`.
#[allow(dead_code)]
pub fn path(d: &str, attrs: &[(&str, &str)]) -> String {
    let extra = svg_attrs(attrs);
    format!(r#"<path d="{d}"{extra}/>"#)
}

/// Render a `<defs>` block.
///
/// `content` is inserted verbatim between `<defs>` and `</defs>`.
#[allow(dead_code)]
pub fn defs(content: &str) -> String {
    format!(r#"<defs>{content}</defs>"#)
}

/// Render a `<marker>` element.
///
/// `id` sets the marker's `id` attribute; `attrs` supplies additional marker
/// attributes such as `refX`, `refY`, `markerWidth`, `markerHeight`, and
/// `orient`; `content` is the inner shape markup (e.g. a `<path>` or
/// `<polygon>`).
#[allow(dead_code)]
pub fn marker(id: &str, attrs: &[(&str, &str)], content: &str) -> String {
    let extra = svg_attrs(attrs);
    format!(r#"<marker id="{id}"{extra}>{content}</marker>"#)
}

/// Render a `<foreignObject>` element with an HTML `<div>` label inside.
///
/// Produces a `<foreignObject>` sized to `w × h` at position `(x, y)`.
/// `label` is placed inside a `<div>` whose inline style is set to
/// `div_style` (may be empty). This matches the pattern used by Mermaid for
/// rich-text node and edge labels.
#[allow(dead_code)]
pub fn foreign_object_label(
    x: f64,
    y: f64,
    w: f64,
    h: f64,
    label: &str,
    div_style: &str,
) -> String {
    format!(
        r#"<foreignObject x="{x}" y="{y}" width="{w}" height="{h}"><div xmlns="http://www.w3.org/1999/xhtml" style="{div_style}">{label}</div></foreignObject>"#
    )
}

/// Render an SVG attribute string from key-value pairs.
///
/// This is the free-function alias for [`svg_attrs`]; both are identical.
/// Provided as a short name for use in expression contexts.
#[allow(dead_code)]
pub fn attrs(pairs: &[(&str, &str)]) -> String {
    svg_attrs(pairs)
}

/// Round all floating-point literals in an SVG string to 3 decimal places.
///
/// Eliminates platform-specific f64 precision differences (Windows vs Linux
/// differ in the 14th+ decimal place). Trailing zeros are stripped so
/// `1.500` becomes `1.5` and `2.000` becomes `2`.
pub(crate) fn normalize_floats(svg: &str) -> String {
    let mut out = String::with_capacity(svg.len());
    let bytes = svg.as_bytes();
    let mut i = 0;
    // Track whether we're inside a tag (< ... >) or in text content (> ... <).
    // Only normalize floats inside tags — text content (e.g. "v1.0") must be preserved.
    let mut in_tag = false;
    while i < bytes.len() {
        if bytes[i] == b'<' {
            in_tag = true;
            out.push('<');
            i += 1;
            continue;
        }
        if bytes[i] == b'>' {
            in_tag = false;
            out.push('>');
            i += 1;
            continue;
        }
        // Outside tags: pass text content through unchanged
        if !in_tag {
            out.push(bytes[i] as char);
            i += 1;
            continue;
        }
        // Detect optional leading minus
        let neg = bytes[i] == b'-' && i + 1 < bytes.len() && bytes[i + 1].is_ascii_digit();
        let start = i;

        let mut j = if neg { i + 1 } else { i };

        // Must start with a digit
        if !bytes[j].is_ascii_digit() {
            out.push(bytes[i] as char);
            i += 1;
            continue;
        }

        // Consume integer digits
        while j < bytes.len() && bytes[j].is_ascii_digit() {
            j += 1;
        }

        // Only a float if followed by '.' then more digits
        if j < bytes.len() && bytes[j] == b'.' {
            let dot = j;
            j += 1;
            let frac_start = j;
            while j < bytes.len() && bytes[j].is_ascii_digit() {
                j += 1;
            }
            if j > frac_start {
                // Parse and reformat to 3dp
                if let Ok(v) = svg[start..j].parse::<f64>() {
                    let rounded = format!("{v:.3}");
                    let trimmed = rounded.trim_end_matches('0').trim_end_matches('.');
                    out.push_str(trimmed);
                    i = j;
                    continue;
                }
                // parse failed — emit verbatim up to dot, retry from dot
                out.push_str(&svg[start..dot]);
                i = dot;
                continue;
            }
            // dot not followed by digits — emit integer part, retry from dot
            out.push_str(&svg[start..dot]);
            i = dot;
            continue;
        }

        // Plain integer — emit verbatim
        out.push_str(&svg[start..j]);
        i = j;
    }
    out
}

/// Inject a background rect into an SVG string so the diagram has an explicit
/// background colour when embedded in a dark host page.
#[allow(dead_code)]
pub(crate) fn inject_background(svg: &str, color: &str) -> String {
    let insert = format!(r#"<rect width="100%" height="100%" fill="{}"/>"#, color);
    // Insert after the first closing `>` of the root <svg ...> tag.
    if let Some(pos) = svg.find('>') {
        let mut out = String::with_capacity(svg.len() + insert.len());
        out.push_str(&svg[..pos + 1]);
        out.push_str(&insert);
        out.push_str(&svg[pos + 1..]);
        out
    } else {
        svg.to_string()
    }
}