46 lines
1.6 KiB
Rust
46 lines
1.6 KiB
Rust
use crate::{command::CommandParseError, tokens::ScanError};
|
|
use ariadne::{Color, Label, Report, ReportKind, Source};
|
|
|
|
pub trait OSDBError {
|
|
fn display(&self, file: &str, input: &str);
|
|
}
|
|
|
|
impl OSDBError for CommandParseError {
|
|
fn display(&self, file: &str, input: &str) {
|
|
match self {
|
|
CommandParseError::Scan(x) => {
|
|
x.display(file, input);
|
|
}
|
|
CommandParseError::UnexpectedToken(token, items) => {
|
|
let location = (file, Into::<std::ops::Range<usize>>::into(&token.location));
|
|
Report::build(ReportKind::Error, location.clone())
|
|
.with_message("unexpected token")
|
|
.with_label(
|
|
Label::new(location.clone())
|
|
.with_color(Color::Red)
|
|
.with_message(format!("found {token}")),
|
|
)
|
|
.with_note(format!("expected token type to be one of {items:?}"))
|
|
.finish()
|
|
.print((file, Source::from(input)))
|
|
.unwrap()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
impl OSDBError for ScanError {
|
|
fn display(&self, file: &str, input: &str) {
|
|
let location = (file, Into::<std::ops::Range<usize>>::into(&self.location));
|
|
Report::build(ReportKind::Error, location.clone())
|
|
.with_message(format!("{self}"))
|
|
.with_label(
|
|
Label::new(location)
|
|
.with_color(Color::Red)
|
|
.with_message(format!("{self}")),
|
|
)
|
|
.finish()
|
|
.print((file, Source::from(input)))
|
|
.unwrap();
|
|
}
|
|
}
|