From 55b4779964b30069078a01a1b3ae83e00ff22d48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kha=C3=AFs=20COLIN?= Date: Sun, 4 May 2025 13:43:05 +0200 Subject: [PATCH] refactor(CommandParseError): remove the display implementation we want to be able to give additional arguments to the display function later, so this is needed --- notes.org | 10 +++++++++- src/command.rs | 14 +++++--------- src/error_display.rs | 2 +- src/tokens.rs | 18 ++++++++++++++++++ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/notes.org b/notes.org index 47adb70..36210c4 100644 --- a/notes.org +++ b/notes.org @@ -158,7 +158,15 @@ CLOCK: [2025-05-04 dim. 13:35]--[2025-05-04 dim. 13:38] => 0:03 :EFFORT: 10 :END: -**** TODO Remove the CommandParseError Display implementation +**** DONE Remove the CommandParseError Display implementation +:PROPERTIES: +:EFFORT: 10 +:END: +:LOGBOOK: +CLOCK: [2025-05-04 dim. 13:38]--[2025-05-04 dim. 13:44] => 0:06 +:END: + +**** TODO implement OSDBError for ScanError :PROPERTIES: :EFFORT: 10 :END: diff --git a/src/command.rs b/src/command.rs index cfbfd5e..d016549 100644 --- a/src/command.rs +++ b/src/command.rs @@ -54,16 +54,12 @@ pub enum CommandParseError { Scan(ScanError), } -impl std::fmt::Display for CommandParseError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { +impl CommandParseError { + pub(crate) fn message(&self) -> String { match self { - CommandParseError::MetaCommand(meta_command_parse_error) => { - write!(f, "{meta_command_parse_error}") - } - CommandParseError::Statement(statement_parse_error) => { - write!(f, "{statement_parse_error}") - } - CommandParseError::Scan(scan_error) => write!(f, "{scan_error:?}"), + CommandParseError::MetaCommand(x) => format!("{x}"), + CommandParseError::Statement(x) => format!("{x}"), + CommandParseError::Scan(x) => format!("{x}"), } } } diff --git a/src/error_display.rs b/src/error_display.rs index 048c1dd..f283b8d 100644 --- a/src/error_display.rs +++ b/src/error_display.rs @@ -8,7 +8,7 @@ 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(format!("{self}")) + .with_message(self.message()) .with_label(Label::new((file, 0..input.len() - 1)).with_color(Color::Red)) .finish() .print((file, Source::from(input))) diff --git a/src/tokens.rs b/src/tokens.rs index 0bff497..404d10c 100644 --- a/src/tokens.rs +++ b/src/tokens.rs @@ -67,12 +67,30 @@ pub enum ScanErrorKind { UnknownMetaCommand(String), } +impl std::fmt::Display for ScanErrorKind { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + match self { + ScanErrorKind::UnexpectedChar(c) => write!(f, "unexpected char: {c:?}"), + ScanErrorKind::UnexpectedEndOfInput => write!(f, "unexpected end of input"), + ScanErrorKind::UnknownKeyword(x) => write!(f, "unknown keyword: {x:?}"), + ScanErrorKind::UnknownMetaCommand(x) => write!(f, "unknown meta-command: {x:?}"), + } + } +} + #[derive(Debug, Eq, PartialEq)] pub struct ScanError { pub location: Location, pub kind: ScanErrorKind, } +impl std::fmt::Display for ScanError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + let kind = &self.kind; + write!(f, "{kind}") + } +} + impl Tokenizer { fn new(input: String, file: String) -> Self { Self {