Skip to main content

swears/
color.rs

1use std::fmt::Display;
2
3#[derive(Clone, Copy, Debug)]
4pub enum Color {
5    Yellow,
6    Magenta,
7    Default,
8    Cyan,
9    Black,
10    White,
11    Red,
12    Blue,
13    Green,
14}
15
16#[derive(Clone, Debug)]
17pub struct ColorString(pub Color, pub String);
18
19impl ColorString {
20    pub fn to_color(&self) -> Color {
21        let Self(color, _) = self;
22        *color
23    }
24}
25
26impl ToString for ColorString {
27    fn to_string(&self) -> String {
28        let Self(_, s) = self;
29        s.clone()
30    }
31}
32
33impl Default for ColorString {
34    fn default() -> Self {
35        Self(Color::Default, String::new())
36    }
37}
38
39impl<T: Display> From<T> for ColorString {
40    fn from(t: T) -> Self {
41        Self(Color::Default, t.to_string())
42    }
43}
44
45pub trait Colorize {
46    fn red(self) -> ColorString;
47    fn blue(self) -> ColorString;
48    fn green(self) -> ColorString;
49    fn yellow(self) -> ColorString;
50    fn magenta(self) -> ColorString;
51    fn cyan(self) -> ColorString;
52    fn black(self) -> ColorString;
53    fn white(self) -> ColorString;
54    fn default(self) -> ColorString;
55}
56
57impl<T: Display> Colorize for T {
58    fn red(self) -> ColorString {
59        ColorString(Color::Red, self.to_string())
60    }
61
62    fn blue(self) -> ColorString {
63        ColorString(Color::Blue, self.to_string())
64    }
65
66    fn green(self) -> ColorString {
67        ColorString(Color::Green, self.to_string())
68    }
69
70    fn yellow(self) -> ColorString {
71        ColorString(Color::Yellow, self.to_string())
72    }
73
74    fn magenta(self) -> ColorString {
75        ColorString(Color::Magenta, self.to_string())
76    }
77
78    fn cyan(self) -> ColorString {
79        ColorString(Color::Cyan, self.to_string())
80    }
81
82    fn black(self) -> ColorString {
83        ColorString(Color::Black, self.to_string())
84    }
85
86    fn white(self) -> ColorString {
87        ColorString(Color::White, self.to_string())
88    }
89
90    fn default(self) -> ColorString {
91        ColorString(Color::Default, self.to_string())
92    }
93}