use std::fmt::{Debug, Display};
use somni_expr::error::MarkInSource;
use somni_parser::Location;
#[derive(Clone, PartialEq, Eq)]
pub struct TemplateError {
pub message: Box<str>,
pub location: Location,
}
impl TemplateError {
pub fn new(message: impl Into<Box<str>>, location: Location) -> Self {
Self {
message: message.into(),
location,
}
}
pub fn display_with<'s>(&'s self, template: &'s str) -> impl Display + 's {
MarkedTemplateError {
error: self,
template,
}
}
}
impl Display for TemplateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "Template error: {}", self.message)
}
}
impl Debug for TemplateError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"TemplateError {{ message: {:?}, location: {:?} }}",
self.message, self.location
)
}
}
struct MarkedTemplateError<'s> {
error: &'s TemplateError,
template: &'s str,
}
impl Display for MarkedTemplateError<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
MarkInSource(
self.template,
self.error.location,
"Template error",
&self.error.message,
)
.fmt(f)
}
}