why2-chat 2.2.2

Lightweight, fast and secure chat application powered by WHY2 encryption.
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
/*
This is part of WHY2
Copyright (C) 2022-2026 Václav Šmejkal

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <https://www.gnu.org/licenses/>.
*/

use std::mem;

use ratatui::
{
    style::{ Modifier, Style },
    text::{ Line, Span },
};

use unicode_width::UnicodeWidthChar;

use super::
{
    math,
    theme,
    state,
    consts,
};

//STRUCTS
struct Emphasis //AN OPEN RUN AND WHERE IT WAS FOUND TO CLOSE
{
    run: usize,
    end: usize,
    modifier: Modifier,
}

struct Marker<'a> //WHAT A ROW OPENS WITH ONCE ITS LINE MARKER IS OFF
{
    spans: Vec<Span<'static>>,   //THE MARKER, AS IT IS DRAWN
    hanging: Vec<Span<'static>>, //WHAT ITS WRAPPED ROWS OPEN WITH
    style: Style,                //A HEADING RESTYLES THE WHOLE ROW
    rest: &'a str,
}

//ENUMS
//WHAT A MESSAGE IS MADE OF, MARKUP OFF
enum Segment
{
    Text(String),
    Raw(String),                                  //A CHARACTER THE BACKSLASH TOOK THE MARKUP OFF
    Code(String),                                 //INLINE `code`
    Block { lang: Option<String>, body: String }, //FENCED ```code```
    Math(String),                                 //INLINE $math$
    Display(String),                              //$$math$$
    Link { text: String, url: String },           //[text](url)
    Open(Modifier),                               //EMPHASIS OPENS
    Close(Modifier),                              //AND CLOSES
}

//FUNCTIONS
//ONE MESSAGE, AS THE ROWS IT DRAWS AS
pub fn render(prefix: Vec<Span<'static>>, text: &str, style: Style, width: u16, math: bool)
    -> Vec<Line<'static>>
{
    let mut out: Vec<Line<'static>> = Vec::new();
    let mut current = prefix;
    let mut open = true;  //A LINE IS BEING BUILT
    let mut start = true; //AND NOTHING IS ON IT YET

    let mut line = style;                             //WHAT A HEADING RESTYLES THE ROW TO
    let mut hanging: Vec<Span<'static>> = Vec::new(); //AND WHAT ITS WRAPPED ROWS OPEN WITH
    let mut stack: Vec<Modifier> = Vec::new();

    for segment in parse(text, math)
    {
        match segment
        {
            //THE ONLY LINE BREAK INSIDE TEXT
            Segment::Text(text) => for (i, part) in text.split('\n').enumerate()
            {
                let mut part = part;

                if i > 0
                {
                    flush(&mut out, &mut current, &mut open, width, &hanging);

                    open = true;
                    start = true;
                    line = style;

                    hanging.clear();
                }

                //A LINE OF NOTHING BUT DASHES IS A RULE
                if start && is_rule(part)
                {
                    open = true;

                    current.push(rule(&current, width));
                    flush(&mut out, &mut current, &mut open, width, &hanging);

                    start = false;

                    continue;
                }

                if start && let Some(found) = marker(part, style)
                {
                    current.extend(found.spans);

                    hanging = found.hanging;
                    line = found.style;
                    part = found.rest;
                    start = false;
                    open = true;
                }

                if !part.is_empty()
                {
                    current.push(Span::styled(part.to_owned(), line.add_modifier(active(&stack))));

                    open = true;
                    start = false;
                }
            },

            Segment::Raw(text) =>
            {
                current.push(Span::styled(text, line.add_modifier(active(&stack))));

                open = true;
                start = false;
            },

            //A NEWLINE INSIDE INLINE CODE IS A SPACE
            Segment::Code(code) =>
            {
                current.push(Span::styled(code.replace('\n', " "), theme::CODE.add_modifier(active(&stack))));

                open = true;
                start = false;
            },

            //SHOWN AS ITS TEXT, WITH THE TARGET BESIDE IT
            Segment::Link { text, url } =>
            {
                current.push(Span::styled(text.clone(), theme::LINK));

                if url != text { current.push(Span::styled(format!(" ({url})"), theme::DIM)); }

                open = true;
                start = false;
            },

            Segment::Math(source) =>
            {
                current.extend(math::inline(&source, line.add_modifier(active(&stack))));

                open = true;
                start = false;
            },

            //BOTH OWN THEIR ROWS, SO CLOSE THE CURRENT RUN
            Segment::Block { lang, body } =>
            {
                close(&mut out, &mut current, &mut open, width, &hanging);
                block(&mut out, lang.as_deref(), &body, width);

                line = style;
                start = true;

                hanging.clear();
            },

            Segment::Display(source) =>
            {
                close(&mut out, &mut current, &mut open, width, &hanging);
                out.extend(math::display(&source, width));

                line = style;
                start = true;

                hanging.clear();
            },

            Segment::Open(modifier) => stack.push(modifier),
            Segment::Close(modifier) => { stack.pop_if(|open| *open == modifier); },
        }
    }

    //A MESSAGE THAT ENDS ON A BLOCK ENDS THERE
    if open || out.is_empty() { flush(&mut out, &mut current, &mut open, width, &hanging); }

    out
}

fn active(stack: &[Modifier]) -> Modifier //EVERY EMPHASIS THE TEXT IS INSIDE OF
{
    stack.iter().fold(Modifier::empty(), |all, modifier| all | *modifier)
}

fn flush(out: &mut Vec<Line<'static>>, current: &mut Vec<Span<'static>>, open: &mut bool, width: u16,
    hanging: &[Span<'static>])
{
    if !*open { return; } //NOTHING IS BEING BUILT

    let hang = hanging.iter().map(|span| text_width(&span.content)).sum::<usize>() as u16;
    let rows = state::wrap_line(&Line::from(mem::take(current)), width.saturating_sub(hang));

    //A WRAPPED ROW OPENS UNDER THE MARKER, NOT UNDER THE PANE
    for (i, row) in rows.into_iter().enumerate()
    {
        match i > 0 && !hanging.is_empty()
        {
            true =>
            {
                let mut spans = hanging.to_vec();

                spans.extend(row.spans);
                out.push(Line::from(spans));
            },

            false => out.push(row),
        }
    }

    *open = false;
}

fn close(out: &mut Vec<Line<'static>>, current: &mut Vec<Span<'static>>, open: &mut bool, width: u16,
    hanging: &[Span<'static>])
{
    match current.is_empty()
    {
        true => *open = false,
        false => flush(out, current, open, width, hanging),
    }
}

//A FENCED BLOCK, PADDED AND NEVER WORD-WRAPPED
fn block(out: &mut Vec<Line<'static>>, lang: Option<&str>, body: &str, width: u16)
{
    let inner = width.saturating_sub(consts::GUTTER).max(1) as usize;

    //NOTHING HIGHLIGHTS, SO THE LANGUAGE IS SHOWN
    if let Some(lang) = lang.filter(|lang| !lang.is_empty())
    {
        out.push(row(pad(lang, inner), theme::CODE_LANG));
    }

    for line in body.split('\n')
    {
        for chunk in split_cells(&expand_tabs(line), inner)
        {
            out.push(row(pad(&chunk, inner), theme::CODE_BLOCK));
        }
    }
}

fn row(content: String, style: Style) -> Line<'static>
{
    Line::from(vec![Span::styled("▏ ", theme::CODE_BAR), Span::styled(content, style)])
}

fn pad(text: &str, width: usize) -> String
{
    let mut out = text.to_owned();

    out.extend(std::iter::repeat_n(' ', width.saturating_sub(text_width(text))));

    out
}

fn expand_tabs(line: &str) -> String
{
    let mut out = String::new();

    for c in line.chars()
    {
        match c
        {
            '\t' => out.extend(std::iter::repeat_n(' ', consts::TAB - out.chars().count() % consts::TAB)),
            '\r' => {},
            _ => out.push(c),
        }
    }

    out
}

fn split_cells(text: &str, width: usize) -> Vec<String> //HARD-BREAK ONE SOURCE LINE EVERY width CELLS
{
    let mut out = Vec::new();
    let mut chunk = String::new();
    let mut column = 0usize;

    for c in text.chars()
    {
        let w = c.width().unwrap_or(0);

        if column + w > width && column > 0
        {
            out.push(mem::take(&mut chunk));
            column = 0;
        }

        chunk.push(c);
        column += w;
    }

    out.push(chunk);
    out
}

pub fn text_width(text: &str) -> usize
{
    text.chars().map(|c| c.width().unwrap_or(0)).sum()
}

//THE LINE-LEVEL MARKDOWN, TAKEN OFF THE FRONT OF A ROW
fn marker(part: &str, style: Style) -> Option<Marker<'_>>
{
    let spaces = part.len() - part.trim_start_matches(' ').len();
    let body = &part[spaces..];
    let indent = || Span::raw(" ".repeat(spaces));

    //A HEADING, WHOSE MARKER IS NOT DRAWN AT ALL
    let hashes = body.chars().take_while(|c| *c == '#').count();

    if (1..=consts::MAX_HEADING).contains(&hashes) && body[hashes..].starts_with(' ')
    {
        return Some(Marker
        {
            spans: vec![indent()],
            hanging: vec![indent()],
            style: heading(style, hashes),
            rest: body[hashes..].trim_start_matches(' '),
        });
    }

    if body.starts_with("> ") || body == ">"
    {
        return Some(Marker
        {
            spans: vec![indent(), Span::styled(consts::QUOTE, theme::QUOTE)],
            hanging: vec![indent(), Span::styled(consts::QUOTE, theme::QUOTE)],
            style,
            rest: body[1..].strip_prefix(' ').unwrap_or(""),
        });
    }

    if matches!(body.get(..2), Some("- " | "* " | "+ "))
    {
        return Some(Marker
        {
            spans: vec![indent(), Span::styled(consts::BULLET, theme::BULLET)],
            hanging: vec![Span::raw(" ".repeat(spaces + text_width(consts::BULLET)))],
            style,
            rest: body[2..].trim_start_matches(' '),
        });
    }

    //AN ORDERED LIST KEEPS THE NUMBER IT WAS TYPED WITH
    let digits = body.chars().take_while(char::is_ascii_digit).count();

    if (1..=consts::MAX_ORDINAL).contains(&digits) && matches!(body.chars().nth(digits), Some('.' | ')'))
        && body[digits + 1..].starts_with(' ')
    {
        return Some(Marker
        {
            spans: vec![indent(), Span::styled(body[..digits + 2].to_owned(), theme::BULLET)],
            hanging: vec![Span::raw(" ".repeat(spaces + digits + 2))],
            style,
            rest: body[digits + 2..].trim_start_matches(' '),
        });
    }

    None
}

fn heading(style: Style, level: usize) -> Style //A HEADING KEEPS THE MESSAGE'S COLOUR WHERE IT HAS ONE
{
    let style = match style.fg
    {
        Some(_) => style,
        None => style.patch(theme::HEADING),
    };

    match level
    {
        1 => style.add_modifier(Modifier::BOLD | Modifier::UNDERLINED),
        _ => style.add_modifier(Modifier::BOLD),
    }
}

fn is_rule(text: &str) -> bool //THREE OR MORE OF THE SAME MARKER, AND NOTHING ELSE
{
    let text = text.trim();
    let Some(first) = text.chars().next() else { return false };

    matches!(first, '-' | '*' | '_') && text.chars().count() >= consts::MIN_RULE
        && text.chars().all(|c| c == first)
}

fn rule(current: &[Span<'static>], width: u16) -> Span<'static> //FILLING WHAT IS LEFT OF THE ROW
{
    let used: usize = current.iter().map(|span| text_width(&span.content)).sum();

    Span::styled(consts::RULE.to_string().repeat((width as usize).saturating_sub(used)), theme::RULE)
}

//THE PARSER; IT NEVER CONSUMES AN UNCLOSED RUN
fn parse(text: &str, math: bool) -> Vec<Segment>
{
    let chars: Vec<char> = text.chars().collect();

    let mut out: Vec<Segment> = Vec::new();
    let mut buf = String::new();
    let mut stack: Vec<Emphasis> = Vec::new();
    let mut i = 0usize;

    //A DELIMITER NOT FOUND ONCE IS NOT SEARCHED AGAIN
    let mut missing = [false; consts::MARKUP_KINDS];

    while i < chars.len()
    {
        //A BACKSLASH TAKES THE MARKUP OFF WHAT FOLLOWS
        if chars[i] == '\\' && chars.get(i + 1).is_some_and(|c| consts::ESCAPABLE.contains(*c))
        {
            flush_text(&mut out, &mut buf);
            out.push(Segment::Raw(chars[i + 1].to_string()));

            i += 2;

            continue;
        }

        let taken = match chars[i]
        {
            '`' => backtick(&chars, i, &mut out, &mut buf, &mut missing),
            '$' if math => dollar(&chars, i, &mut out, &mut buf, &mut missing),
            '[' => link(&chars, i, &mut out, &mut buf, &mut missing),
            '*' | '_' | '~' => emphasis(&chars, i, &mut out, &mut buf, &mut stack, &mut missing, math),
            _ => None,
        };

        match taken
        {
            Some(next) => i = next,
            None =>
            {
                buf.push(chars[i]);
                i += 1;
            },
        }
    }

    if !buf.is_empty() { out.push(Segment::Text(buf)); }

    out
}

//THREE BACKTICKS OPEN A FENCE, ONE OR TWO INLINE
fn backtick(chars: &[char], i: usize, out: &mut Vec<Segment>, buf: &mut String,
    missing: &mut [bool; consts::MARKUP_KINDS]) -> Option<usize>
{
    let run = chars[i..].iter().take_while(|c| **c == '`').count();
    let kind = run.min(3) - 1;

    if missing[kind] { return None; }

    let (segment, next) = match run >= 3
    {
        true =>
        {
            let start = i + 3;
            let end = seen(find(chars, start, &['`', '`', '`']), &mut missing[kind])?;
            let inner: String = chars[start..end].iter().collect();

            (fence(&inner), end + 3)
        },

        false =>
        {
            let start = i + run;
            let close = vec!['`'; run];
            let end = seen(find_escaped(chars, start, &close), &mut missing[kind])?;
            let inner: String = chars[start..end].iter().collect();

            if inner.is_empty() { return None; }

            (Segment::Code(inner), end + run)
        },
    };

    flush_text(out, buf);
    out.push(segment);

    Some(next)
}

//DISCORD'S RULE: A LONE FIRST WORD IS THE LANGUAGE
fn fence(inner: &str) -> Segment
{
    let (lang, body) = match inner.split_once('\n')
    {
        Some((first, rest)) if is_language(first) => (Some(first.trim().to_owned()), rest),
        _ => (None, inner.strip_prefix('\n').unwrap_or(inner)),
    };

    Segment::Block { lang, body: body.strip_suffix('\n').unwrap_or(body).to_owned() }
}

fn is_language(word: &str) -> bool
{
    let word = word.trim();

    !word.is_empty() && word.len() <= consts::MAX_LANG
        && word.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '+' | '#' | '-' | '_' | '.'))
}

//MATH, WITH THE GUARDS THAT KEEP PRICES OUT OF IT
fn dollar(chars: &[char], i: usize, out: &mut Vec<Segment>, buf: &mut String,
    missing: &mut [bool; consts::MARKUP_KINDS]) -> Option<usize>
{
    let display = chars.get(i + 1) == Some(&'$');
    let close: Vec<char> = if display { vec!['$', '$'] } else { vec!['$'] };
    let start = i + close.len();
    let kind = 2 + close.len();

    if missing[kind] { return None; }
    if chars.get(start).is_none_or(|c| c.is_whitespace()) { return None; }

    let end = seen(find_escaped(chars, start, &close), &mut missing[kind])?;

    if chars[end - 1].is_whitespace() { return None; }
    if !display && chars.get(end + 1).is_some_and(char::is_ascii_digit) { return None; }

    let inner: String = chars[start..end].iter().collect();

    flush_text(out, buf);
    out.push(if display { Segment::Display(inner) } else { Segment::Math(inner) });

    Some(end + close.len())
}

//A LINK, WHOSE TARGET HAS TO BE ONE WORD
fn link(chars: &[char], i: usize, out: &mut Vec<Segment>, buf: &mut String,
    missing: &mut [bool; consts::MARKUP_KINDS]) -> Option<usize>
{
    let kind = kind('[');

    if missing[kind] { return None; }

    let label = seen(find_escaped(chars, i + 1, &[']']), &mut missing[kind])?;

    if chars.get(label + 1) != Some(&'(') { return None; }

    let end = find(chars, label + 2, &[')'])?;
    let text: String = chars[i + 1..label].iter().collect();
    let url: String = chars[label + 2..end].iter().collect();

    if text.is_empty() || url.is_empty() || url.chars().any(char::is_whitespace) { return None; }

    flush_text(out, buf);
    out.push(Segment::Link { text, url });

    Some(end + 1)
}

//EMPHASIS; A RUN ONLY OPENS IF ITS CLOSE IS ALREADY IN SIGHT
fn emphasis(chars: &[char], i: usize, out: &mut Vec<Segment>, buf: &mut String, stack: &mut Vec<Emphasis>,
    missing: &mut [bool; consts::MARKUP_KINDS], math: bool) -> Option<usize>
{
    //WHATEVER IS OPEN CLOSES HERE
    if stack.last().is_some_and(|open| open.end == i)
    {
        let open = stack.pop()?;

        flush_text(out, buf);
        out.push(Segment::Close(open.modifier));

        return Some(i + open.run);
    }

    let c = chars[i];
    let run = chars[i..].iter().take_while(|x| **x == c).take(consts::MAX_RUN).count();
    let modifier = modifier(c, run)?;
    let kind = kind(c);

    if missing[kind] { return None; }

    let start = i + run;

    if chars.get(start).is_none_or(|c| c.is_whitespace()) { return None; }
    if c == '_' && i > 0 && chars[i - 1].is_alphanumeric() { return None; } //snake_case IS A WORD

    let end = seen(find_run(chars, start, c, run, math), &mut missing[kind])?;

    if c == '_' && chars.get(end + run).is_some_and(|c| c.is_alphanumeric()) { return None; }

    flush_text(out, buf);
    stack.push(Emphasis { run, end, modifier });
    out.push(Segment::Open(modifier));

    Some(start)
}

fn modifier(c: char, run: usize) -> Option<Modifier> //WHAT A RUN OF THAT LENGTH MEANS
{
    match (c, run)
    {
        ('*' | '_', 1) => Some(Modifier::ITALIC),
        ('*', 2) => Some(Modifier::BOLD),
        ('*', _) => Some(Modifier::BOLD | Modifier::ITALIC),
        ('_', 2) => Some(Modifier::UNDERLINED),
        ('_', _) => Some(Modifier::UNDERLINED | Modifier::ITALIC),
        ('~', 1) => None,
        ('~', _) => Some(Modifier::CROSSED_OUT),
        _ => None,
    }
}

fn kind(c: char) -> usize //ITS SLOT IN THE MISSING TABLE
{
    match c
    {
        '*' => 5,
        '_' => 6,
        '~' => 7,
        _ => 8,
    }
}

fn find(chars: &[char], from: usize, needle: &[char]) -> Option<usize> //FIRST needle AT OR AFTER from
{
    (from..chars.len().saturating_sub(needle.len() - 1))
        .find(|i| chars[*i..*i + needle.len()] == *needle)
}

//THE SAME, PAST BACKSLASHES
fn find_escaped(chars: &[char], from: usize, needle: &[char]) -> Option<usize>
{
    let mut i = from;

    while i + needle.len() <= chars.len()
    {
        if chars[i] == '\\' { i += 2; continue; }
        if chars[i..i + needle.len()] == *needle { return Some(i); }

        i += 1;
    }

    None
}

//AND THE RUN THAT COULD CLOSE AN EMPHASIS, PAST CODE AND MATH
fn find_run(chars: &[char], from: usize, needle: char, run: usize, math: bool) -> Option<usize>
{
    let mut i = from;

    while i < chars.len()
    {
        if chars[i] == '\\' { i += 2; continue; }

        if let Some(next) = span(chars, i, math) { i = next; continue; }

        let len = chars[i..].iter().take_while(|c| **c == needle).take(run).count();

        if len == 0 { i += 1; continue; }
        if len >= run && !chars[i - 1].is_whitespace() { return Some(i); } //A CLOSE HANGS ON THE WORD BEFORE IT

        i += len;
    }

    None
}

fn span(chars: &[char], i: usize, math: bool) -> Option<usize> //HOW FAR A CODE OR MATH SPAN AT i REACHES
{
    let delim = match chars[i]
    {
        '`' => '`',
        '$' if math => '$',
        _ => return None,
    };

    let run = chars[i..].iter().take_while(|c| **c == delim).count().min(3);

    find(chars, i + run, &vec![delim; run]).map(|end| end + run)
}

fn seen(found: Option<usize>, missing: &mut bool) -> Option<usize> //A SEARCH THAT FAILED IS NOT REPEATED
{
    *missing = found.is_none();

    found
}

fn flush_text(out: &mut Vec<Segment>, buf: &mut String)
{
    if !buf.is_empty() { out.push(Segment::Text(mem::take(buf))); }
}