feat(error display): better error message for unmatched "

This commit is contained in:
Khaïs COLIN 2025-05-25 17:20:23 +02:00
parent b7400e23af
commit b79702684a
Signed by: logistic-bot
SSH key fingerprint: SHA256:3zI3/tx0ZpCLHCLPmEaGR4oeYCPMCzQxXhXutBmtOAU
2 changed files with 29 additions and 8 deletions

View file

@ -32,15 +32,34 @@ impl OSDBError for CommandParseError {
impl OSDBError for ScanError {
fn display(&self, file: &str, input: &str) {
let location = (file, Into::<std::ops::Range<usize>>::into(&self.location));
Report::build(ReportKind::Error, location.clone())
.with_message(format!("{self}"))
.with_label(
let report =
Report::build(ReportKind::Error, location.clone()).with_message(format!("{self}"));
let report = match &self.kind {
crate::tokens::ScanErrorKind::UnexpectedEndOfInputWhileLookingForMatching(
c,
start_location,
) => {
let start_location = (file, Into::<std::ops::Range<usize>>::into(start_location));
report
.with_label(
Label::new(location)
.with_order(-1)
.with_color(Color::Red)
.with_message(format!("expected {c:?} but found end of input")),
)
.with_label(
Label::new(start_location)
.with_color(Color::Blue)
.with_message(format!("looking for a {c:?} to match this")),
)
}
_ => report.with_label(
Label::new(location)
.with_color(Color::Red)
.with_message(format!("{self}")),
)
.finish()
.print((file, Source::from(input)))
.unwrap();
),
};
report.finish().print((file, Source::from(input))).unwrap();
}
}