osdb/src/error_display.rs

49 lines
1.5 KiB
Rust
Raw Normal View History

use crate::{
command::CommandParseError, meta_commands::MetaCommandParseError,
statements::StatementParseError,
};
2025-05-03 21:55:06 +02:00
use ariadne::{Color, Label, Report, ReportKind, Source};
pub trait OSDBError {
fn display(&self, file: &str, input: &str);
}
impl OSDBError for MetaCommandParseError {
fn display(&self, file: &str, input: &str) {
2025-05-03 21:55:06 +02:00
Report::build(ReportKind::Error, (file, 0..input.len() - 1))
.with_message(format!("{self}"))
2025-05-03 21:55:06 +02:00
.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) {
2025-05-03 21:55:06 +02:00
Report::build(ReportKind::Error, (file, 0..input.len() - 1))
.with_message(format!("{self}"))
2025-05-03 21:55:06 +02:00
.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 {
fn display(&self, file: &str, input: &str) {
match self {
CommandParseError::MetaCommand(err) => err.display(file, input),
CommandParseError::Statement(err) => err.display(file, input),
}
}
}