refactor(error): deduplicate error display code

This commit is contained in:
Khaïs COLIN 2025-05-03 21:56:12 +02:00
parent fe6573b2de
commit 825511a515
2 changed files with 15 additions and 40 deletions

View file

@ -1,6 +1,6 @@
#+title: Notes #+title: Notes
* TODO show errors with ariadne * DONE show errors with ariadne
:PROPERTIES: :PROPERTIES:
:EFFORT: 10min :EFFORT: 10min
:END: :END:
@ -36,7 +36,13 @@ CLOCK: [2025-05-03 sam. 21:30]--[2025-05-03 sam. 21:50] => 0:20
:LOGBOOK: :LOGBOOK:
CLOCK: [2025-05-03 sam. 21:51]--[2025-05-03 sam. 21:54] => 0:03 CLOCK: [2025-05-03 sam. 21:51]--[2025-05-03 sam. 21:54] => 0:03
:END: :END:
** TODO deduplicate error display code ** DONE deduplicate error display code
:PROPERTIES:
:EFFORT: 10
:END:
:LOGBOOK:
CLOCK: [2025-05-03 sam. 21:55]--[2025-05-03 sam. 22:01] => 0:06
:END:
* DONE snapshot testing * DONE snapshot testing
** DONE Find the snapshot testing library ** DONE Find the snapshot testing library

View file

@ -1,48 +1,17 @@
use crate::{ use crate::command::CommandParseError;
command::CommandParseError, meta_commands::MetaCommandParseError,
statements::StatementParseError,
};
use ariadne::{Color, Label, Report, ReportKind, Source}; use ariadne::{Color, Label, 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 {
fn display(&self, file: &str, input: &str) {
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();
}
}
impl OSDBError for StatementParseError {
fn display(&self, file: &str, input: &str) {
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();
}
}
impl OSDBError for CommandParseError { impl OSDBError for CommandParseError {
fn display(&self, file: &str, input: &str) { fn display(&self, file: &str, input: &str) {
match self { Report::build(ReportKind::Error, (file, 0..input.len() - 1))
CommandParseError::MetaCommand(err) => err.display(file, input), .with_message(format!("{self}"))
CommandParseError::Statement(err) => err.display(file, input), .with_label(Label::new((file, 0..input.len() - 1)).with_color(Color::Red))
} .finish()
.print((file, Source::from(input)))
.unwrap();
} }
} }