use crate::renderer::context_element::ContextElement; use std::error; use std::fmt; pub enum RenderError<'a> { Generic(String), /// For when walking fails (example, a missing key on a map) CantWalk { segment: String, elem: &'a dyn ContextElement, }, NotFound { path: &'a Vec<&'a str>, breadcrumbs: Vec<&'a dyn ContextElement>, }, } pub enum WalkError<'a> { CantWalk { segment: String, elem: &'a dyn ContextElement, }, NotFound { path: &'a Vec<&'a str>, breadcrumbs: Vec<&'a dyn ContextElement>, }, } #[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::CantWalk { segment, elem } => { write!(f, "Tried to walk to {} from {:?}", segment, elem) } RenderError::NotFound { path, breadcrumbs } => { write!(f, "Could not find {:?} in {:?}", path, breadcrumbs) } } } } impl fmt::Debug for RenderError<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { RenderError::Generic(msg) => write!(f, "{}", msg), RenderError::CantWalk { segment, elem } => { write!(f, "Tried to walk to {} from {:?}", segment, elem) } RenderError::NotFound { path, breadcrumbs } => { write!(f, "Could not find {:?} in {:?}", path, breadcrumbs) } } } } 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 { segment, elem } => { write!(f, "Tried to walk to {} from {:?}", segment, elem) } WalkError::NotFound { path, breadcrumbs } => { write!(f, "Could not find {:?} in {:?}", path, breadcrumbs) } } } } impl fmt::Debug for WalkError<'_> { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self { WalkError::CantWalk { segment, elem } => { write!(f, "Tried to walk to {} from {:?}", segment, elem) } WalkError::NotFound { path, breadcrumbs } => { write!(f, "Could not find {:?} in {:?}", path, breadcrumbs) } } } } 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 } }