From 92092ccc245269e0c3a864d06f7e382a5bb9d8b0 Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Thu, 9 Jan 2025 16:26:20 +0100 Subject: Merge all error types into a single one MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit They started with good intentions, but in the end they were all pretty much alike. Hence, it makes sense to simplify everything and provide a single struct. Signed-off-by: Miquel Sabaté Solà --- lib/xixanta/src/lib.rs | 52 +++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) (limited to 'lib/xixanta/src/lib.rs') diff --git a/lib/xixanta/src/lib.rs b/lib/xixanta/src/lib.rs index 95ead9c..2da8fd0 100644 --- a/lib/xixanta/src/lib.rs +++ b/lib/xixanta/src/lib.rs @@ -16,8 +16,58 @@ impl Default for SourceInfo { } } +/// Error provides an interface used throughout this crate in which an error +/// message of type String is located through a line and an associated +/// SourceInfo. If global is set to true, then `line` does not matter. +#[derive(Debug, Clone, PartialEq)] +pub struct Error { + /// Line number where the error was found relative to the `source`. You can + /// ignore this field if `global` is true. + pub line: usize, + + /// Whether the error affects the whole file or not. + pub global: bool, + + /// Information on the file where the error was found. + pub source: SourceInfo, + + /// Human-readable representation of the error. Don't use this field + /// directly, prefer its `std::fmt::Display` implementation. Hence, calling + /// `.to_string()` on it is most probably the way to go. + pub message: String, +} + +impl From for Vec { + fn from(err: Error) -> Self { + vec![err] + } +} + +impl std::error::Error for Error {} + +impl std::fmt::Display for Error { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + if self.global { + if self.source.name.is_empty() { + write!(f, "{}", self.message) + } else { + write!(f, "{} ({})", self.message, self.source.name) + } + } else if self.source.name.is_empty() { + write!(f, "{} (line {})", self.message, self.line + 1) + } else { + write!( + f, + "{} ({}: line {})", + self.message, + self.source.name, + self.line + 1 + ) + } + } +} + pub mod assembler; -pub mod errors; pub mod node; pub mod object; pub mod opcodes; -- cgit v1.2.3