feat(errors): display basic errors with ariadne

This commit is contained in:
Khaïs COLIN 2025-05-03 21:31:26 +02:00
parent 4246775db5
commit 51569d3ec2
4 changed files with 52 additions and 7 deletions

23
Cargo.lock generated
View file

@ -2,6 +2,16 @@
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4 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]] [[package]]
name = "console" name = "console"
version = "0.15.11" version = "0.15.11"
@ -47,6 +57,7 @@ checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
name = "osdb" name = "osdb"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"ariadne",
"insta", "insta",
] ]
@ -56,6 +67,12 @@ version = "2.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa" checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa"
[[package]]
name = "unicode-width"
version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
[[package]] [[package]]
name = "windows-sys" name = "windows-sys"
version = "0.59.0" version = "0.59.0"
@ -128,3 +145,9 @@ name = "windows_x86_64_msvc"
version = "0.52.6" version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec" checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"
[[package]]
name = "yansi"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049"

View file

@ -5,6 +5,7 @@ edition = "2024"
authors = ["Khaïs COLIN"] authors = ["Khaïs COLIN"]
[dependencies] [dependencies]
ariadne = "0.5.1"
[dev-dependencies] [dev-dependencies]
insta = "1.43.1" insta = "1.43.1"

View file

@ -22,7 +22,14 @@ CLOCK: [2025-05-03 sam. 21:24]--[2025-05-03 sam. 21:28] => 0:04
:LOGBOOK: :LOGBOOK:
CLOCK: [2025-05-03 sam. 21:28]--[2025-05-03 sam. 21:30] => 0:02 CLOCK: [2025-05-03 sam. 21:28]--[2025-05-03 sam. 21:30] => 0:02
:END: :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 snapshot testing
** DONE Find the snapshot testing library ** 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: :LOGBOOK:
CLOCK: [2025-05-03 sam. 19:06]--[2025-05-03 sam. 19:07] => 0:01 CLOCK: [2025-05-03 sam. 19:06]--[2025-05-03 sam. 19:07] => 0:01
:END: :END:
* TODO switch statement parsing to more extensible token-based algorithm

View file

@ -2,25 +2,37 @@ use crate::{
command::CommandParseError, meta_commands::MetaCommandParseError, command::CommandParseError, meta_commands::MetaCommandParseError,
statements::StatementParseError, statements::StatementParseError,
}; };
use ariadne::{Report, ReportKind, Source};
pub trait OSDBError { pub trait OSDBError {
fn display(&self, file: &str, input: &str); fn display(&self, file: &str, input: &str);
} }
impl OSDBError for MetaCommandParseError { impl OSDBError for MetaCommandParseError {
fn display(&self, file: &str, _input: &str) { fn display(&self, file: &str, input: &str) {
println!("{file}: {self}") Report::build(ReportKind::Error, (file, 0..input.len()))
.with_message(format!("{self}"))
.finish()
.print((file, Source::from(input)))
.unwrap();
} }
} }
impl OSDBError for StatementParseError { impl OSDBError for StatementParseError {
fn display(&self, file: &str, _input: &str) { fn display(&self, file: &str, input: &str) {
println!("{file}: {self}") Report::build(ReportKind::Error, (file, 0..input.len()))
.with_message(format!("{self}"))
.finish()
.print((file, Source::from(input)))
.unwrap();
} }
} }
impl OSDBError for CommandParseError { impl OSDBError for CommandParseError {
fn display(&self, file: &str, _input: &str) { fn display(&self, file: &str, input: &str) {
println!("{file}: {self}") match self {
CommandParseError::MetaCommand(err) => err.display(file, input),
CommandParseError::Statement(err) => err.display(file, input),
}
} }
} }