From 51569d3ec2143d715d925544b093ad091034ed8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Sat, 3 May 2025 21:31:26 +0200 Subject: [PATCH] feat(errors): display basic errors with ariadne --- Cargo.lock | 23 +++++++++++++++++++++++ Cargo.toml | 1 + notes.org | 11 ++++++++++- src/error_display.rs | 24 ++++++++++++++++++------ 4 files changed, 52 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1b637c2..35cd7e3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2,6 +2,16 @@ # It is not intended for manual editing. version = 4 +[[package]] +name = "ariadne" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "36f5e3dca4e09a6f340a61a0e9c7b61e030c69fc27bf29d73218f7e5e3b7638f" +dependencies = [ + "unicode-width", + "yansi", +] + [[package]] name = "console" version = "0.15.11" @@ -47,6 +57,7 @@ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" name = "osdb" version = "0.1.0" dependencies = [ + "ariadne", "insta", ] @@ -56,6 +67,12 @@ version = "2.7.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa" +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + [[package]] name = "windows-sys" version = "0.59.0" @@ -128,3 +145,9 @@ name = "windows_x86_64_msvc" version = "0.52.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" + +[[package]] +name = "yansi" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049" diff --git a/Cargo.toml b/Cargo.toml index c53c9f7..6ba1a65 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,7 @@ edition = "2024" authors = ["Khaïs COLIN"] [dependencies] +ariadne = "0.5.1" [dev-dependencies] insta = "1.43.1" diff --git a/notes.org b/notes.org index 00cd0a9..e1d1f54 100644 --- a/notes.org +++ b/notes.org @@ -22,7 +22,14 @@ CLOCK: [2025-05-03 sam. 21:24]--[2025-05-03 sam. 21:28] => 0:04 :LOGBOOK: CLOCK: [2025-05-03 sam. 21:28]--[2025-05-03 sam. 21:30] => 0:02 :END: -** TODO OSDBError::display() should generate ariadne errors and return those +** DONE OSDBError::display() should generate ariadne errors and return those +:PROPERTIES: +:EFFORT: 10 +:END: +: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 snapshot testing ** DONE Find the snapshot testing library @@ -99,3 +106,5 @@ CLOCK: [2025-05-03 sam. 21:21]--[2025-05-03 sam. 21:22] => 0:01 :LOGBOOK: CLOCK: [2025-05-03 sam. 19:06]--[2025-05-03 sam. 19:07] => 0:01 :END: + +* TODO switch statement parsing to more extensible token-based algorithm diff --git a/src/error_display.rs b/src/error_display.rs index 3cc2a3d..e3819bb 100644 --- a/src/error_display.rs +++ b/src/error_display.rs @@ -2,25 +2,37 @@ use crate::{ command::CommandParseError, meta_commands::MetaCommandParseError, statements::StatementParseError, }; +use ariadne::{Report, ReportKind, Source}; pub trait OSDBError { fn display(&self, file: &str, input: &str); } impl OSDBError for MetaCommandParseError { - fn display(&self, file: &str, _input: &str) { - println!("{file}: {self}") + fn display(&self, file: &str, input: &str) { + Report::build(ReportKind::Error, (file, 0..input.len())) + .with_message(format!("{self}")) + .finish() + .print((file, Source::from(input))) + .unwrap(); } } impl OSDBError for StatementParseError { - fn display(&self, file: &str, _input: &str) { - println!("{file}: {self}") + fn display(&self, file: &str, input: &str) { + Report::build(ReportKind::Error, (file, 0..input.len())) + .with_message(format!("{self}")) + .finish() + .print((file, Source::from(input))) + .unwrap(); } } impl OSDBError for CommandParseError { - fn display(&self, file: &str, _input: &str) { - println!("{file}: {self}") + fn display(&self, file: &str, input: &str) { + match self { + CommandParseError::MetaCommand(err) => err.display(file, input), + CommandParseError::Statement(err) => err.display(file, input), + } } }