Skip to main content

rucc_rules/
error.rs

1//! What a malformed rule file is told.
2
3use std::fmt;
4
5/// A place in a rule file and what is wrong there.
6///
7/// The compiler's own diagnostics carry a code, a caret and a source map, and none of that is
8/// wanted here. A rule file is read by `cargo xtask rules` and its errors are read by whoever
9/// is editing the rules, in a build log, so the shape that belongs here is the one every other
10/// build tool prints: the file, the position, and one sentence. Spending a number out of the
11/// compiler's diagnostic sequence on it would also be wrong, because those numbers are part of
12/// what a user of the compiler can look up, and nobody compiling a C program will ever see one
13/// of these.
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub struct Error {
16    /// The file the rule was read from, as it should be printed.
17    pub path: String,
18    /// The line, counted from one.
19    pub line: u32,
20    /// The column, counted from one, in characters.
21    pub column: u32,
22    /// What is wrong, as a sentence with no trailing full stop.
23    pub message: String,
24}
25
26impl fmt::Display for Error {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        write!(f, "{}:{}:{}: {}", self.path, self.line, self.column, self.message)
29    }
30}
31
32impl std::error::Error for Error {}