Commenting out the parameterscontext type.

This commit is contained in:
Tom Alexander 2020-05-10 19:01:02 -04:00
parent 6fb329388f
commit 5c79b436a0
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
3 changed files with 65 additions and 50 deletions

View File

@ -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<dyn ContextElement>;
}
impl<C: 'static + ContextElement> IntoBoxedContextElement for C {
fn to_box(self) -> Box<dyn ContextElement> {
Box::new(self)
}
}
// impl PartialEq<dyn ContextElement> for dyn ContextElement {
// fn eq(&self, other: &dyn ContextElement) -> bool {
// todo!()

View File

@ -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<dyn ContextElement> = y.clone().to_box();
// let owned_y: Box<dyn ContextElement> = 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<KVPair<'a>>,
) -> 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<KVPair<'a>>,
// ) -> 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<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<'a> Renderable for ParametersContext<'a> {
// 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<'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 {}

View File

@ -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);