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
#[allow(unused_imports)]
use crate::{
    AnsiColors, BgDynColorDisplay, CssColors, DynColor, FgDynColorDisplay, Rgb, XtermColors,
};
use core::fmt;

/// An enum describing runtime-configurable colors which can be displayed using [`FgDynColorDisplay`](FgDynColorDisplay)
/// or [`BgDynColorDisplay`](BgDynColorDisplay), allowing for multiple types of colors to be used
/// at runtime.
#[allow(missing_docs)]
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
pub enum DynColors {
    Ansi(AnsiColors),
    Css(CssColors),
    Xterm(XtermColors),
    Rgb(u8, u8, u8),
}

impl DynColor for DynColors {
    fn fmt_ansi_fg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DynColors::Ansi(ansi) => ansi.fmt_ansi_fg(f),
            DynColors::Css(css) => css.fmt_ansi_fg(f),
            DynColors::Xterm(xterm) => xterm.fmt_ansi_fg(f),
            &DynColors::Rgb(r, g, b) => Rgb(r, g, b).fmt_ansi_fg(f),
        }
    }

    fn fmt_ansi_bg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DynColors::Ansi(ansi) => ansi.fmt_ansi_bg(f),
            DynColors::Css(css) => css.fmt_ansi_bg(f),
            DynColors::Xterm(xterm) => xterm.fmt_ansi_bg(f),
            &DynColors::Rgb(r, g, b) => Rgb(r, g, b).fmt_ansi_bg(f),
        }
    }

    fn fmt_raw_ansi_fg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DynColors::Ansi(ansi) => ansi.fmt_raw_ansi_fg(f),
            DynColors::Css(css) => css.fmt_raw_ansi_fg(f),
            DynColors::Xterm(xterm) => xterm.fmt_raw_ansi_fg(f),
            &DynColors::Rgb(r, g, b) => Rgb(r, g, b).fmt_raw_ansi_fg(f),
        }
    }

    fn fmt_raw_ansi_bg(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DynColors::Ansi(ansi) => ansi.fmt_raw_ansi_bg(f),
            DynColors::Css(css) => css.fmt_raw_ansi_bg(f),
            DynColors::Xterm(xterm) => xterm.fmt_raw_ansi_bg(f),
            &DynColors::Rgb(r, g, b) => Rgb(r, g, b).fmt_raw_ansi_bg(f),
        }
    }

    #[doc(hidden)]
    fn get_dyncolors_fg(&self) -> crate::DynColors {
        *self
    }

    #[doc(hidden)]
    fn get_dyncolors_bg(&self) -> crate::DynColors {
        *self
    }
}

/// An error for when the color can not be parsed from a string at runtime
#[derive(Debug)]
pub struct ParseColorError;

impl core::str::FromStr for DynColors {
    type Err = ParseColorError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        if s.chars().next().ok_or(ParseColorError)? == '#' {
            match s.len() {
                4 => {
                    // TODO
                    Err(ParseColorError)
                }
                7 => Ok(Self::Rgb(
                    u8::from_str_radix(&s[1..3], 16).or(Err(ParseColorError))?,
                    u8::from_str_radix(&s[3..5], 16).or(Err(ParseColorError))?,
                    u8::from_str_radix(&s[5..7], 16).or(Err(ParseColorError))?,
                )),
                _ => Err(ParseColorError),
            }
        } else {
            let ansi = match s {
                "black" => AnsiColors::Black,
                "red" => AnsiColors::Red,
                "green" => AnsiColors::Green,
                "yellow" => AnsiColors::Yellow,
                "blue" => AnsiColors::Blue,
                "magenta" | "purple" => AnsiColors::Magenta,
                "cyan" => AnsiColors::Cyan,
                "white" => AnsiColors::White,
                "bright black" => AnsiColors::BrightBlack,
                "bright red" => AnsiColors::BrightRed,
                "bright green" => AnsiColors::BrightGreen,
                "bright yellow" => AnsiColors::BrightYellow,
                "bright blue" => AnsiColors::BrightBlue,
                "bright magenta" => AnsiColors::BrightMagenta,
                "bright cyan" => AnsiColors::BrightCyan,
                "bright white" => AnsiColors::BrightWhite,
                _ => return Err(ParseColorError),
            };

            Ok(Self::Ansi(ansi))
        }
    }
}