From caa79d5ba25d3a35030b767dd70fef40e195cae1 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 2 May 2020 15:55:38 -0400 Subject: [PATCH] Moving towards a generic trait wrapper to call between dynamic and static dispatch functions. --- src/renderer/context_element.rs | 3 ++- src/renderer/renderer.rs | 20 +++++++++++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/renderer/context_element.rs b/src/renderer/context_element.rs index 14f1aae..0dccc2c 100644 --- a/src/renderer/context_element.rs +++ b/src/renderer/context_element.rs @@ -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>; diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index b98e77c..418abc8 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -19,6 +19,24 @@ pub struct DustRenderer<'a> { templates: HashMap>, } +pub trait RenderWrapper { + fn render_body<'a>( + &'a self, + renderer: &'a DustRenderer, + body: &Body, + ) -> Result; +} + +impl RenderWrapper for C { + fn render_body<'a>( + &'a self, + renderer: &'a DustRenderer, + body: &Body, + ) -> Result> { + renderer.render_body(body, self) + } +} + pub fn compile_template<'a>( source: &'a str, name: String, @@ -117,7 +135,7 @@ impl<'a> DustRenderer<'a> { let rendered_results: Result, 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(""));