feat(ScanError) implement OSDBError trait

This commit is contained in:
Khaïs COLIN 2025-05-04 13:55:56 +02:00
parent 55b4779964
commit 80cbbab6ef
3 changed files with 34 additions and 6 deletions

View file

@ -1,4 +1,4 @@
use crate::command::CommandParseError;
use crate::{command::CommandParseError, tokens::ScanError};
use ariadne::{Color, Label, Report, ReportKind, Source};
pub trait OSDBError {
@ -7,9 +7,25 @@ pub trait OSDBError {
impl OSDBError for CommandParseError {
fn display(&self, file: &str, input: &str) {
Report::build(ReportKind::Error, (file, 0..input.len() - 1))
.with_message(self.message())
.with_label(Label::new((file, 0..input.len() - 1)).with_color(Color::Red))
if let CommandParseError::Scan(x) = self {
x.display(file, input);
} else {
Report::build(ReportKind::Error, (file, 0..input.len() - 1))
.with_message(self.message())
.with_label(Label::new((file, 0..input.len() - 1)).with_color(Color::Red))
.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))
.finish()
.print((file, Source::from(input)))
.unwrap();

View file

@ -25,6 +25,15 @@ pub struct Location {
pub length: usize,
}
impl From<&Location> for std::ops::Range<usize> {
fn from(val: &Location) -> Self {
std::ops::Range {
start: val.offset,
end: val.offset + val.length,
}
}
}
impl Location {
/// ```
/// use osdb::tokens::Location;