Simplified the RenderError class.
This commit is contained in:
parent
05b56e83a9
commit
5d7c991bf0
@ -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
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user