c0data 0.3.1

C0DATA — structured data using ASCII C0 control codes
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
//! Pretty form: Unicode Control Pictures (U+2400 block) for the codes,
//! line breaks and indentation as layout. [`format`] and [`parse`] are
//! inverses on well-formed input.

use crate::*;

/// Pretty layout mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FormatMode {
    /// One element per line, no padding.
    Compact,
    /// Column-aligned units within table groups.
    Aligned,
    /// Aligned, plus a space after the prefix and around `␟`.
    Spaced,
}

/// The Control Picture for a control byte.
#[inline]
pub fn glyph(byte: u8) -> char {
    char::from_u32(0x2400 + byte as u32).unwrap_or('\u{FFFD}')
}

#[inline]
fn push_glyph(out: &mut Vec<u8>, byte: u8) {
    let mut tmp = [0u8; 4];
    out.extend_from_slice(glyph(byte).encode_utf8(&mut tmp).as_bytes());
}

/// Format compact bytes as pretty form, two-space indent, compact mode.
pub fn format(buf: &[u8]) -> Result<String> {
    format_with(buf, "  ")
}

/// Format with a custom indent string, compact mode.
pub fn format_with(buf: &[u8], indent: &str) -> Result<String> {
    let mut out = Vec::new();
    format_compact(buf, indent, &mut out)?;
    Ok(String::from_utf8_lossy(&out).into_owned())
}

/// Format with a layout mode.
pub fn format_mode(buf: &[u8], indent: &str, mode: FormatMode) -> Result<String> {
    let compact = format_with(buf, indent)?;
    Ok(match mode {
        FormatMode::Compact => compact,
        _ => align(&compact, mode),
    })
}

// ---- formatting ----------------------------------------------------

fn newline_unless(out: &mut Vec<u8>, line_start: bool) {
    if !line_start {
        out.push(b'\n');
    }
}

fn format_compact(buf: &[u8], indent: &str, out: &mut Vec<u8>) -> Result<()> {
    let len = buf.len();
    let mut pos = 0;
    let mut depth = 0usize; // nesting depth (levels)
    let mut gdepth = 0usize; // group depth within the current level
    let mut line_start = true;
    let mut stack: Vec<usize> = Vec::new();

    while pos < len {
        let byte = buf[pos];
        if byte >= 0x20 || is_layout(byte) {
            // stray plain or layout at element level: layout is dropped
            if is_layout(byte) {
                pos += 1;
            } else {
                let start = pos;
                pos = scan::plain(buf, pos, len);
                write_data(&buf[start..pos], out)?;
                line_start = false;
            }
            continue;
        }
        match byte {
            SYN => {
                newline_unless(out, line_start);
                push_glyph(out, SYN);
                pos = write_heading(buf, pos + 1, len, out)?;
                out.push(b'\n');
                line_start = true;
                gdepth = 0;
            }
            FS => {
                newline_unless(out, line_start);
                write_indent(out, indent, depth);
                push_glyph(out, FS);
                pos = write_plain(buf, pos + 1, len, out)?;
                out.push(b'\n');
                line_start = true;
                gdepth = 1;
            }
            GS => {
                let mut run = 0;
                while pos < len && buf[pos] == GS {
                    run += 1;
                    pos += 1;
                }
                newline_unless(out, line_start);
                write_indent(out, indent, depth);
                for _ in 0..run {
                    push_glyph(out, GS);
                }
                pos = write_plain(buf, pos, len, out)?;
                out.push(b'\n');
                line_start = true;
                gdepth = 1;
            }
            SOH | RS | BEL | ETB => {
                newline_unless(out, line_start);
                write_indent(out, indent, depth + gdepth);
                let (p, d, ls) = write_element(buf, pos, len, out, depth, gdepth, &mut stack)?;
                pos = p;
                depth = d;
                line_start = ls;
            }
            CAN => {
                depth = depth.saturating_sub(1);
                gdepth = stack.pop().unwrap_or(0);
                newline_unless(out, line_start);
                write_indent(out, indent, depth + gdepth);
                push_glyph(out, CAN);
                pos += 1;
                // the rest of the enclosing record continues on this line
                let (p, d, ls) = write_units(buf, pos, len, out, depth, gdepth, &mut stack)?;
                pos = p;
                depth = d;
                line_start = ls;
                if !line_start {
                    out.push(b'\n');
                    line_start = true;
                }
            }
            EOT => {
                newline_unless(out, line_start);
                push_glyph(out, EOT);
                out.push(b'\n');
                pos += 1;
                line_start = true;
            }
            EM => {
                newline_unless(out, line_start);
                push_glyph(out, EM);
                let tail = len - pos - 1;
                if tail > 0 {
                    out.extend_from_slice(format!(" {tail} bytes").as_bytes());
                }
                out.push(b'\n');
                return Ok(());
            }
            STX | DLE | ENQ | US | SUB => {
                // a unit-level code at element level (e.g. a diff pattern): inline
                let (p, d, ls) = write_units(buf, pos, len, out, depth, gdepth, &mut stack)?;
                pos = p;
                depth = d;
                let _ = ls;
                out.push(b'\n');
                line_start = true;
            }
            _ => {
                return Err(Error::UnassignedCode {
                    byte,
                    position: pos,
                })
            }
        }
    }
    if !line_start {
        out.push(b'\n');
    }
    Ok(())
}

// SOH / RS / BEL / ETB at element level: the element and its units on one
// line (a level opener breaks the line). Returns (pos, depth, line_start).
fn write_element(
    buf: &[u8],
    mut pos: usize,
    len: usize,
    out: &mut Vec<u8>,
    mut depth: usize,
    gdepth: usize,
    stack: &mut Vec<usize>,
) -> Result<(usize, usize, bool)> {
    let byte = buf[pos];
    let mut line_start = false;
    match byte {
        BEL => {
            push_glyph(out, BEL);
            pos = scan::layout(buf, pos + 1, len);
            if pos < len && (buf[pos] >= 0x20 || buf[pos] == STX) {
                let (p, d, _ls) = write_units(buf, pos, len, out, depth, gdepth, stack)?;
                out.push(b'\n');
                return Ok((p, d, true));
            }
            // dead element: render it on this line
            if pos >= len {
                return Ok((pos, depth, false));
            }
            return write_element(buf, pos, len, out, depth, gdepth, stack);
        }
        SOH => {
            push_glyph(out, SOH);
            pos = scan::layout(buf, pos + 1, len);
            if pos < len && buf[pos] == RS {
                push_glyph(out, RS);
                pos += 1;
            }
            let (p, d, ls) = write_units(buf, pos, len, out, depth, gdepth, stack)?;
            pos = p;
            depth = d;
            line_start = ls;
        }
        RS => {
            push_glyph(out, RS);
            let (p, d, ls) = write_units(buf, pos + 1, len, out, depth, gdepth, stack)?;
            pos = p;
            depth = d;
            line_start = ls;
        }
        ETB => {
            push_glyph(out, ETB);
            pos = write_plain(buf, pos + 1, len, out)?;
        }
        _ => {}
    }
    if !line_start {
        out.push(b'\n');
        line_start = true;
    }
    Ok((pos, depth, line_start))
}

// Units of a record/header/heading on the current line. Stops at a
// record-level code. A level opener writes ␐, breaks the line and returns
// the new depth; the caller resumes at the matching CAN. Returns
// (pos, depth, line_start).
fn write_units(
    buf: &[u8],
    mut pos: usize,
    len: usize,
    out: &mut Vec<u8>,
    depth: usize,
    gdepth: usize,
    stack: &mut Vec<usize>,
) -> Result<(usize, usize, bool)> {
    while pos < len {
        let b = buf[pos];
        if b >= 0x20 {
            let start = pos;
            pos = scan::plain(buf, pos, len);
            write_data(trim(&buf[start..pos]), out)?;
        } else if is_layout(b) {
            pos += 1;
        } else if b == US || b == ENQ || b == SUB {
            push_glyph(out, b);
            pos += 1;
        } else if b == SOH {
            let p2 = scan::layout(buf, pos + 1, len);
            if p2 < len && buf[p2] == RS {
                break;
            }
            push_glyph(out, SOH);
            pos += 1;
        } else if b == STX {
            let stop = scan::text(buf, pos, len)?;
            write_text(&buf[pos..stop], out)?;
            pos = stop;
        } else if b == DLE {
            push_glyph(out, DLE);
            out.push(b'\n');
            stack.push(gdepth);
            return Ok((pos + 1, depth + 1, true));
        } else if b == BEL {
            let p2 = scan::layout(buf, pos + 1, len);
            if p2 < len && (buf[p2] >= 0x20 || buf[p2] == STX || buf[p2] == DLE) {
                push_glyph(out, BEL);
                pos = p2;
            } else {
                break;
            }
        } else if b == ETB {
            push_glyph(out, ETB);
            pos = write_plain(buf, pos + 1, len, out)?;
        } else {
            break;
        }
    }
    Ok((pos, depth, false))
}

fn write_heading(buf: &[u8], mut pos: usize, len: usize, out: &mut Vec<u8>) -> Result<usize> {
    while pos < len {
        let b = buf[pos];
        if b == SOH {
            let p2 = scan::layout(buf, pos + 1, len);
            if p2 < len && buf[p2] == RS {
                break;
            }
            push_glyph(out, SOH);
            pos = write_plain(buf, p2, len, out)?;
            if pos < len && buf[pos] == US {
                push_glyph(out, US);
                pos = scan::layout(buf, pos + 1, len);
                if pos < len && buf[pos] == STX {
                    let stop = scan::text(buf, pos, len)?;
                    write_text(&buf[pos..stop], out)?;
                    pos = stop;
                } else if pos < len && buf[pos] == DLE {
                    let stop = scan::level(buf, pos, len)?;
                    // a nested heading value is written inline
                    write_inline_level(&buf[pos..stop], out)?;
                    pos = stop;
                } else {
                    pos = write_plain(buf, pos, len, out)?;
                }
            }
        } else if b == BEL {
            let p2 = scan::layout(buf, pos + 1, len);
            if p2 < len && (buf[p2] >= 0x20 || buf[p2] == STX) {
                push_glyph(out, BEL);
                pos = p2;
                if buf[pos] == STX {
                    let stop = scan::text(buf, pos, len)?;
                    write_text(&buf[pos..stop], out)?;
                    pos = stop;
                } else {
                    pos = write_plain(buf, pos, len, out)?;
                }
            } else if p2 < len && buf[p2] == SOH {
                push_glyph(out, BEL);
                pos = p2;
            } else {
                break;
            }
        } else if is_layout(b) {
            pos += 1;
        } else {
            break;
        }
    }
    Ok(pos)
}

// A level written on one line (used for heading values).
fn write_inline_level(raw: &[u8], out: &mut Vec<u8>) -> Result<()> {
    let mut i = 0;
    while i < raw.len() {
        let b = raw[i];
        if b == STX {
            let stop = scan::text(raw, i, raw.len())?;
            write_text(&raw[i..stop], out)?;
            i = stop;
        } else if b >= 0x20 {
            let start = i;
            i = scan::plain(raw, i, raw.len());
            write_data(trim(&raw[start..i]), out)?;
        } else if is_layout(b) {
            i += 1;
        } else {
            push_glyph(out, b);
            i += 1;
        }
    }
    Ok(())
}

fn write_plain(buf: &[u8], pos: usize, len: usize, out: &mut Vec<u8>) -> Result<usize> {
    let start = scan::layout(buf, pos, len);
    let pos = scan::plain(buf, start, len);
    write_data(trim(&buf[start..pos]), out)?;
    Ok(pos)
}

// A whole text (STX … ETX): glyphs for the brackets and for any control
// byte inside; everything else as is.
fn write_text(text: &[u8], out: &mut Vec<u8>) -> Result<()> {
    check_glyphs(text)?;
    push_glyph(out, STX);
    for &b in &text[1..text.len() - 1] {
        if b < 0x20 && b != LF && b != CR && b != HT {
            push_glyph(out, b);
        } else {
            out.push(b);
        }
    }
    push_glyph(out, ETX);
    Ok(())
}

fn write_data(data: &[u8], out: &mut Vec<u8>) -> Result<()> {
    check_glyphs(data)?;
    out.extend_from_slice(data);
    Ok(())
}

// Pretty form cannot show a value that contains a Control Picture
// character (U+2400–U+241F, UTF-8 E2 90 80–9F).
fn check_glyphs(data: &[u8]) -> Result<()> {
    if data
        .windows(3)
        .any(|w| w[0] == 0xE2 && w[1] == 0x90 && (0x80..=0x9F).contains(&w[2]))
    {
        return Err(Error::GlyphInData);
    }
    Ok(())
}

fn write_indent(out: &mut Vec<u8>, indent: &str, depth: usize) {
    for _ in 0..depth {
        out.extend_from_slice(indent.as_bytes());
    }
}

// ---- parsing ---------------------------------------------------------

fn glyph_code(ch: char) -> Option<u8> {
    let cp = ch as u32;
    (0x2400..=0x241F).contains(&cp).then(|| (cp - 0x2400) as u8)
}

fn push_char(out: &mut Vec<u8>, ch: char) {
    let mut tmp = [0u8; 4];
    out.extend_from_slice(ch.encode_utf8(&mut tmp).as_bytes());
}

/// Pretty form → compact bytes. Glyphs become codes; outside text, layout
/// touching a code is dropped and interior spaces are kept; inside text
/// everything is kept.
pub fn parse(s: &str) -> Vec<u8> {
    let chars: Vec<char> = s.chars().collect();
    let mut out = Vec::new();
    let mut pending: Vec<u8> = Vec::new(); // whitespace between data characters
    let mut after_code = true;
    let mut i = 0;
    while i < chars.len() {
        let ch = chars[i];
        if let Some(code) = glyph_code(ch) {
            pending.clear();
            out.push(code);
            i += 1;
            if code == STX {
                i = parse_text(&chars, i, &mut out);
            } else if code == EM {
                // the rest of the line is a summary, not data
                while i < chars.len() && chars[i] != '\n' {
                    i += 1;
                }
            }
            after_code = true;
        } else if ch == '\n' || ch == '\r' {
            pending.clear();
            after_code = true;
            i += 1;
        } else if ch == ' ' || ch == '\t' {
            if !after_code {
                pending.push(ch as u8);
            }
            i += 1;
        } else {
            out.append(&mut pending);
            after_code = false;
            push_char(&mut out, ch);
            i += 1;
        }
    }
    out
}

// Inside text: everything verbatim until the ETX glyph; a DLE glyph takes
// the next glyph with it.
fn parse_text(chars: &[char], mut i: usize, out: &mut Vec<u8>) -> usize {
    while i < chars.len() {
        let ch = chars[i];
        if let Some(code) = glyph_code(ch) {
            out.push(code);
            i += 1;
            if code == DLE {
                if let Some(nx) = chars.get(i).and_then(|&c| glyph_code(c)) {
                    out.push(nx);
                    i += 1;
                }
            } else if code == ETX {
                return i;
            }
        } else {
            push_char(out, ch);
            i += 1;
        }
    }
    i
}

// ---- alignment -------------------------------------------------------

struct TableLine {
    line_index: usize,
    prefix: String,
    fields: Vec<String>,
    // seps[i] stood between fields[i] and fields[i+1]: true for ␟, false
    // when the next field begins with its own ␁ mark.
    seps: Vec<bool>,
}

/// Reformat a pretty-form string with column alignment. Keyed records
/// (`␁` units) are aligned on their `␟` like two-column tables.
pub fn align(pretty: &str, mode: FormatMode) -> String {
    let mut lines: Vec<String> = pretty.split('\n').map(str::to_string).collect();
    if lines.last().is_some_and(String::is_empty) {
        lines.pop();
    }
    let groups = find_table_groups(&lines);
    let spaced = mode == FormatMode::Spaced;
    let sp = if spaced { " " } else { "" };
    let g_us = glyph(US);

    let mut table_lines = std::collections::HashSet::new();
    for group in &groups {
        for line in group {
            table_lines.insert(line.line_index);
        }
    }

    for group in &groups {
        let col_count = group.iter().map(|l| l.fields.len()).max().unwrap_or(0);
        let mut max_widths = vec![0usize; col_count];
        for line in group {
            for (c, f) in line.fields.iter().enumerate() {
                max_widths[c] = max_widths[c].max(f.chars().count());
            }
        }
        for line in group {
            let mut text = String::new();
            text.push_str(&line.prefix);
            text.push_str(sp);
            let n = line.fields.len();
            for (col, field) in line.fields.iter().enumerate() {
                text.push_str(field);
                if col < n - 1 {
                    let pad = max_widths[col] - field.chars().count();
                    text.extend(std::iter::repeat_n(' ', pad));
                    if line.seps[col] {
                        text.push_str(sp);
                        text.push(g_us);
                        text.push_str(sp);
                    } else {
                        text.push(' ');
                    }
                }
            }
            lines[line.line_index] = text;
        }
    }

    let prefixes = [
        glyph(FS),
        glyph(GS),
        glyph(RS),
        glyph(SOH),
        glyph(US),
        glyph(SYN),
        glyph(BEL),
    ];
    for (i, line) in lines.iter_mut().enumerate() {
        if table_lines.contains(&i) || line.trim().is_empty() {
            continue;
        }
        let chars: Vec<char> = line.chars().collect();
        let mut ws_end = 0;
        while ws_end < chars.len() && (chars[ws_end] == ' ' || chars[ws_end] == '\t') {
            ws_end += 1;
        }
        let mut glyph_end = ws_end;
        while glyph_end < chars.len() && prefixes.contains(&chars[glyph_end]) {
            glyph_end += 1;
        }
        if glyph_end == ws_end || glyph_end >= chars.len() {
            continue;
        }
        let indent_str: String = chars[..ws_end].iter().collect();
        let glyphs: String = chars[ws_end..glyph_end].iter().collect();
        let rest: String = chars[glyph_end..].iter().collect();
        let rest = rest.trim_start();
        *line = if spaced {
            format!("{indent_str}{glyphs} {rest}")
        } else {
            format!("{indent_str}{glyphs}{rest}")
        };
    }

    let mut result = lines.join("\n");
    result.push('\n');
    result
}

fn find_table_groups(lines: &[String]) -> Vec<Vec<TableLine>> {
    let breakers = [glyph(GS), glyph(FS), glyph(EOT), glyph(SYN), glyph(CAN)];
    let g_soh = glyph(SOH);
    let g_rs = glyph(RS);
    let mut groups: Vec<Vec<TableLine>> = Vec::new();
    let mut current: Option<Vec<TableLine>> = None;

    fn close(groups: &mut Vec<Vec<TableLine>>, current: &mut Option<Vec<TableLine>>) {
        if let Some(cur) = current.take() {
            if !cur.is_empty() {
                groups.push(cur);
            }
        }
    }

    for (i, text) in lines.iter().enumerate() {
        let trimmed = text.trim();
        let first = trimmed.chars().next();
        if trimmed.is_empty() || first.is_some_and(|c| breakers.contains(&c)) {
            close(&mut groups, &mut current);
            continue;
        }
        if first == Some(g_soh) || first == Some(g_rs) {
            match parse_table_line(i, text) {
                Some(parsed) if parsed.fields.len() >= 2 => {
                    current.get_or_insert_with(Vec::new).push(parsed);
                }
                _ => close(&mut groups, &mut current),
            }
        } else {
            close(&mut groups, &mut current);
        }
    }
    close(&mut groups, &mut current);
    groups
}

// Split a ␞ or ␁␞ line into its units on ␟ (text is not split; a keyed
// record splits on its ␁ marks too, so key and value align).
fn parse_table_line(line_index: usize, text: &str) -> Option<TableLine> {
    let g_soh = glyph(SOH);
    let g_rs = glyph(RS);
    let g_us = glyph(US);
    let g_stx = glyph(STX);
    let g_etx = glyph(ETX);
    let g_dle = glyph(DLE);
    let g_can = glyph(CAN);
    let chars: Vec<char> = text.chars().collect();
    let mut i = 0;
    while i < chars.len() && (chars[i] == ' ' || chars[i] == '\t') {
        i += 1;
    }
    if i >= chars.len() {
        return None;
    }
    if chars[i] == g_soh {
        i += 1;
        if !(i < chars.len() && chars[i] == g_rs) {
            return None;
        }
        i += 1;
    } else if chars[i] == g_rs {
        i += 1;
    } else {
        return None;
    }
    let prefix: String = chars[..i].iter().collect();
    let prefix_len = i;
    let mut fields = Vec::new();
    let mut seps = Vec::new();
    let mut field = String::new();
    let mut in_text = false;
    let keyed = i < chars.len() && chars[i] == g_soh;
    while i < chars.len() {
        let ch = chars[i];
        if in_text {
            field.push(ch);
            if ch == g_etx {
                in_text = false;
            }
            if ch == g_dle && i + 1 < chars.len() {
                field.push(chars[i + 1]);
                i += 1;
            }
        } else if ch == g_stx {
            in_text = true;
            field.push(ch);
        } else if ch == g_dle || ch == g_can {
            return None; // nested levels are not aligned
        } else if ch == g_us {
            fields.push(field.trim().to_string());
            seps.push(true);
            field = String::new();
        } else if keyed && ch == g_soh && i > prefix_len {
            fields.push(field.trim().to_string());
            seps.push(false);
            field = String::new();
            field.push(ch);
        } else {
            field.push(ch);
        }
        i += 1;
    }
    if in_text {
        return None; // text continues on the next line
    }
    fields.push(field.trim().to_string());
    if fields.len() < 2 {
        return None;
    }
    Some(TableLine {
        line_index,
        prefix,
        fields,
        seps,
    })
}