Running into the walking issue again.

This commit is contained in:
Tom Alexander 2020-05-10 21:28:47 -04:00
parent 137e7887a0
commit 4e274b9ea5
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 54 additions and 3 deletions

View File

@ -23,11 +23,13 @@ use std::collections::HashMap;
/// are imposing the cost of copying the data in the renderer because /// 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 /// the parser has no reason to not be able to reference data from the
/// input string. /// input string.
#[derive(Clone, Debug, PartialEq)]
pub enum OwnedRValue { pub enum OwnedRValue {
RVPath(OwnedPath), RVPath(OwnedPath),
RVString(String), RVString(String),
} }
#[derive(Clone, Debug, PartialEq)]
pub struct OwnedPath { pub struct OwnedPath {
pub keys: Vec<String>, pub keys: Vec<String>,
} }
@ -43,6 +45,7 @@ impl From<&RValue<'_>> for OwnedRValue {
} }
} }
#[derive(Debug)]
pub struct NewParametersContext { pub struct NewParametersContext {
params: HashMap<String, OwnedRValue>, params: HashMap<String, OwnedRValue>,
breadcrumbs: Vec<Box<dyn ContextElement>>, breadcrumbs: Vec<Box<dyn ContextElement>>,
@ -67,6 +70,54 @@ impl NewParametersContext {
} }
} }
impl ContextElement for NewParametersContext {}
impl Renderable for NewParametersContext {
fn render(&self, _filters: &Vec<Filter>) -> Result<String, RenderError> {
// 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)] // #[derive(Clone, Debug)]
// pub struct ParametersContext<'a> { // pub struct ParametersContext<'a> {
// params: HashMap<&'a str, &'a RValue<'a>>, // params: HashMap<&'a str, &'a RValue<'a>>,

View File

@ -12,7 +12,7 @@ use crate::renderer::errors::RenderError;
use crate::renderer::errors::WalkError; use crate::renderer::errors::WalkError;
use crate::renderer::inline_partial_tree::extract_inline_partials; use crate::renderer::inline_partial_tree::extract_inline_partials;
use crate::renderer::inline_partial_tree::InlinePartialTreeElement; 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 crate::renderer::walking::walk_path;
use std::collections::HashMap; use std::collections::HashMap;
@ -194,9 +194,9 @@ impl<'a> DustRenderer<'a> {
self.render_template(&partial.name, breadcrumbs, Some(blocks))?; self.render_template(&partial.name, breadcrumbs, Some(blocks))?;
return Ok(rendered_content); return Ok(rendered_content);
} else { } else {
// let injected_context = ParametersContext::new(breadcrumbs, &partial.params); let injected_context = NewParametersContext::new(breadcrumbs, &partial.params);
let mut new_breadcrumbs = breadcrumbs.clone(); 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 = let rendered_content =
self.render_template(&partial.name, &new_breadcrumbs, Some(blocks))?; self.render_template(&partial.name, &new_breadcrumbs, Some(blocks))?;
return Ok(rendered_content); return Ok(rendered_content);