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::error;
use std::fmt; use std::fmt;
pub enum RenderError<'a> { pub enum RenderError {
Generic(String), Generic(String),
/// For when walking fails (example, a missing key on a map) TemplateNotFound(String),
CantWalk {
segment: String,
elem: &'a dyn ContextElement,
},
NotFound {
path: &'a Vec<&'a str>,
breadcrumbs: Vec<&'a dyn ContextElement>,
},
} }
pub enum WalkError { pub enum WalkError {
@ -24,35 +15,29 @@ pub struct CompileError {
pub message: String, pub message: String,
} }
impl fmt::Display for RenderError<'_> { impl fmt::Display for RenderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
RenderError::Generic(msg) => write!(f, "{}", msg), RenderError::Generic(msg) => write!(f, "{}", msg),
RenderError::CantWalk { segment, elem } => { RenderError::TemplateNotFound(name) => {
write!(f, "Tried to walk to {} from {:?}", segment, elem) write!(f, "No template named {} in context", name)
}
RenderError::NotFound { path, breadcrumbs } => {
write!(f, "Could not find {:?} in {:?}", path, breadcrumbs)
} }
} }
} }
} }
impl fmt::Debug for RenderError<'_> { impl fmt::Debug for RenderError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
RenderError::Generic(msg) => write!(f, "{}", msg), RenderError::Generic(msg) => write!(f, "{}", msg),
RenderError::CantWalk { segment, elem } => { RenderError::TemplateNotFound(name) => {
write!(f, "Tried to walk to {} from {:?}", segment, elem) write!(f, "No template named {} in context", name)
}
RenderError::NotFound { path, breadcrumbs } => {
write!(f, "Could not find {:?} in {:?}", path, breadcrumbs)
} }
} }
} }
} }
impl error::Error for RenderError<'_> { impl error::Error for RenderError {
fn source(&self) -> Option<&(dyn error::Error + 'static)> { fn source(&self) -> Option<&(dyn error::Error + 'static)> {
None None
} }

View File

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