mido-core 0.6.0

Parse, layout and theme engine behind the mido Markdown reader, with no terminal dependency.
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
use std::collections::{BTreeMap, BTreeSet};
use std::fmt;
use std::str::FromStr;
use std::sync::LazyLock;

use ratatui_core::style::{Color, Modifier, Style};
use serde::Deserialize;
use two_face::theme::{EmbeddedLazyThemeSet, EmbeddedThemeName};

use super::glyphs::Glyphs;
use crate::markdown::Alert;

pub const SCHEMA: u32 = 1;
pub const DEFAULT_DARK: &str = "mido-dark";
pub const DEFAULT_LIGHT: &str = "mido-light";

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum ColorMode {
    TrueColor,
    Indexed,
    Mono,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Kind {
    Dark,
    Light,
}

impl FromStr for Kind {
    type Err = String;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "dark" => Ok(Self::Dark),
            "light" => Ok(Self::Light),
            other => Err(format!("unknown kind `{other}`, expected dark or light")),
        }
    }
}

impl fmt::Display for Kind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.pad(match self {
            Self::Dark => "dark",
            Self::Light => "light",
        })
    }
}

macro_rules! tokens {
    ($($name:ident: $doc:literal,)*) => {
        #[derive(Debug, Clone)]
        pub struct Theme {
            pub name: String,
            pub kind: Kind,
            pub syntax: EmbeddedThemeName,
            pub mode: ColorMode,
            pub glyphs: Glyphs,
            $(#[doc = $doc] pub $name: Color,)*
        }

        /// Every color token with what it paints, in the order the docs list them.
        pub const TOKENS: &[(&str, &str)] = &[$((stringify!($name), $doc)),*];

        impl Theme {
            fn blank(name: &str, kind: Kind) -> Self {
                Self {
                    name: name.to_string(),
                    kind,
                    syntax: default_syntax(kind),
                    mode: ColorMode::TrueColor,
                    glyphs: Glyphs::default(),
                    $($name: Color::Reset,)*
                }
            }

            fn token_mut(&mut self, token: &str) -> Option<&mut Color> {
                match token {
                    $(stringify!($name) => Some(&mut self.$name),)*
                    _ => None,
                }
            }

            pub fn map_colors(mut self, f: impl Fn(Color) -> Color) -> Self {
                $(self.$name = f(self.$name);)*
                self
            }
        }
    };
}

tokens! {
    bg: "Background behind the whole viewer, `default` keeps the terminal's own",
    fg: "Body text",
    fg_muted: "Secondary text: H5 and H6, quotes, front matter keys, panel connectors",
    fg_faint: "Hints, placeholders, raw HTML, the overflow marker",
    accent: "Status chip, focus, footnote markers, the image marker",
    h1: "The H1 title chip",
    h2: "H2 text and its underline, second-level list markers",
    h3: "H3 text, third-level list markers",
    h4: "H4 text",
    h5: "H5 text",
    h6: "H6 text",
    h1_fg: "Text on the H1 chip and the status chip",
    link: "Link text",
    link_url: "The URL shown after a link",
    code_fg: "Inline code text",
    code_bg: "Inline code and code block background",
    code_border: "The bar left of a code block",
    surface: "Panel tint, table header band",
    quote_bar: "The bar left of a quote",
    quote_fg: "Quote text",
    rule: "Thematic breaks",
    table_border: "Table box drawing",
    list_marker: "First-level list markers",
    status_bg: "Status bar background",
    status_fg: "Status bar text",
    search_bg: "Search matches",
    search_fg: "Text on a search match",
    search_current_bg: "The current search match",
    overlay_border: "Border of overlays: help, contents, finder",
    selected_bg: "Selected row in a panel or overlay",
    alert_note: "NOTE alerts",
    alert_tip: "TIP alerts",
    alert_important: "IMPORTANT alerts",
    alert_warning: "WARNING alerts",
    alert_caution: "CAUTION alerts",
    math: "Inline and block math",
    label_bg: "Key side of a front matter or badge chip",
    label_value_bg: "Value side of a front matter chip",
}

pub const BUILTIN: &[(&str, &str)] = &[
    ("mido-dark", include_str!("themes/mido-dark.toml")),
    ("mido-light", include_str!("themes/mido-light.toml")),
    ("mido-reading", include_str!("themes/mido-reading.toml")),
    (
        "catppuccin-mocha",
        include_str!("themes/catppuccin-mocha.toml"),
    ),
    (
        "catppuccin-latte",
        include_str!("themes/catppuccin-latte.toml"),
    ),
    ("gruvbox", include_str!("themes/gruvbox.toml")),
    ("nord", include_str!("themes/nord.toml")),
    ("tokyo-night", include_str!("themes/tokyo-night.toml")),
    ("dracula", include_str!("themes/dracula.toml")),
    ("solarized-dark", include_str!("themes/solarized-dark.toml")),
    (
        "solarized-light",
        include_str!("themes/solarized-light.toml"),
    ),
];

static SYNTAX_THEMES: LazyLock<EmbeddedLazyThemeSet> = LazyLock::new(two_face::theme::extra);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ThemeError(pub String);

impl fmt::Display for ThemeError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl std::error::Error for ThemeError {}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct ThemeFile {
    schema: u32,
    kind: Option<Kind>,
    extends: Option<String>,
    syntax: Option<String>,
    #[serde(default)]
    palette: BTreeMap<String, String>,
    #[serde(default)]
    colors: BTreeMap<String, String>,
}

impl Theme {
    pub fn dark() -> Self {
        Self::builtin(DEFAULT_DARK).expect("mido-dark is built in")
    }

    pub fn light() -> Self {
        Self::builtin(DEFAULT_LIGHT).expect("mido-light is built in")
    }

    pub fn builtin(name: &str) -> Option<Self> {
        let (_, text) = BUILTIN.iter().find(|(n, _)| *n == name)?;
        Some(Self::parse(name, text, &|base| Self::builtin(base)).expect("built-in themes parse"))
    }

    /// Reads a theme file. `base` resolves the theme named by `extends`.
    pub fn parse(
        name: &str,
        text: &str,
        base: &dyn Fn(&str) -> Option<Theme>,
    ) -> Result<Self, ThemeError> {
        let file: ThemeFile =
            toml::from_str(text).map_err(|e| ThemeError(format!("{name}: {}", e.message())))?;
        if file.schema != SCHEMA {
            return Err(ThemeError(format!(
                "{name}: schema {} is not supported, this mido reads schema {SCHEMA}",
                file.schema
            )));
        }
        let mut theme = match &file.extends {
            Some(parent) => {
                let mut theme = base(parent).ok_or_else(|| {
                    ThemeError(format!("{name}: extends `{parent}`, which is not a theme"))
                })?;
                theme.name = name.to_string();
                if let Some(kind) = file.kind {
                    theme.kind = kind;
                }
                theme
            }
            None => {
                let kind = file
                    .kind
                    .ok_or_else(|| ThemeError(format!("{name}: `kind` must be dark or light")))?;
                Self::blank(name, kind)
            }
        };
        if file.extends.is_none() || file.kind.is_some() {
            theme.syntax = default_syntax(theme.kind);
        }
        if let Some(syntax) = &file.syntax {
            theme.syntax = syntax_theme(syntax)
                .ok_or_else(|| ThemeError(format!("{name}: unknown syntax theme `{syntax}`")))?;
        }
        let mut set = BTreeSet::new();
        for (token, value) in &file.colors {
            let lookup = file.palette.get(value).unwrap_or(value);
            let color = parse_color(lookup).ok_or_else(|| {
                ThemeError(format!(
                    "{name}: `{token} = \"{value}\"` is not a color or a palette name"
                ))
            })?;
            let slot = theme
                .token_mut(token)
                .ok_or_else(|| ThemeError(format!("{name}: unknown token `{token}`")))?;
            *slot = color;
            set.insert(token.as_str());
        }
        if file.extends.is_none() {
            let missing: Vec<&str> = TOKENS
                .iter()
                .map(|(t, _)| *t)
                .filter(|t| !set.contains(t))
                .collect();
            if !missing.is_empty() {
                return Err(ThemeError(format!(
                    "{name}: missing {}. Set them or add `extends = \"{}\"`",
                    missing.join(", "),
                    match theme.kind {
                        Kind::Dark => DEFAULT_DARK,
                        Kind::Light => DEFAULT_LIGHT,
                    }
                )));
            }
        }
        Ok(theme)
    }

    pub fn detect_mode() -> ColorMode {
        if std::env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty()) {
            ColorMode::Mono
        } else if truecolor_supported() {
            ColorMode::TrueColor
        } else {
            ColorMode::Indexed
        }
    }

    pub fn with_mode(self, mode: ColorMode) -> Self {
        let mut theme = match mode {
            ColorMode::TrueColor => self,
            ColorMode::Indexed => self.map_colors(to_indexed),
            ColorMode::Mono => self.map_colors(|_| Color::Reset),
        };
        theme.mode = mode;
        theme
    }

    pub fn with_glyphs(mut self, glyphs: Glyphs) -> Self {
        self.glyphs = glyphs;
        self
    }

    pub fn adapt(&self, color: Color) -> Color {
        match self.mode {
            ColorMode::TrueColor => color,
            ColorMode::Indexed => to_indexed(color),
            ColorMode::Mono => Color::Reset,
        }
    }

    pub fn rgb(&self, r: u8, g: u8, b: u8) -> Color {
        self.adapt(Color::Rgb(r, g, b))
    }

    pub fn syntax_theme(&self) -> &'static syntect::highlighting::Theme {
        SYNTAX_THEMES.get(self.syntax)
    }

    pub fn paints_background(&self) -> bool {
        self.bg != Color::Reset
    }

    pub fn background(&self) -> Style {
        Style::new().fg(self.fg).bg(self.bg)
    }

    pub fn text(&self) -> Style {
        Style::new().fg(self.fg)
    }

    pub fn muted(&self) -> Style {
        Style::new().fg(self.fg_muted)
    }

    pub fn faint(&self) -> Style {
        Style::new().fg(self.fg_faint)
    }

    pub fn accent(&self) -> Style {
        Style::new().fg(self.accent)
    }

    pub fn heading_color(&self, level: u8) -> Color {
        match level {
            ..=1 => self.h1,
            2 => self.h2,
            3 => self.h3,
            4 => self.h4,
            5 => self.h5,
            _ => self.h6,
        }
    }

    pub fn heading(&self, level: u8) -> Style {
        match level {
            1 => Style::new()
                .fg(self.h1_fg)
                .bg(self.h1)
                .add_modifier(Modifier::BOLD),
            _ => self.heading_text(level),
        }
    }

    pub fn heading_text(&self, level: u8) -> Style {
        let style = Style::new()
            .fg(self.heading_color(level))
            .add_modifier(Modifier::BOLD);
        if level >= 6 {
            style.add_modifier(Modifier::ITALIC)
        } else {
            style
        }
    }

    pub fn heading_rule(&self) -> Style {
        Style::new().fg(self.h2)
    }

    pub fn code_inline(&self) -> Style {
        Style::new().fg(self.code_fg).bg(self.code_bg)
    }

    pub fn code_block(&self) -> Style {
        Style::new().fg(self.fg).bg(self.code_bg)
    }

    pub fn code_border(&self) -> Style {
        Style::new().fg(self.code_border)
    }

    pub fn link(&self) -> Style {
        Style::new()
            .fg(self.link)
            .add_modifier(Modifier::UNDERLINED)
    }

    pub fn link_url(&self) -> Style {
        Style::new().fg(self.link_url)
    }

    pub fn quote(&self) -> Style {
        Style::new()
            .fg(self.quote_fg)
            .add_modifier(Modifier::ITALIC)
    }

    pub fn quote_bar(&self) -> Style {
        Style::new().fg(self.quote_bar)
    }

    pub fn rule(&self) -> Style {
        Style::new().fg(self.rule)
    }

    pub fn table_border(&self) -> Style {
        Style::new().fg(self.table_border)
    }

    pub fn table_header(&self) -> Style {
        Style::new()
            .fg(self.fg)
            .bg(self.surface)
            .add_modifier(Modifier::BOLD)
    }

    pub fn table_band(&self) -> Style {
        Style::new().bg(self.surface)
    }

    pub fn list_marker(&self) -> Style {
        Style::new().fg(self.list_marker)
    }

    pub fn list_marker_at(&self, depth: usize) -> Style {
        let colors = [self.list_marker, self.h2, self.h3];
        Style::new().fg(colors[depth % colors.len()])
    }

    pub fn label(&self) -> Style {
        Style::new().fg(self.fg_muted).bg(self.label_bg)
    }

    pub fn label_value(&self) -> Style {
        Style::new().fg(self.fg).bg(self.label_value_bg)
    }

    pub fn label_colored(&self, (r, g, b): (u8, u8, u8)) -> Style {
        let luminance =
            (0.2126 * f64::from(r) + 0.7152 * f64::from(g) + 0.0722 * f64::from(b)) / 255.0;
        let (dark_text, light_text) = match self.kind {
            Kind::Dark => (self.h1_fg, self.fg),
            Kind::Light => (self.fg, self.h1_fg),
        };
        let fg = if luminance > 0.55 {
            dark_text
        } else {
            light_text
        };
        Style::new().fg(fg).bg(self.rgb(r, g, b))
    }

    pub fn footnote(&self) -> Style {
        Style::new().fg(self.accent)
    }

    pub fn alert(&self, alert: Alert) -> Color {
        match alert {
            Alert::Note => self.alert_note,
            Alert::Tip => self.alert_tip,
            Alert::Important => self.alert_important,
            Alert::Warning => self.alert_warning,
            Alert::Caution => self.alert_caution,
        }
    }

    pub fn alert_bar(&self, alert: Alert) -> Style {
        Style::new().fg(self.alert(alert))
    }

    pub fn alert_title(&self, alert: Alert) -> Style {
        Style::new()
            .fg(self.alert(alert))
            .add_modifier(Modifier::BOLD)
    }

    pub fn math(&self) -> Style {
        Style::new().fg(self.math).add_modifier(Modifier::ITALIC)
    }

    pub fn status_chip(&self) -> Style {
        Style::new()
            .fg(self.h1_fg)
            .bg(self.accent)
            .add_modifier(Modifier::BOLD)
    }

    pub fn status(&self) -> Style {
        Style::new().fg(self.status_fg).bg(self.status_bg)
    }

    pub fn search(&self, current: bool) -> Style {
        let bg = if current {
            self.search_current_bg
        } else {
            self.search_bg
        };
        Style::new().fg(self.search_fg).bg(bg)
    }

    pub fn overlay_border(&self) -> Style {
        Style::new().fg(self.overlay_border)
    }

    pub fn selected(&self) -> Style {
        Style::new()
            .bg(self.selected_bg)
            .add_modifier(Modifier::BOLD)
    }
}

pub fn parse_color(value: &str) -> Option<Color> {
    let value = value.trim();
    match value.to_ascii_lowercase().as_str() {
        "default" | "none" | "reset" => return Some(Color::Reset),
        _ => {}
    }
    if let Some(hex) = value.strip_prefix('#')
        && hex.len() == 3
        && hex.chars().all(|c| c.is_ascii_hexdigit())
    {
        let digit = |i: usize| u8::from_str_radix(&hex[i..=i], 16).ok().map(|v| v * 17);
        return Some(Color::Rgb(digit(0)?, digit(1)?, digit(2)?));
    }
    Color::from_str(value).ok()
}

pub fn syntax_theme(name: &str) -> Option<EmbeddedThemeName> {
    let wanted = normalize(name);
    EmbeddedLazyThemeSet::theme_names()
        .iter()
        .copied()
        .find(|t| normalize(t.as_name()) == wanted)
}

pub fn syntax_theme_names() -> Vec<String> {
    EmbeddedLazyThemeSet::theme_names()
        .iter()
        .map(|t| normalize(t.as_name()))
        .collect()
}

fn normalize(name: &str) -> String {
    let mut out = String::new();
    for c in name.chars() {
        if c.is_ascii_alphanumeric() {
            out.push(c.to_ascii_lowercase());
        } else if !out.is_empty() && !out.ends_with('-') {
            out.push('-');
        }
    }
    out.trim_end_matches('-').to_string()
}

/// The generated half of `docs/themes.md`: built-in themes, tokens and syntax theme names.
pub fn markdown() -> String {
    let mut out = String::from(
        "## Built-in themes\n\n| Theme | Kind | Code | About |\n| --- | --- | --- | --- |\n",
    );
    for (name, text) in BUILTIN {
        let theme = Theme::builtin(name).expect("built-in themes parse");
        let about = text
            .lines()
            .take_while(|l| l.starts_with('#'))
            .map(|l| l.trim_start_matches('#').trim())
            .collect::<Vec<_>>()
            .join(" ");
        out.push_str(&format!(
            "| `{name}` | {} | `{}` | {about} |\n",
            theme.kind,
            normalize(theme.syntax.as_name())
        ));
    }
    out.push_str(concat!(
        "\n## Tokens\n\n",
        "Every token a theme sets, in the order `mido-dark.toml` lists them. A theme without ",
        "`extends` must set all of them.\n\n",
        "| Token | Paints |\n| --- | --- |\n",
    ));
    for (token, what) in TOKENS {
        out.push_str(&format!("| `{token}` | {what} |\n"));
    }
    out.push_str("\n## Syntax themes\n\n`syntax` takes one of these names, the set bat ships:\n\n");
    let names: Vec<String> = syntax_theme_names()
        .iter()
        .map(|n| format!("`{n}`"))
        .collect();
    out.push_str(&names.join(", "));
    out.push('\n');
    out
}

fn default_syntax(kind: Kind) -> EmbeddedThemeName {
    match kind {
        Kind::Dark => EmbeddedThemeName::CatppuccinMocha,
        Kind::Light => EmbeddedThemeName::CatppuccinLatte,
    }
}

pub fn truecolor_supported() -> bool {
    std::env::var("COLORTERM").is_ok_and(|v| matches!(v.as_str(), "truecolor" | "24bit"))
}

pub fn to_indexed(color: Color) -> Color {
    let Color::Rgb(r, g, b) = color else {
        return color;
    };
    if r == g && g == b {
        return Color::Indexed(match r {
            0..=4 => 16,
            243.. => 231,
            v => 232 + ((v as u16 - 8) * 24 / 240).min(23) as u8,
        });
    }
    let q = |v: u8| -> u8 {
        match v {
            0..=47 => 0,
            48..=114 => 1,
            v => ((v as u16 - 35) / 40) as u8,
        }
    };
    Color::Indexed(16 + 36 * q(r) + 6 * q(g) + q(b))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn every_builtin_theme_defines_every_token() {
        for (name, text) in BUILTIN {
            let file: ThemeFile = toml::from_str(text).unwrap();
            assert!(file.extends.is_none(), "{name} stands alone");
            let theme = Theme::parse(name, text, &|_| None).unwrap_or_else(|e| panic!("{e}"));
            assert_eq!(theme.name, *name);
            for (token, _) in TOKENS {
                assert!(file.colors.contains_key(*token), "{name} sets {token}");
            }
        }
    }

    #[test]
    fn builtin_kinds_match_their_backgrounds() {
        for (name, _) in BUILTIN {
            let theme = Theme::builtin(name).unwrap();
            if let Color::Rgb(r, g, b) = theme.fg {
                let light_text = u32::from(r) + u32::from(g) + u32::from(b) > 3 * 128;
                assert_eq!(
                    light_text,
                    theme.kind == Kind::Dark,
                    "{name} fg suits its kind"
                );
            }
        }
    }

    #[test]
    fn a_theme_can_extend_another() {
        let text = "schema = 1\nextends = \"mido-dark\"\n[palette]\nrose = \"#f5c2e7\"\n[colors]\naccent = \"rose\"\nlink = \"#abc\"\n";
        let theme = Theme::parse("mine", text, &|n| Theme::builtin(n)).unwrap();
        assert_eq!(theme.name, "mine");
        assert_eq!(theme.accent, Color::Rgb(245, 194, 231));
        assert_eq!(theme.link, Color::Rgb(170, 187, 204));
        assert_eq!(theme.fg, Theme::dark().fg);
        assert_eq!(theme.kind, Kind::Dark);
    }

    #[test]
    fn theme_errors_name_the_problem() {
        let base = |n: &str| Theme::builtin(n);
        let err = |text: &str| Theme::parse("t", text, &base).unwrap_err().0;
        assert!(err("schema = 2\nkind = \"dark\"").contains("schema 2"));
        assert!(err("schema = 1\nkind = \"dark\"").contains("missing bg, fg"));
        assert!(
            err("schema = 1\nextends = \"mido-dark\"\n[colors]\nfgg = \"red\"")
                .contains("unknown token `fgg`")
        );
        assert!(
            err("schema = 1\nextends = \"mido-dark\"\n[colors]\nfg = \"rose\"")
                .contains("not a color")
        );
        assert!(err("schema = 1\nextends = \"nope\"").contains("extends `nope`"));
        assert!(
            err("schema = 1\nextends = \"mido-dark\"\nsyntax = \"nope\"").contains("syntax theme")
        );
    }

    #[test]
    fn colors_parse_in_every_form() {
        assert_eq!(parse_color("#ff8000"), Some(Color::Rgb(255, 128, 0)));
        assert_eq!(parse_color("#f80"), Some(Color::Rgb(255, 136, 0)));
        assert_eq!(parse_color("default"), Some(Color::Reset));
        assert_eq!(parse_color("bright-blue"), Some(Color::LightBlue));
        assert_eq!(parse_color("208"), Some(Color::Indexed(208)));
        assert_eq!(parse_color("#ggg"), None);
    }

    #[test]
    fn syntax_names_match_loosely() {
        assert_eq!(
            syntax_theme("catppuccin-mocha"),
            Some(EmbeddedThemeName::CatppuccinMocha)
        );
        assert_eq!(
            syntax_theme("Solarized (dark)"),
            Some(EmbeddedThemeName::SolarizedDark)
        );
        assert_eq!(
            syntax_theme("solarized-light"),
            Some(EmbeddedThemeName::SolarizedLight)
        );
        assert!(syntax_theme_names().contains(&"gruvbox-dark".to_string()));
    }
}