osdb/src/error_display.rs

31 lines
998 B
Rust
Raw Normal View History

use crate::{command::CommandParseError, tokens::ScanError};
2025-05-03 21:55:06 +02:00
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) {
let CommandParseError::Scan(x) = self;
x.display(file, input);
}
}
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}")),
)
.with_help("Make sure you don't have any typos or unexpected characters.")
.finish()
.print((file, Source::from(input)))
.unwrap();
}
}