refactor(CommandParseError): remove the display implementation

we want to be able to give additional arguments to the display function later,
so this is needed
This commit is contained in:
Khaïs COLIN 2025-05-04 13:43:05 +02:00
parent b8ed0868cb
commit 55b4779964
4 changed files with 33 additions and 11 deletions

View file

@ -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}"),
}
}
}

View file

@ -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)))

View file

@ -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 {