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

@ -153,7 +153,7 @@ CLOCK: [2025-05-04 dim. 13:27]--[2025-05-04 dim. 13:32] => 0:05
CLOCK: [2025-05-04 dim. 13:35]--[2025-05-04 dim. 13:38] => 0:03 CLOCK: [2025-05-04 dim. 13:35]--[2025-05-04 dim. 13:38] => 0:03
:END: :END:
*** TODO ScanErrors must be convertible to ariadne reports *** DONE ScanErrors must be convertible to ariadne reports
:PROPERTIES: :PROPERTIES:
:EFFORT: 10 :EFFORT: 10
:END: :END:
@ -166,10 +166,13 @@ CLOCK: [2025-05-04 dim. 13:35]--[2025-05-04 dim. 13:38] => 0:03
CLOCK: [2025-05-04 dim. 13:38]--[2025-05-04 dim. 13:44] => 0:06 CLOCK: [2025-05-04 dim. 13:38]--[2025-05-04 dim. 13:44] => 0:06
:END: :END:
**** TODO implement OSDBError for ScanError **** DONE implement OSDBError for ScanError
:PROPERTIES: :PROPERTIES:
:EFFORT: 10 :EFFORT: 10
:END: :END:
:LOGBOOK:
CLOCK: [2025-05-04 dim. 13:45]--[2025-05-04 dim. 13:56] => 0:11
:END:
*** TODO remove token types which are not recognized at all *** TODO remove token types which are not recognized at all
:PROPERTIES: :PROPERTIES:

View file

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