Skip to content

Commit 2ecb5b3

Browse files
committed
Init compiler error messages
1 parent ff51933 commit 2ecb5b3

File tree

2 files changed

+163
-5
lines changed

2 files changed

+163
-5
lines changed

src/colors.rs

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
//! Implements ANSI foreground colors for printable types
2+
3+
use core::marker::PhantomData;
4+
5+
/// Wrapper struct for generically selecting a color over something to be printed
6+
pub struct Styled<'a, T: Sized, C> {
7+
/// Original data to stylize
8+
original: &'a T,
9+
10+
/// Color of the text
11+
color: PhantomData<C>,
12+
}
13+
14+
/// Trait which provides the ANSI color string
15+
pub trait Color {
16+
/// The ANSI color string for this color
17+
const ANSI: &'static str;
18+
}
19+
20+
/// Creates structs for each ANSI color and implemented the [`Color`] trait for them
21+
macro_rules! create_color {
22+
($color:ident, $bold:expr, $num:expr) => {
23+
pub struct $color;
24+
25+
impl Color for $color {
26+
const ANSI: &'static str =
27+
concat!("\x1b[", stringify!($bold), ";", stringify!($num), "m");
28+
}
29+
};
30+
}
31+
32+
create_color!(Black, 0, 30);
33+
create_color!(Red, 0, 31);
34+
create_color!(Green, 0, 32);
35+
create_color!(Yellow, 0, 33);
36+
create_color!(Blue, 0, 34);
37+
create_color!(Magenta, 0, 35);
38+
create_color!(Cyan, 0, 36);
39+
create_color!(White, 0, 37);
40+
create_color!(Normal, 0, 39);
41+
create_color!(BrightBlack, 0, 90);
42+
create_color!(BrightRed, 0, 91);
43+
create_color!(BrightGreen, 0, 92);
44+
create_color!(BrightYellow, 0, 93);
45+
create_color!(BrightBlue, 0, 94);
46+
create_color!(BrightMagenta, 0, 95);
47+
create_color!(BrightCyan, 0, 96);
48+
create_color!(BrightWhite, 0, 97);
49+
create_color!(BoldBlack, 1, 30);
50+
create_color!(BoldRed, 1, 31);
51+
create_color!(BoldGreen, 1, 32);
52+
create_color!(BoldYellow, 1, 33);
53+
create_color!(BoldBlue, 1, 34);
54+
create_color!(BoldMagenta, 1, 35);
55+
create_color!(BoldCyan, 1, 36);
56+
create_color!(BoldWhite, 1, 37);
57+
create_color!(BoldNormal, 1, 39);
58+
create_color!(BoldBrightBlack, 1, 90);
59+
create_color!(BoldBrightRed, 1, 91);
60+
create_color!(BoldBrightGreen, 1, 92);
61+
create_color!(BoldBrightYellow, 1, 93);
62+
create_color!(BoldBrightBlue, 1, 94);
63+
create_color!(BoldBrightMagenta, 1, 95);
64+
create_color!(BoldBrightCyan, 1, 96);
65+
create_color!(BoldBrightWhite, 1, 97);
66+
67+
/// Implements the various `core::fmt` traits
68+
macro_rules! impl_formats {
69+
($ty:path) => {
70+
impl<'a, T: $ty, C: Color> $ty for Styled<'a, T, C> {
71+
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
72+
let _ = f.write_str(C::ANSI);
73+
let _ = <T as $ty>::fmt(&self.original, f);
74+
f.write_str(Normal::ANSI)
75+
}
76+
}
77+
};
78+
}
79+
80+
/// Implements each function that is available for adding color
81+
macro_rules! trait_func {
82+
($color:ident, $ty:ident) => {
83+
fn $color(&self) -> Styled<Self, $ty>
84+
where
85+
Self: Sized,
86+
{
87+
Styled {
88+
original: self,
89+
color: PhantomData,
90+
}
91+
}
92+
};
93+
}
94+
95+
/// Provides wrapper functions to apply foreground colors
96+
pub trait Colorized {
97+
trait_func!(black, Black);
98+
trait_func!(red, Red);
99+
trait_func!(green, Green);
100+
trait_func!(yellow, Yellow);
101+
trait_func!(blue, Blue);
102+
trait_func!(magenta, Magenta);
103+
trait_func!(cyan, Cyan);
104+
trait_func!(white, White);
105+
trait_func!(normal, Normal);
106+
trait_func!(default, Normal);
107+
trait_func!(bright_black, BrightBlack);
108+
trait_func!(bright_red, BrightRed);
109+
trait_func!(bright_green, BrightGreen);
110+
trait_func!(bright_yellow, BrightYellow);
111+
trait_func!(bright_blue, BrightBlue);
112+
trait_func!(bright_magenta, BrightMagenta);
113+
trait_func!(bright_cyan, BrightCyan);
114+
trait_func!(bright_white, BrightWhite);
115+
trait_func!(light_black, BrightBlack);
116+
trait_func!(light_red, BrightRed);
117+
trait_func!(light_green, BrightGreen);
118+
trait_func!(light_yellow, BrightYellow);
119+
trait_func!(light_blue, BrightBlue);
120+
trait_func!(light_magenta, BrightMagenta);
121+
trait_func!(light_cyan, BrightCyan);
122+
trait_func!(light_white, BrightWhite);
123+
trait_func!(bold_black, BoldBlack);
124+
trait_func!(bold_red, BoldRed);
125+
trait_func!(bold_green, BoldGreen);
126+
trait_func!(bold_yellow, BoldYellow);
127+
trait_func!(bold_blue, BoldBlue);
128+
trait_func!(bold_magenta, BoldMagenta);
129+
trait_func!(bold_cyan, BoldCyan);
130+
trait_func!(bold_white, BoldWhite);
131+
trait_func!(bold_normal, BoldNormal);
132+
trait_func!(bold_default, BoldNormal);
133+
trait_func!(bold_bright_black, BoldBrightBlack);
134+
trait_func!(bold_bright_red, BoldBrightRed);
135+
trait_func!(bold_bright_green, BoldBrightGreen);
136+
trait_func!(bold_bright_yellow, BoldBrightYellow);
137+
trait_func!(bold_bright_blue, BoldBrightBlue);
138+
trait_func!(bold_bright_magenta, BoldBrightMagenta);
139+
trait_func!(bold_bright_cyan, BoldBrightCyan);
140+
trait_func!(bold_bright_white, BoldBrightWhite);
141+
trait_func!(bold_light_black, BoldBrightBlack);
142+
trait_func!(bold_light_red, BoldBrightRed);
143+
trait_func!(bold_light_green, BoldBrightGreen);
144+
trait_func!(bold_light_yellow, BoldBrightYellow);
145+
trait_func!(bold_light_blue, BoldBrightBlue);
146+
trait_func!(bold_light_magenta, BoldBrightMagenta);
147+
trait_func!(bold_light_cyan, BoldBrightCyan);
148+
trait_func!(bold_light_white, BoldBrightWhite);
149+
}
150+
151+
impl_formats!(core::fmt::Debug);
152+
impl_formats!(core::fmt::Display);
153+
impl_formats!(core::fmt::LowerHex);
154+
155+
// Magic impl which gives any trait that implements Display color functions
156+
impl<T: core::fmt::Display> Colorized for T {}

src/lib.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2385,6 +2385,8 @@ mod tests {
23852385
result_env: Vec<(std::string::String, NodeId)>) -> Result<(), (EvalError, u32)> {
23862386
let (root, mut ast) = parse_str(input).unwrap();
23872387

2388+
ast.dump_dot(root, "/tmp/dump");
2389+
23882390
let mut env: HashMap<std::string::String, NodeId> = HashMap::new();
23892391
for (key, value) in init_env.iter() {
23902392
env.insert(key.to_string(), *value);
@@ -2404,8 +2406,6 @@ mod tests {
24042406

24052407
}
24062408

2407-
dbg!(&env);
2408-
24092409
let curr_result = ast.nodes[curr_result].clone();
24102410
assert_eq!(curr_result, wanted_result);
24112411

@@ -2415,8 +2415,8 @@ mod tests {
24152415
}
24162416
assert_eq!(env, res_env);
24172417

2418+
24182419
Ok(())
2419-
// ast.dump_dot(result, "/tmp/dump");
24202420
}
24212421

24222422
#[test]
@@ -5151,8 +5151,10 @@ mod tests {
51515151
#[test]
51525152
fn test_eval_nested_where() {
51535153
impl_eval_test_with_env(
5154-
"a + b
5155-
. a = 1 . b = 2",
5154+
"
5155+
a + b
5156+
. a = 1
5157+
. b = 2",
51565158
vec![ ],
51575159
Node::Int { data: 3 },
51585160
vec![

0 commit comments

Comments
 (0)