refactor(error): deduplicate error display code
This commit is contained in:
parent
fe6573b2de
commit
825511a515
2 changed files with 15 additions and 40 deletions
|
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue