Moving towards a generic trait wrapper to call between dynamic and static dispatch functions.

This commit is contained in:
Tom Alexander 2020-05-02 15:55:38 -04:00
parent 3cfc294e93
commit caa79d5ba2
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 21 additions and 2 deletions

View File

@ -1,8 +1,9 @@
use crate::parser::Filter; use crate::parser::Filter;
use crate::renderer::errors::RenderError; use crate::renderer::errors::RenderError;
use crate::renderer::renderer::RenderWrapper;
use std::fmt::Debug; use std::fmt::Debug;
pub trait ContextElement: Debug + Walkable + Renderable + Loopable {} pub trait ContextElement: Debug + RenderWrapper + Walkable + Renderable + Loopable {}
pub trait Walkable { pub trait Walkable {
fn walk(&self, segment: &str) -> Result<&dyn ContextElement, RenderError>; 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>>, 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>( pub fn compile_template<'a>(
source: &'a str, source: &'a str,
name: String, name: String,
@ -117,7 +135,7 @@ impl<'a> DustRenderer<'a> {
let rendered_results: Result<Vec<String>, RenderError> = let rendered_results: Result<Vec<String>, RenderError> =
loop_elements loop_elements
.into_iter() .into_iter()
.map(|array_elem| self.render_body(&body, array_elem)) .map(|array_elem| array_elem.render_body(self, &body))
.collect(); .collect();
let rendered_slice: &[String] = &rendered_results?; let rendered_slice: &[String] = &rendered_results?;
return Ok(rendered_slice.join("")); return Ok(rendered_slice.join(""));