garden_lang_parser/
diagnostics.rs

1use owo_colors::OwoColorize as _;
2
3#[derive(Debug, Clone)]
4pub enum MessagePart {
5    Text(String),
6    Code(String),
7}
8
9/// A macro that wraps Text(format!("foo")) to make it easier to write
10/// error messages.
11#[macro_export]
12macro_rules! msgtext {
13    ($($arg:tt)*) => {
14        $crate::diagnostics::MessagePart::Text(format!($($arg)*))
15    };
16}
17
18/// A macro that wraps Code(format!("foo")) to make it easier to write
19/// error messages.
20#[macro_export]
21macro_rules! msgcode {
22    ($($arg:tt)*) => {
23        $crate::diagnostics::MessagePart::Code(format!($($arg)*))
24    };
25}
26
27#[derive(Debug, Clone)]
28pub struct ErrorMessage(pub Vec<MessagePart>);
29
30impl ErrorMessage {
31    pub fn as_string(&self) -> String {
32        let mut s = String::new();
33        for message_part in &self.0 {
34            match message_part {
35                MessagePart::Text(t) => s.push_str(t),
36                MessagePart::Code(c) => s.push_str(&format!("`{}`", c)),
37            }
38        }
39
40        s
41    }
42
43    pub fn as_styled_string(&self) -> String {
44        let mut s = String::new();
45        for message_part in &self.0 {
46            match message_part {
47                MessagePart::Text(t) => s.push_str(t),
48                MessagePart::Code(c) => s.push_str(&c.bold().to_string()),
49            }
50        }
51
52        s
53    }
54}