Simplified the RenderError class.

This commit is contained in:
Tom Alexander 2020-05-09 14:27:42 -04:00
parent 05b56e83a9
commit 5d7c991bf0
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 14 additions and 33 deletions

View File

@ -1,18 +1,9 @@
use crate::renderer::context_element::ContextElement;
use std::error;
use std::fmt;
pub enum RenderError<'a> {
pub enum RenderError {
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>,
},
TemplateNotFound(String),
}
pub enum WalkError {
@ -24,35 +15,29 @@ pub struct CompileError {
pub message: String,
}
impl fmt::Display for RenderError<'_> {
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)
RenderError::TemplateNotFound(name) => {
write!(f, "No template named {} in context", name)
}
}
}
}
impl fmt::Debug for RenderError<'_> {
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)
RenderError::TemplateNotFound(name) => {
write!(f, "No template named {} in context", name)
}
}
}
}
impl error::Error for RenderError<'_> {
impl error::Error for RenderError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None
}

View File

@ -51,14 +51,11 @@ impl<'a> DustRenderer<'a> {
&'a self,
name: &str,
breadcrumbs: &Vec<&'a dyn ContextElement>,
) -> Result<String, RenderError<'a>> {
) -> Result<String, RenderError> {
let main_template = match self.templates.get(name) {
Some(tmpl) => tmpl,
None => {
return Err(RenderError::Generic(format!(
"No template named {} in context",
name
)));
return Err(RenderError::TemplateNotFound(name.to_owned()));
}
};
self.render_body(&main_template.contents, breadcrumbs)
@ -68,7 +65,7 @@ impl<'a> DustRenderer<'a> {
&'a self,
body: &'a Body,
breadcrumbs: &Vec<&'a dyn ContextElement>,
) -> Result<String, RenderError<'a>> {
) -> Result<String, RenderError> {
let mut output = String::new();
for elem in &body.elements {
match elem {
@ -86,7 +83,7 @@ impl<'a> DustRenderer<'a> {
&'a self,
tag: &'a DustTag,
breadcrumbs: &Vec<&'a dyn ContextElement>,
) -> Result<String, RenderError<'a>> {
) -> Result<String, RenderError> {
match tag {
DustTag::DTComment(_comment) => (),
DustTag::DTSpecial(special) => {
@ -181,8 +178,7 @@ impl<'a> DustRenderer<'a> {
let injected_context = ParametersContext::new(breadcrumbs, &partial.params);
let mut new_breadcrumbs = breadcrumbs.clone();
new_breadcrumbs.insert(new_breadcrumbs.len() - 1, &injected_context);
// TODO: Change unwrap to ?
let rendered_content = self.render(&partial.name, &new_breadcrumbs).unwrap();
let rendered_content = self.render(&partial.name, &new_breadcrumbs)?;
return Ok(rendered_content);
}
}