mermaid-text 0.49.0

Render Mermaid diagrams as Unicode box-drawing text — no browser, no image protocols, pure Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
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
//! Renderer for [`Packet`] diagrams. Produces a fixed-width table of Unicode
//! box-drawing characters with one row per 32-bit word and field names
//! occupying their bit ranges.
//!
//! ## Layout
//!
//! Rows are 32 bits wide. Each field occupies the columns proportional to its
//! bit width within a row. A bit-number ruler is printed above the first row
//! and at each row boundary so the reader can count bit positions.
//!
//! Fields narrower than 3 characters have their labels truncated with `…` (or
//! omitted when they are 1 bit wide and even the `…` would not fit).
//!
//! Fields spanning more than one row (>32 bits) are split at the 32-bit
//! boundary; the label is printed in the first fragment and the continuation
//! rows are left empty.
//!
//! ## Phase 1 limitations
//!
//! - Row width is fixed at 32 bits. Custom widths are not supported.
//! - Custom colours and `accDescr`/`accTitle` are silently ignored.
//! - Multi-row fields produce a continuation cell rather than a spanning box.

use unicode_width::UnicodeWidthStr;

use crate::packet::{Packet, PacketField};

/// Number of bits per display row.
const BITS_PER_ROW: u32 = 32;

/// Render a [`Packet`] diagram to a Unicode string.
///
/// # Arguments
///
/// * `diag`      — the parsed packet diagram
/// * `max_width` — optional column budget; the inner cell width is scaled down
///   so the total row width stays within the budget (minimum: 1 char per bit)
///
/// # Returns
///
/// A multi-line string. Each row is one 32-bit word with a bit-number ruler
/// above it and the field labels centred inside their cells. Box-drawing
/// characters (`┌ ─ ┐ └ ┘ │ ├ ┤ ┬ ┴ ┼`) are used for borders.
pub fn render(diag: &Packet, max_width: Option<usize>) -> String {
    if diag.fields.is_empty() {
        return "(empty packet diagram)".to_string();
    }

    // Choose cell width (characters per bit) so total width <= max_width.
    // Total row width = 1 (left border) + BITS_PER_ROW * cell_w + BITS_PER_ROW (dividers) + 0
    // = 1 + 32 * cell_w + 32 separator chars
    // Actually: left `│` + for each bit: cell_w chars + `│` = 1 + 32*(cell_w+1) chars
    let cell_w: usize = if let Some(budget) = max_width {
        // 1 + 32*(cell_w+1) <= budget  =>  cell_w <= (budget - 1) / 32 - 1
        let max_cell = budget.saturating_sub(1) / BITS_PER_ROW as usize;
        let max_cell = max_cell.saturating_sub(1);
        max_cell.max(1)
    } else {
        // Default: 2 chars per bit (gives a 97-col row for 32 bits)
        2
    };

    let total_bits = diag.total_bits();
    // Total rows needed (round up to full 32-bit words).
    let total_rows = total_bits.max(1).div_ceil(BITS_PER_ROW);

    let mut out = String::new();

    // Title.
    if let Some(title) = &diag.title {
        out.push_str(title);
        out.push('\n');
        out.push('\n');
    }

    // Build a per-bit slot list: for each bit in the 32*N space, which field
    // (if any) owns it?
    let total_bit_slots = total_rows * BITS_PER_ROW;
    // field_index[bit] = index into diag.fields, or None.
    let mut field_index: Vec<Option<usize>> = vec![None; total_bit_slots as usize];
    for (idx, f) in diag.fields.iter().enumerate() {
        for bit in f.start_bit..=f.end_bit {
            if (bit as usize) < field_index.len() {
                field_index[bit as usize] = Some(idx);
            }
        }
    }

    for row in 0..total_rows {
        let row_start = row * BITS_PER_ROW;
        let row_end = row_start + BITS_PER_ROW - 1;

        // -- Bit ruler above every row --
        out.push_str(&render_ruler(row_start, row_end, cell_w));
        out.push('\n');

        // Collect the field segments for this row.
        let segments = collect_row_segments(&field_index, &diag.fields, row_start, row_end);

        // -- Top border --
        out.push_str(&render_top_border(&segments, cell_w, row == 0));
        out.push('\n');

        // -- Content line --
        out.push_str(&render_content_line(&segments, &diag.fields, cell_w));
        out.push('\n');

        // -- Bottom border (only for the last row) --
        if row + 1 == total_rows {
            out.push_str(&render_bottom_border(&segments, cell_w));
            out.push('\n');
        }
    }

    // Trim trailing newlines.
    while out.ends_with('\n') {
        out.pop();
    }

    out
}

// ---------------------------------------------------------------------------
// Segment helpers
// ---------------------------------------------------------------------------

/// A contiguous run of bits belonging to the same field (or gap) within a row.
#[derive(Debug)]
struct Segment {
    /// Bit index of the first bit in this segment (absolute).
    start_bit: u32,
    /// Bit index of the last bit in this segment (absolute, inclusive).
    end_bit: u32,
    /// `Some(idx)` for a field segment, `None` for an unoccupied gap.
    field_idx: Option<usize>,
    /// `true` when this segment is the first fragment of its field in this row
    /// (used to decide whether to print the label or leave blank).
    is_first_fragment: bool,
}

impl Segment {
    fn bit_width(&self) -> u32 {
        self.end_bit - self.start_bit + 1
    }

    /// Cell width: number of characters this segment occupies (borders
    /// between cells are shared, so each segment gets `bit_width * cell_w`
    /// inner chars, with one `│` separator between adjacent segments).
    fn inner_width(&self, cell_w: usize) -> usize {
        self.bit_width() as usize * cell_w + self.bit_width() as usize - 1
    }
}

/// Collect the ordered list of segments for the bits `row_start..=row_end`.
fn collect_row_segments(
    field_index: &[Option<usize>],
    fields: &[PacketField],
    row_start: u32,
    row_end: u32,
) -> Vec<Segment> {
    let mut segments: Vec<Segment> = Vec::new();
    let mut bit = row_start;

    while bit <= row_end {
        let fi = field_index[bit as usize];
        // Find the run of consecutive bits with the same field.
        let mut end = bit;
        while end < row_end && field_index[(end + 1) as usize] == fi {
            end += 1;
        }

        // Determine if this is the first fragment of this field in any row.
        let is_first_fragment = match fi {
            None => false,
            Some(idx) => fields[idx].start_bit == bit,
        };

        segments.push(Segment {
            start_bit: bit,
            end_bit: end,
            field_idx: fi,
            is_first_fragment,
        });

        bit = end + 1;
    }

    segments
}

// ---------------------------------------------------------------------------
// Rendering helpers
// ---------------------------------------------------------------------------

/// Render the bit-number ruler line above a row.
///
/// Shows bit numbers at the leftmost and rightmost position of each segment,
/// and at the row start/end. Numbers are printed left-padded in the cell.
fn render_ruler(row_start: u32, row_end: u32, cell_w: usize) -> String {
    // Build a character buffer of width = 1 + 32*(cell_w+1) chars.
    // The `│` borders are not part of the ruler, just spaces.
    // We'll print numbers at certain columns.

    let width = (BITS_PER_ROW as usize) * (cell_w + 1) + 1;
    let mut buf = vec![b' '; width];

    // For each bit in the row, the leftmost character of its cell is at:
    //   col = 1 + (bit - row_start) as usize * (cell_w + 1)
    // Print the bit number at that position when it fits.
    let print_bit_label = |buf: &mut Vec<u8>, bit: u32| {
        let col = 1 + (bit - row_start) as usize * (cell_w + 1);
        let label = bit.to_string();
        for (i, ch) in label.bytes().enumerate() {
            if col + i < buf.len() {
                buf[col + i] = ch;
            }
        }
    };

    // Print start-of-row, mid (bit 16 boundary), and end-of-row numbers.
    print_bit_label(&mut buf, row_start);
    // Mid-ruler: print bit at column 16 of this row if cell_w is wide enough.
    let mid_bit = row_start + BITS_PER_ROW / 2;
    if mid_bit <= row_end && cell_w >= 2 {
        print_bit_label(&mut buf, mid_bit);
    }
    // End of row: right-align the last bit number in the last cell.
    {
        let last_label = row_end.to_string();
        let last_col = 1 + (BITS_PER_ROW - 1) as usize * (cell_w + 1);
        let label_bytes = last_label.as_bytes();
        // Print right-aligned within the last cell.
        let start = last_col + cell_w - label_bytes.len().min(cell_w);
        for (i, &ch) in label_bytes.iter().enumerate() {
            if start + i < buf.len() {
                buf[start + i] = ch;
            }
        }
    }

    String::from_utf8(buf)
        .unwrap_or_default()
        .trim_end()
        .to_string()
}

/// Render the top border line of a row.
///
/// `is_first_row` controls whether we use `┌`/`┐`/`┬` (first row) or
/// `├`/`┤`/`┼`/`┬`/`┴` (continuation rows where top border merges with
/// the previous row's bottom border).
fn render_top_border(segments: &[Segment], cell_w: usize, is_first_row: bool) -> String {
    let mut line = String::new();

    if is_first_row {
        line.push('\u{250C}'); //    } else {
        line.push('\u{251C}'); //    }

    for (i, seg) in segments.iter().enumerate() {
        let inner = seg.inner_width(cell_w);
        for _ in 0..inner {
            line.push('\u{2500}'); //        }
        if i + 1 < segments.len() {
            if is_first_row {
                line.push('\u{252C}'); //            } else {
                line.push('\u{253C}'); //            }
        }
    }

    if is_first_row {
        line.push('\u{2510}'); //    } else {
        line.push('\u{2524}'); //    }

    line
}

/// Render the bottom border line of the last row.
fn render_bottom_border(segments: &[Segment], cell_w: usize) -> String {
    let mut line = String::new();
    line.push('\u{2514}'); //
    for (i, seg) in segments.iter().enumerate() {
        let inner = seg.inner_width(cell_w);
        for _ in 0..inner {
            line.push('\u{2500}'); //        }
        if i + 1 < segments.len() {
            line.push('\u{2534}'); //        }
    }

    line.push('\u{2518}'); //    line
}

/// Render the content line: `│ label │ label │ …`.
fn render_content_line(segments: &[Segment], fields: &[PacketField], cell_w: usize) -> String {
    let mut line = String::new();
    line.push('\u{2502}'); //
    for seg in segments {
        let inner = seg.inner_width(cell_w);
        let label = match seg.field_idx {
            None => String::new(),
            Some(idx) if seg.is_first_fragment => fields[idx].label.clone(),
            Some(_) => String::new(), // continuation fragment — blank
        };

        let label = fit_label(&label, inner);
        let label_w = UnicodeWidthStr::width(label.as_str());
        let total_pad = inner.saturating_sub(label_w);
        let left_pad = total_pad / 2;
        let right_pad = total_pad - left_pad;

        for _ in 0..left_pad {
            line.push(' ');
        }
        line.push_str(&label);
        for _ in 0..right_pad {
            line.push(' ');
        }
        line.push('\u{2502}'); //    }

    line
}

/// Fit a label into `max_w` display columns, truncating with `…` if needed.
///
/// When `max_w` is 0, returns an empty string.
/// When `max_w` is 1, returns `…` if the label is non-empty, otherwise `""`.
fn fit_label(label: &str, max_w: usize) -> String {
    if max_w == 0 || label.is_empty() {
        return String::new();
    }
    let w = UnicodeWidthStr::width(label);
    if w <= max_w {
        return label.to_string();
    }
    // Truncate to max_w - 1 columns, then append `…`.
    let target = max_w.saturating_sub(1);
    if target == 0 {
        return "\u{2026}".to_string(); //    }
    let mut result = String::new();
    let mut used = 0usize;
    for ch in label.chars() {
        let cw = unicode_width::UnicodeWidthChar::width(ch).unwrap_or(1);
        if used + cw > target {
            break;
        }
        result.push(ch);
        used += cw;
    }
    result.push('\u{2026}'); //    result
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::parser::packet::parse;

    fn parsed(src: &str) -> Packet {
        parse(src).expect("parse should succeed")
    }

    #[test]
    fn title_appears_in_output() {
        let diag = parsed("packet-beta\n    title My Header\n    0-31: \"Data\"");
        let out = render(&diag, None);
        assert!(
            out.contains("My Header"),
            "title must appear in output:\n{out}"
        );
    }

    #[test]
    fn single_row_32_bit_fields_render() {
        // Two fields that together fill exactly 32 bits.
        let diag =
            parsed("packet-beta\n    0-15: \"Source Port\"\n    16-31: \"Destination Port\"");
        let out = render(&diag, None);

        // Both labels must appear.
        assert!(out.contains("Source Port"), "Source Port missing:\n{out}");
        assert!(
            out.contains("Destination Port"),
            "Destination Port missing:\n{out}"
        );

        // Box-drawing corners must be present.
        assert!(
            out.contains('\u{250C}'),
            "top-left corner ┌ missing:\n{out}"
        );
        assert!(
            out.contains('\u{2510}'),
            "top-right corner ┐ missing:\n{out}"
        );
        assert!(
            out.contains('\u{2514}'),
            "bottom-left corner └ missing:\n{out}"
        );
        assert!(
            out.contains('\u{2518}'),
            "bottom-right corner ┘ missing:\n{out}"
        );

        // A mid-row divider ┬ must be present (between the two fields).
        assert!(out.contains('\u{252C}'), "top divider ┬ missing:\n{out}");
    }

    #[test]
    fn multi_row_field_label_appears_in_first_row_only() {
        // A 64-bit field spans two rows. The label appears in row 0 only.
        let diag = parsed("packet-beta\n    0-63: \"Sequence Number\"");
        let out = render(&diag, None);

        assert!(out.contains("Sequence Number"), "label must appear:\n{out}");

        // Count occurrences of "Sequence Number" — should be exactly 1.
        let occurrences = out.matches("Sequence Number").count();
        assert_eq!(
            occurrences, 1,
            "label should appear exactly once (first fragment only):\n{out}"
        );

        // Two rows means the bottom-left └ comes after a mid-row ├ border.
        assert!(
            out.contains('\u{251C}'),
            "row continuation ├ missing:\n{out}"
        );
    }

    #[test]
    fn empty_diagram_renders_placeholder() {
        let diag = Packet {
            title: None,
            fields: vec![],
        };
        let out = render(&diag, None);
        assert!(
            out.contains("empty packet diagram"),
            "placeholder missing:\n{out}"
        );
    }

    #[test]
    fn single_bit_field_renders_without_panic() {
        let diag = parsed("packet-beta\n    0-30: \"Data\"\n    31: \"Flag\"");
        let out = render(&diag, None);
        // Both fields must appear in some form.
        assert!(out.contains("Data"), "Data field missing:\n{out}");
        // Flag or its truncated form must be somewhere.
        let has_flag = out.contains("Flag") || out.contains('\u{2026}');
        assert!(has_flag, "Flag or ellipsis missing:\n{out}");
    }

    #[test]
    fn max_width_does_not_panic() {
        let diag = parsed("packet-beta\n    0-31: \"Header\"");
        // Very narrow budget — must not panic.
        let out = render(&diag, Some(40));
        assert!(out.contains('\u{250C}'), "box must still render:\n{out}");
    }
}