use thiserror::Error as ThisError;
#[derive(Debug, ThisError)]
#[cfg_attr(feature = "miette", derive(miette::Diagnostic))]
pub enum RenderError {
#[error("missing key `{0}`")]
MissingKey(String),
#[error("write failed: {0}")]
Io(#[from] std::io::Error),
}
#[derive(Clone, Debug, ThisError, PartialEq, Eq)]
#[cfg_attr(feature = "miette", derive(miette::Diagnostic))]
#[cfg_attr(feature = "miette", diagnostic(transparent))]
#[error(transparent)]
pub struct ParseError(Box<InnerParseError>);
#[derive(Clone, Debug, ThisError, PartialEq, Eq)]
#[error("{kind} at span start = {offset}, len = {len}: {src}")]
struct InnerParseError {
src: String,
offset: usize,
len: usize,
kind: ErrorKind,
}
#[cfg(feature = "miette")]
impl miette::Diagnostic for InnerParseError {
fn source_code(&self) -> Option<&dyn miette::SourceCode> {
Some(&self.src)
}
fn labels(&self) -> Option<Box<dyn Iterator<Item = miette::LabeledSpan> + '_>> {
Some(Box::new(std::iter::once_with(|| {
miette::LabeledSpan::new(Some(self.kind.to_string()), self.offset, self.len)
})))
}
}
#[derive(Clone, Debug, ThisError, PartialEq, Eq)]
enum ErrorKind {
#[error("This bracket is not opening or closing anything. Try removing it, or escaping it with a backslash.")]
Unbalanced,
#[error("This escape is malformed.")]
Escape,
#[error("A key cannot be empty.")]
KeyEmpty,
#[error("Escapes are not allowed in keys.")]
KeyEscape,
}
impl ParseError {
fn new(src: &str, start: usize, end: usize, kind: ErrorKind) -> Self {
Self(Box::new(InnerParseError {
src: String::from(src),
offset: start,
len: end.saturating_sub(start) + 1,
kind,
}))
}
pub(crate) fn unbalanced(src: &str, start: usize, end: usize) -> Self {
Self::new(src, start, end, ErrorKind::Unbalanced)
}
pub(crate) fn escape(src: &str, start: usize, end: usize) -> Self {
Self::new(src, start, end, ErrorKind::Escape)
}
pub(crate) fn key_empty(src: &str, start: usize, end: usize) -> Self {
Self::new(src, start, end, ErrorKind::KeyEmpty)
}
pub(crate) fn key_escape(src: &str, start: usize, end: usize) -> Self {
Self::new(src, start, end, ErrorKind::KeyEscape)
}
}