Merge branch 'trait_wrapper' into render

This commit is contained in:
Tom Alexander 2020-05-02 17:23:29 -04:00
commit 82fb4964ee
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 26 additions and 8 deletions

View File

@ -1,8 +1,9 @@
use crate::parser::Filter;
use crate::renderer::errors::RenderError;
use crate::renderer::renderer::RenderWrapper;
use std::fmt::Debug;
pub trait ContextElement: Debug + Walkable + Renderable + Loopable {}
pub trait ContextElement: Debug + RenderWrapper + Walkable + Renderable + Loopable {}
pub trait Walkable {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError>;

View File

@ -19,6 +19,24 @@ pub struct DustRenderer<'a> {
templates: HashMap<String, &'a Template<'a>>,
}
pub trait RenderWrapper {
fn render_body<'a>(
&'a self,
renderer: &'a DustRenderer,
body: &Body,
) -> Result<String, RenderError>;
}
impl<C: ContextElement> RenderWrapper for C {
fn render_body<'a>(
&'a self,
renderer: &'a DustRenderer,
body: &Body,
) -> Result<String, RenderError<'a>> {
renderer.render_body(body, self)
}
}
pub fn compile_template<'a>(
source: &'a str,
name: String,
@ -44,7 +62,7 @@ impl<'a> DustRenderer<'a> {
.insert(template.name.clone(), &template.template);
}
pub fn render<C>(&self, name: &str, context: &'a C) -> Result<String, RenderError<'a>>
pub fn render<C>(&'a self, name: &str, context: &'a C) -> Result<String, RenderError<'a>>
where
C: ContextElement,
{
@ -60,7 +78,7 @@ impl<'a> DustRenderer<'a> {
self.render_body(&main_template.contents, context)
}
fn render_body<C>(&self, body: &Body, context: &'a C) -> Result<String, RenderError<'a>>
fn render_body<C>(&'a self, body: &Body, context: &'a C) -> Result<String, RenderError<'a>>
where
C: ContextElement,
{
@ -76,7 +94,7 @@ impl<'a> DustRenderer<'a> {
Ok(output)
}
fn render_tag<C>(&self, tag: &DustTag, context: &'a C) -> Result<String, RenderError<'a>>
fn render_tag<C>(&'a self, tag: &DustTag, context: &'a C) -> Result<String, RenderError<'a>>
where
C: ContextElement,
{
@ -111,19 +129,18 @@ impl<'a> DustRenderer<'a> {
None => Ok("".to_owned()),
};
} else {
match container.contents {
None => Ok("".to_owned()),
match &container.contents {
None => return Ok("".to_owned()),
Some(body) => {
let rendered_results: Result<Vec<String>, RenderError> =
loop_elements
.into_iter()
.map(|array_elem| self.render_body(&body, array_elem))
.map(|array_elem| array_elem.render_body(self, &body))
.collect();
let rendered_slice: &[String] = &rendered_results?;
return Ok(rendered_slice.join(""));
}
};
return Ok("".to_owned());
}
}
}