2024-09-10 05:04:30 +00:00
|
|
|
use annotate_snippets::{Level, Renderer, Snippet};
|
|
|
|
|
2024-09-07 10:38:12 +00:00
|
|
|
use crate::{vm::VmError, LexError, ParseError};
|
2024-09-07 08:34:03 +00:00
|
|
|
|
2024-09-07 16:15:47 +00:00
|
|
|
#[derive(Debug, PartialEq)]
|
2024-09-07 08:34:03 +00:00
|
|
|
pub enum DustError<'src> {
|
2024-09-07 16:15:47 +00:00
|
|
|
Lex {
|
2024-09-07 08:34:03 +00:00
|
|
|
error: LexError,
|
|
|
|
source: &'src str,
|
|
|
|
},
|
2024-09-07 16:15:47 +00:00
|
|
|
Parse {
|
2024-09-07 08:34:03 +00:00
|
|
|
error: ParseError,
|
|
|
|
source: &'src str,
|
|
|
|
},
|
2024-09-07 16:15:47 +00:00
|
|
|
Runtime {
|
2024-09-07 08:34:03 +00:00
|
|
|
error: VmError,
|
|
|
|
source: &'src str,
|
|
|
|
},
|
|
|
|
}
|
2024-09-10 05:04:30 +00:00
|
|
|
|
|
|
|
impl<'src> DustError<'src> {
|
|
|
|
pub fn report(&self) -> String {
|
|
|
|
let mut report = String::new();
|
|
|
|
let renderer = Renderer::styled();
|
|
|
|
|
|
|
|
match self {
|
|
|
|
DustError::Runtime { error, source } => {
|
|
|
|
let position = error.position();
|
|
|
|
let label = format!("Runtime {}", error.title());
|
|
|
|
let description = error.description();
|
|
|
|
let message = Level::Error.title(&label).snippet(
|
|
|
|
Snippet::source(source).fold(true).annotation(
|
|
|
|
Level::Error
|
|
|
|
.span(position.0..position.1)
|
|
|
|
.label(&description),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
report.push_str(&renderer.render(message).to_string());
|
|
|
|
}
|
|
|
|
_ => todo!(),
|
|
|
|
}
|
|
|
|
|
|
|
|
report
|
|
|
|
}
|
|
|
|
}
|