diff --git a/src/renderer/context_element.rs b/src/renderer/context_element.rs index f00f56c..4440e31 100644 --- a/src/renderer/context_element.rs +++ b/src/renderer/context_element.rs @@ -4,7 +4,10 @@ use crate::renderer::errors::WalkError; use std::any::Any; use std::fmt::Debug; -pub trait ContextElement: Debug + Walkable + Renderable + Loopable {} +pub trait ContextElement: + Debug + Walkable + Renderable + Loopable + IntoBoxedContextElement +{ +} pub trait Walkable { fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError>; @@ -31,6 +34,16 @@ pub trait CompareContextElement { fn to_any(&self) -> &dyn Any; } +pub trait IntoBoxedContextElement { + fn to_box(self) -> Box; +} + +impl IntoBoxedContextElement for C { + fn to_box(self) -> Box { + Box::new(self) + } +} + // impl PartialEq for dyn ContextElement { // fn eq(&self, other: &dyn ContextElement) -> bool { // todo!() diff --git a/src/renderer/parameters_context.rs b/src/renderer/parameters_context.rs index cf8feb3..f69314d 100644 --- a/src/renderer/parameters_context.rs +++ b/src/renderer/parameters_context.rs @@ -1,6 +1,7 @@ use crate::parser::KVPair; use crate::parser::{Filter, RValue}; use crate::renderer::context_element::ContextElement; +use crate::renderer::context_element::IntoBoxedContextElement; use crate::renderer::walking::walk_path; use crate::renderer::Loopable; use crate::renderer::RenderError; @@ -57,6 +58,7 @@ impl NewParametersContext { let x: String = "foo".to_owned(); let y: &dyn ContextElement = &x as _; + let z: Box = y.clone().to_box(); // let owned_y: Box = Box::new(*y.clone()); // unsafe { // let ce = &mut *y.clone() as *mut dyn ContextElement; @@ -73,57 +75,57 @@ impl NewParametersContext { } } -#[derive(Clone, Debug)] -pub struct ParametersContext<'a> { - params: HashMap<&'a str, &'a RValue<'a>>, - breadcrumbs: &'a Vec<&'a dyn ContextElement>, -} +// #[derive(Clone, Debug)] +// pub struct ParametersContext<'a> { +// params: HashMap<&'a str, &'a RValue<'a>>, +// breadcrumbs: &'a Vec<&'a dyn ContextElement>, +// } -impl<'a> ParametersContext<'a> { - pub fn new( - breadcrumbs: &'a Vec<&'a dyn ContextElement>, - params: &'a Vec>, - ) -> ParametersContext<'a> { - let param_map = params - .iter() - .map(|pair: &KVPair<'a>| (pair.key, &pair.value)) - .collect(); - ParametersContext { - params: param_map, - breadcrumbs: breadcrumbs, - } - } -} +// impl<'a> ParametersContext<'a> { +// pub fn new( +// breadcrumbs: &'a Vec<&'a dyn ContextElement>, +// params: &'a Vec>, +// ) -> ParametersContext<'a> { +// let param_map = params +// .iter() +// .map(|pair: &KVPair<'a>| (pair.key, &pair.value)) +// .collect(); +// ParametersContext { +// params: param_map, +// breadcrumbs: breadcrumbs, +// } +// } +// } -impl<'a> ContextElement for ParametersContext<'a> {} +// impl<'a> ContextElement for ParametersContext<'a> {} -impl<'a> Renderable for ParametersContext<'a> { - fn render(&self, _filters: &Vec) -> Result { - // TODO: Would this even ever be called? Won't matter, but I'd - // like to know. Since it is injected 1 above the current - // context, we wouldn't be able to access it with `{.}`. - Ok("[object Object]".to_owned()) - } -} +// impl<'a> Renderable for ParametersContext<'a> { +// fn render(&self, _filters: &Vec) -> Result { +// // TODO: Would this even ever be called? Won't matter, but I'd +// // like to know. Since it is injected 1 above the current +// // context, we wouldn't be able to access it with `{.}`. +// Ok("[object Object]".to_owned()) +// } +// } -impl<'a> Loopable for ParametersContext<'a> { - fn get_loop_elements(&self) -> Vec<&dyn ContextElement> { - // TODO: Would this even ever be called? Won't matter, but I'd - // like to know. Since it is injected 1 above the current - // context, we wouldn't be able to access it with `{.}`. - vec![self] - } -} +// impl<'a> Loopable for ParametersContext<'a> { +// fn get_loop_elements(&self) -> Vec<&dyn ContextElement> { +// // TODO: Would this even ever be called? Won't matter, but I'd +// // like to know. Since it is injected 1 above the current +// // context, we wouldn't be able to access it with `{.}`. +// vec![self] +// } +// } -impl<'a> Walkable for ParametersContext<'a> { - fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> { - let rval = self.params.get(segment).ok_or(WalkError::CantWalk)?; - match rval { - RValue::RVPath(path) => walk_path(self.breadcrumbs, &path.keys), - RValue::RVString(text) => Ok(text), - } - } -} +// impl<'a> Walkable for ParametersContext<'a> { +// fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> { +// let rval = self.params.get(segment).ok_or(WalkError::CantWalk)?; +// match rval { +// RValue::RVPath(path) => walk_path(self.breadcrumbs, &path.keys), +// RValue::RVString(text) => Ok(text), +// } +// } +// } impl ContextElement for String {} diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 4e3d9f8..76e6317 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -12,7 +12,7 @@ use crate::renderer::errors::RenderError; use crate::renderer::errors::WalkError; use crate::renderer::inline_partial_tree::extract_inline_partials; use crate::renderer::inline_partial_tree::InlinePartialTreeElement; -use crate::renderer::parameters_context::ParametersContext; +// use crate::renderer::parameters_context::ParametersContext; use crate::renderer::walking::walk_path; use std::collections::HashMap; @@ -194,9 +194,9 @@ impl<'a> DustRenderer<'a> { self.render_template(&partial.name, breadcrumbs, Some(blocks))?; return Ok(rendered_content); } else { - let injected_context = ParametersContext::new(breadcrumbs, &partial.params); + // let injected_context = ParametersContext::new(breadcrumbs, &partial.params); let mut new_breadcrumbs = breadcrumbs.clone(); - new_breadcrumbs.insert(new_breadcrumbs.len() - 1, &injected_context); + // new_breadcrumbs.insert(new_breadcrumbs.len() - 1, &injected_context); let rendered_content = self.render_template(&partial.name, &new_breadcrumbs, Some(blocks))?; return Ok(rendered_content);