render_tree/stylesheet/
color.rs

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
use std::fmt;
use std::str::FromStr;
use termcolor;

#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub enum Color {
    Black,
    Blue,
    Green,
    Red,
    Cyan,
    Magenta,
    Yellow,
    White,
}

impl fmt::Display for Color {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let out = match self {
            Color::Black => "black",
            Color::Blue => "blue",
            Color::Green => "green",
            Color::Red => "red",
            Color::Cyan => "cyan",
            Color::Magenta => "magenta",
            Color::Yellow => "yellow",
            Color::White => "white",
        };

        write!(f, "{}", out)
    }
}

impl From<Color> for termcolor::Color {
    fn from(color: Color) -> termcolor::Color {
        match color {
            Color::Black => termcolor::Color::Black,
            Color::Blue => if cfg!(windows) {
                termcolor::Color::Cyan
            } else {
                termcolor::Color::Blue
            },
            Color::Green => termcolor::Color::Green,
            Color::Red => termcolor::Color::Red,
            Color::Cyan => termcolor::Color::Cyan,
            Color::Magenta => termcolor::Color::Magenta,
            Color::Yellow => termcolor::Color::Yellow,
            Color::White => termcolor::Color::White,
        }
    }
}

impl FromStr for Color {
    type Err = (&'static str, String);

    fn from_str(s: &str) -> Result<Color, (&'static str, String)> {
        match &*s.to_lowercase() {
            "black" => Ok(Color::Black),
            "blue" => Ok(Color::Blue),
            "green" => Ok(Color::Green),
            "red" => Ok(Color::Red),
            "cyan" => Ok(Color::Cyan),
            "magenta" => Ok(Color::Magenta),
            "yellow" => Ok(Color::Yellow),
            "white" => Ok(Color::White),
            _ => Err(("invalid color", s.to_string())),
        }
    }
}

impl<'a> From<&'a str> for Color {
    fn from(s: &str) -> Color {
        Color::from_str(s).unwrap()
    }
}

impl<'a> From<&'a termcolor::Color> for Color {
    fn from(color: &'a termcolor::Color) -> Color {
        match color {
            termcolor::Color::Black => Color::Black,
            termcolor::Color::Blue => Color::Blue,
            termcolor::Color::Green => Color::Green,
            termcolor::Color::Red => Color::Red,
            termcolor::Color::Cyan => Color::Cyan,
            termcolor::Color::Magenta => Color::Magenta,
            termcolor::Color::Yellow => Color::Yellow,
            termcolor::Color::White => Color::White,

            other => panic!(
                "termcolor {:?} is a non-portable color and cannot be converted",
                other
            ),
        }
    }
}