use std::{error, fmt, io};
#[derive(Debug)]
pub enum Error {
Io(io::Error),
StackOverflow,
UnclosedSection(Box<str>),
UnopenedSection(Box<str>),
UnclosedTag,
PartialsDisabled,
IllegalPartial(Box<str>),
NotFound(Box<str>),
#[cfg(feature = "indexes")]
IndexParse(String),
}
impl error::Error for Error {}
impl From<io::Error> for Error {
fn from(err: io::Error) -> Self {
Error::Io(err)
}
}
impl<T> From<arrayvec::CapacityError<T>> for Error {
fn from(_: arrayvec::CapacityError<T>) -> Self {
Error::StackOverflow
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Error::Io(err) => err.fmt(f),
Error::StackOverflow => write!(
f,
"Ramhorns has overflown its stack when parsing nested sections",
),
Error::UnclosedSection(name) => write!(
f,
"Section not closed properly, was expecting {{{{/{}}}}}",
name
),
Error::UnopenedSection(name) => {
write!(f, "Unexpected closing section {{{{/{}}}}}", name)
}
Error::UnclosedTag => write!(f, "Couldn't find closing braces matching opening braces"),
Error::PartialsDisabled => write!(f, "Partials are not allowed in the current context"),
Error::IllegalPartial(name) => write!(
f,
"Attempted to load {}; partials can only be loaded from the template directory",
name
),
Error::NotFound(name) => write!(f, "Template file {} not found", name),
#[cfg(feature = "indexes")]
Error::IndexParse(index) => write!(f, "Failed to parse index {}", index),
}
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn displays_properly() {
assert_eq!(
Error::UnclosedSection("foo".into()).to_string(),
"Section not closed properly, was expecting {{/foo}}"
);
assert_eq!(
Error::UnclosedTag.to_string(),
"Couldn't find closing braces matching opening braces"
);
}
}