From 4e274b9ea53067a75953fd2ade933277e9059273 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 10 May 2020 21:28:47 -0400 Subject: [PATCH] Running into the walking issue again. --- src/renderer/parameters_context.rs | 51 ++++++++++++++++++++++++++++++ src/renderer/renderer.rs | 6 ++-- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/src/renderer/parameters_context.rs b/src/renderer/parameters_context.rs index e9dd02c..1a5af63 100644 --- a/src/renderer/parameters_context.rs +++ b/src/renderer/parameters_context.rs @@ -23,11 +23,13 @@ use std::collections::HashMap; /// are imposing the cost of copying the data in the renderer because /// the parser has no reason to not be able to reference data from the /// input string. +#[derive(Clone, Debug, PartialEq)] pub enum OwnedRValue { RVPath(OwnedPath), RVString(String), } +#[derive(Clone, Debug, PartialEq)] pub struct OwnedPath { pub keys: Vec, } @@ -43,6 +45,7 @@ impl From<&RValue<'_>> for OwnedRValue { } } +#[derive(Debug)] pub struct NewParametersContext { params: HashMap, breadcrumbs: Vec>, @@ -67,6 +70,54 @@ impl NewParametersContext { } } +impl ContextElement for NewParametersContext {} + +impl Renderable for NewParametersContext { + 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 Loopable for NewParametersContext { + 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 Walkable for NewParametersContext { + fn walk(&self, segment: &str) -> Result<&dyn ContextElement, WalkError> { + let rval = self.params.get(segment).ok_or(WalkError::CantWalk)?; + match rval { + OwnedRValue::RVPath(path) => walk_path(self.breadcrumbs, &path.keys), + OwnedRValue::RVString(text) => Ok(text), + } + } +} + +impl Clone for NewParametersContext { + fn clone(&self) -> Self { + // TODO: What is this doing, really? + *self + } +} + +impl CompareContextElement for NewParametersContext { + fn to_any(&self) -> &dyn Any { + self + } + + fn equals(&self, other: &dyn ContextElement) -> bool { + // TODO: Does this ever happen? perhaps I should have a panic here. + false + } +} + // #[derive(Clone, Debug)] // pub struct ParametersContext<'a> { // params: HashMap<&'a str, &'a RValue<'a>>, diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index a0b8a73..8d66251 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::NewParametersContext; 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 = NewParametersContext::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);