diff --git a/notes.org b/notes.org index 36210c4..0a535bf 100644 --- a/notes.org +++ b/notes.org @@ -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 :END: -*** TODO ScanErrors must be convertible to ariadne reports +*** DONE ScanErrors must be convertible to ariadne reports :PROPERTIES: :EFFORT: 10 :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 :END: -**** TODO implement OSDBError for ScanError +**** DONE implement OSDBError for ScanError :PROPERTIES: :EFFORT: 10 :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 :PROPERTIES: diff --git a/src/error_display.rs b/src/error_display.rs index f283b8d..838acd4 100644 --- a/src/error_display.rs +++ b/src/error_display.rs @@ -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::>::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(); diff --git a/src/tokens.rs b/src/tokens.rs index 404d10c..9a88df1 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -25,6 +25,15 @@ pub struct Location { pub length: usize, } +impl From<&Location> for std::ops::Range { + fn from(val: &Location) -> Self { + std::ops::Range { + start: val.offset, + end: val.offset + val.length, + } + } +} + impl Location { /// ``` /// use osdb::tokens::Location;