refactor(error): deduplicate error display code

This commit is contained in:
Khaïs COLIN 2025-05-03 21:56:12 +02:00
parent fe6573b2de
commit 825511a515
2 changed files with 15 additions and 40 deletions

View file

@ -1,48 +1,17 @@
use crate::{
command::CommandParseError, meta_commands::MetaCommandParseError,
statements::StatementParseError,
};
use crate::command::CommandParseError;
use ariadne::{Color, Label, Report, ReportKind, Source};
pub trait OSDBError {
fn display(&self, file: &str, input: &str);
}
impl OSDBError for MetaCommandParseError {
fn display(&self, file: &str, input: &str) {
Report::build(ReportKind::Error, (file, 0..input.len() - 1))
.with_message(format!("{self}"))
.with_label(
Label::new((file, 0..input.len() - 1))
.with_color(Color::Red)
.with_message("here"),
)
.finish()
.print((file, Source::from(input)))
.unwrap();
}
}
impl OSDBError for StatementParseError {
fn display(&self, file: &str, input: &str) {
Report::build(ReportKind::Error, (file, 0..input.len() - 1))
.with_message(format!("{self}"))
.with_label(
Label::new((file, 0..input.len() - 1))
.with_color(Color::Red)
.with_message("here"),
)
.finish()
.print((file, Source::from(input)))
.unwrap();
}
}
impl OSDBError for CommandParseError {
fn display(&self, file: &str, input: &str) {
match self {
CommandParseError::MetaCommand(err) => err.display(file, input),
CommandParseError::Statement(err) => err.display(file, input),
}
Report::build(ReportKind::Error, (file, 0..input.len() - 1))
.with_message(format!("{self}"))
.with_label(Label::new((file, 0..input.len() - 1)).with_color(Color::Red))
.finish()
.print((file, Source::from(input)))
.unwrap();
}
}