Skip to main content

windows_rdl/
error.rs

1/// An error produced while reading, writing, or formatting RDL.
2pub struct Error {
3    /// Human-readable description of what went wrong.
4    pub message: String,
5    /// Name of the file in which the error occurred, if known.
6    pub file_name: String,
7    /// Line number of the error, or `0` if not applicable.
8    pub line: usize,
9    /// Zero-based column number of the error, or `0` if not applicable.
10    pub column: usize,
11}
12
13impl Error {
14    /// Creates a new error with the given message and source location.
15    pub fn new(message: &str, file_name: &str, line: usize, column: usize) -> Self {
16        Self {
17            message: message.to_string(),
18            file_name: file_name.to_string(),
19            line,
20            column,
21        }
22    }
23}
24
25impl std::error::Error for Error {}
26
27impl std::fmt::Debug for Error {
28    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
29        std::fmt::Display::fmt(self, f)
30    }
31}
32
33impl std::fmt::Display for Error {
34    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
35        if self.line != 0 || self.column != 0 {
36            write!(
37                f,
38                "\nerror: {}\n --> {}:{}:{}",
39                self.message,
40                self.file_name,
41                self.line,
42                self.column + 1
43            )
44        } else if self.file_name.is_empty() {
45            write!(f, "\nerror: {}", self.message)
46        } else {
47            write!(f, "\nerror: {}\n --> {}", self.message, self.file_name)
48        }
49    }
50}