From fe6573b2deb43c3a00dae317ceaf84bc3797ce8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Sat, 3 May 2025 21:55:06 +0200 Subject: [PATCH] feat(error): display location of error --- notes.org | 9 ++++++++- src/error_display.rs | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/notes.org b/notes.org index e1d1f54..852f60e 100644 --- a/notes.org +++ b/notes.org @@ -29,7 +29,14 @@ CLOCK: [2025-05-03 sam. 21:28]--[2025-05-03 sam. 21:30] => 0:02 :LOGBOOK: CLOCK: [2025-05-03 sam. 21:30]--[2025-05-03 sam. 21:50] => 0:20 :END: -** TODO error display should include a span to show where the error occured +** DONE error display should include a span to show where the error occured +:PROPERTIES: +:EFFORT: 10 +:END: +:LOGBOOK: +CLOCK: [2025-05-03 sam. 21:51]--[2025-05-03 sam. 21:54] => 0:03 +:END: +** TODO deduplicate error display code * DONE snapshot testing ** DONE Find the snapshot testing library diff --git a/src/error_display.rs b/src/error_display.rs index e3819bb..ffaae9e 100644 --- a/src/error_display.rs +++ b/src/error_display.rs @@ -2,7 +2,7 @@ use crate::{ command::CommandParseError, meta_commands::MetaCommandParseError, statements::StatementParseError, }; -use ariadne::{Report, ReportKind, Source}; +use ariadne::{Color, Label, Report, ReportKind, Source}; pub trait OSDBError { fn display(&self, file: &str, input: &str); @@ -10,8 +10,13 @@ pub trait OSDBError { impl OSDBError for MetaCommandParseError { fn display(&self, file: &str, input: &str) { - Report::build(ReportKind::Error, (file, 0..input.len())) + Report::build(ReportKind::Error, (file, 0..input.len() - 1)) .with_message(format!("{self}")) + .with_label( + Label::new((file, 0..input.len() - 1)) + .with_color(Color::Red) + .with_message("here"), + ) .finish() .print((file, Source::from(input))) .unwrap(); @@ -20,8 +25,13 @@ impl OSDBError for MetaCommandParseError { impl OSDBError for StatementParseError { fn display(&self, file: &str, input: &str) { - Report::build(ReportKind::Error, (file, 0..input.len())) + Report::build(ReportKind::Error, (file, 0..input.len() - 1)) .with_message(format!("{self}")) + .with_label( + Label::new((file, 0..input.len() - 1)) + .with_color(Color::Red) + .with_message("here"), + ) .finish() .print((file, Source::from(input))) .unwrap();