Skip to main content

anillo/
error.rs

1pub struct CompilationError {
2    line: u32,
3    column: u32,
4    diagnostic: String,
5    src_available: bool,
6}
7
8impl CompilationError {
9    pub fn new(line: u32, column: u32, diagnostic: String) -> CompilationError {
10        CompilationError {
11            line,
12            column,
13            diagnostic,
14            src_available: true,
15        }
16    }
17
18    pub fn new_without_src_info(diagnostic: String) -> CompilationError {
19        CompilationError {
20            line: 0,
21            column: 0,
22            diagnostic,
23            src_available: false,
24        }
25    }
26}
27
28impl std::fmt::Debug for CompilationError {
29    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
30        const EMPTY: String = String::new();
31        write!(
32            f,
33            "CompilationError: \n{}{}{}{}",
34            if self.src_available {
35                format!("line = {}, ", self.line)
36            } else {
37                EMPTY
38            },
39            if self.src_available {
40                format!("column = {}, ", self.column)
41            } else {
42                EMPTY
43            },
44            self.diagnostic,
45            if self.src_available {
46                ""
47            } else {
48                "\n(line info unavaliable)"
49            }
50        )
51    }
52}
53
54impl std::fmt::Display for CompilationError {
55    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
56        write!(
57            f,
58            "CompilationError: {{line = {}, column = {}, diagnostic = {}}}",
59            self.line, self.column, self.diagnostic
60        )
61    }
62}
63
64impl std::error::Error for CompilationError {
65    fn cause(&self) -> Option<&dyn std::error::Error> {
66        todo!()
67    }
68
69    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
70        todo!()
71    }
72
73    fn description(&self) -> &str {
74        todo!()
75    }
76}