duster/src/renderer/errors.rs
2020-05-23 18:14:23 -04:00

101 lines
2.4 KiB
Rust

use std::error;
use std::fmt;
/// Fatal errors while rendering.
///
/// A RenderError will halt rendering.
#[derive(PartialEq)]
pub enum RenderError {
Generic(String),
TemplateNotFound(String),
InvalidJson(String),
}
#[derive(PartialEq)]
pub enum WalkError {
CantWalk,
}
#[derive(Clone)]
pub struct CompileError {
pub message: String,
}
impl fmt::Display for RenderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RenderError::Generic(msg) => write!(f, "{}", msg),
RenderError::TemplateNotFound(name) => {
write!(f, "No template named {} in context", name)
}
RenderError::InvalidJson(invalid_json) => write!(
f,
"Attempted to parse the following invalid JSON: {}",
invalid_json
),
}
}
}
impl fmt::Debug for RenderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
RenderError::Generic(msg) => write!(f, "{}", msg),
RenderError::TemplateNotFound(name) => {
write!(f, "No template named {} in context", name)
}
RenderError::InvalidJson(invalid_json) => write!(
f,
"Attempted to parse the following invalid JSON: {}",
invalid_json
),
}
}
}
impl error::Error for RenderError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}
}
impl fmt::Display for WalkError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
WalkError::CantWalk => write!(f, "Failed to walk"),
}
}
}
impl fmt::Debug for WalkError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
WalkError::CantWalk => write!(f, "Failed to walk"),
}
}
}
impl error::Error for WalkError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}
}
impl fmt::Display for CompileError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Error compiling: {}", self.message)
}
}
impl fmt::Debug for CompileError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Error compiling: {}", self.message)
}
}
impl error::Error for CompileError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}
}