Switching back to a Vec because inserting multiple elements into the linked list structure while maintaining ownership of each node proved to be difficult.

This commit is contained in:
Tom Alexander
2020-06-06 22:24:27 -04:00
parent 78bffb5f04
commit 71592a9a32
5 changed files with 50 additions and 142 deletions

View File

@@ -7,7 +7,6 @@ use crate::parser::Path;
use crate::parser::Special;
use crate::parser::Template;
use crate::parser::TemplateElement;
use crate::renderer::breadcrumb_tree::BreadcrumbTree;
use crate::renderer::breadcrumb_tree::BreadcrumbTreeElement;
use crate::renderer::context_element::ContextElement;
use crate::renderer::context_element::IntoContextElement;
@@ -49,15 +48,16 @@ impl<'a> DustRenderer<'a> {
where
C: IntoContextElement,
{
let breadcrumbs =
context.map(|ctx| BreadcrumbTree::new(None, BreadcrumbTreeElement::from_borrowed(ctx)));
let breadcrumbs = context
.map(|ctx| vec![BreadcrumbTreeElement::from_borrowed(ctx)])
.unwrap_or(Vec::new());
self.render_template(name, breadcrumbs.as_ref(), None)
}
pub fn render_template(
&'a self,
name: &str,
breadcrumbs: Option<&'a BreadcrumbTree>,
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
blocks: Option<&'a InlinePartialTreeElement<'a>>,
) -> Result<String, RenderError> {
let main_template = match self.templates.get(name) {
@@ -78,7 +78,7 @@ impl<'a> DustRenderer<'a> {
fn render_maybe_body(
&'a self,
body: &'a Option<Body>,
breadcrumbs: Option<&'a BreadcrumbTree>,
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
blocks: &'a BlockContext<'a>,
) -> Result<String, RenderError> {
match body {
@@ -90,7 +90,7 @@ impl<'a> DustRenderer<'a> {
fn render_body(
&'a self,
body: &'a Body,
breadcrumbs: Option<&'a BreadcrumbTree>,
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
blocks: &'a BlockContext<'a>,
) -> Result<String, RenderError> {
let mut output = String::new();
@@ -110,7 +110,7 @@ impl<'a> DustRenderer<'a> {
pub fn render_partial_name(
&'a self,
body: &'a Vec<PartialNameElement>,
breadcrumbs: Option<&'a BreadcrumbTree>,
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
) -> Result<String, RenderError> {
let converted_to_template_elements: Vec<TemplateElement<'a>> =
body.into_iter().map(|e| e.into()).collect();
@@ -118,7 +118,7 @@ impl<'a> DustRenderer<'a> {
// cannot contain blocks or inline partials, so we use a blank
// BlockContext.
let empty_block_context = BlockContext {
breadcrumbs: None,
breadcrumbs: &Vec::new(),
blocks: &InlinePartialTreeElement::new(None, HashMap::new()),
};
self.render_body(
@@ -133,7 +133,7 @@ impl<'a> DustRenderer<'a> {
fn render_tag(
&'a self,
tag: &'a DustTag,
breadcrumbs: Option<&'a BreadcrumbTree>,
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
blocks: &'a BlockContext<'a>,
) -> Result<String, RenderError> {
match tag {
@@ -190,23 +190,15 @@ impl<'a> DustRenderer<'a> {
Ok("".to_owned())
}
/// Returns a option of a tuple of (parent, new_node_elements)
/// which can then be formed into new BreadcrumbTreeNodes
///
/// If None is returned, then it is a signal to simply re-use the
/// existing breadcrumbs.
///
/// Otherwise, the parent (which may be None, especially for
/// explicit contexts) and the additional node elements (which may
/// be empty) should be combined into a final BreadcrumbTreeNode
fn new_breadcrumbs_section<'b>(
&'b self,
maybe_breadcrumbs: Option<&'b BreadcrumbTree>,
maybe_breadcrumbs: &'b Vec<BreadcrumbTreeElement<'b>>,
index_context: Option<&'b dyn IntoContextElement>,
injected_context: Option<&'b dyn IntoContextElement>,
explicit_context: &Option<Path<'b>>,
new_context_element: Option<&'b dyn ContextElement>,
) -> Option<(Option<&'b BreadcrumbTree>, Vec<BreadcrumbTreeElement<'b>>)> {
) {
/*
// If none of the additional contexts are present, return None
// to signal that the original breadcrumbs should be used
// rather than incurring a copy here.
@@ -248,24 +240,18 @@ impl<'a> DustRenderer<'a> {
index_context.map(|ctx| new_nodes.push(BreadcrumbTreeElement::from_borrowed(ctx)));
Some((parent, new_nodes))
*/
todo!()
}
/// Returns a option of a tuple of (parent, new_node_elements)
/// which can then be formed into new BreadcrumbTreeNodes
///
/// If None is returned, then it is a signal to simply re-use the
/// existing breadcrumbs.
///
/// Otherwise, the parent (which may be None, especially for
/// explicit contexts) and the additional node elements (which may
/// be empty) should be combined into a final BreadcrumbTreeNode
fn new_breadcrumbs_partial<'b>(
&'b self,
maybe_breadcrumbs: Option<&'b BreadcrumbTree>,
explicit_context_maybe_breadcrumbs: Option<&'b BreadcrumbTree>,
maybe_breadcrumbs: &'b Vec<BreadcrumbTreeElement<'b>>,
explicit_context_maybe_breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
injected_context: Option<&'b dyn IntoContextElement>,
explicit_context: &Option<Path<'b>>,
) -> Option<(Option<&'b BreadcrumbTree>, Vec<BreadcrumbTreeElement<'b>>)> {
) {
/*
// If none of the additional contexts are present, return None
// to signal that the original breadcrumbs should be used
// rather than incurring a copy here.
@@ -313,33 +299,8 @@ impl<'a> DustRenderer<'a> {
});
});
Some((parent, new_nodes))
}
/// Returns a Breadcrumb tree where all the bottom nodes that do
/// not match the predicate and the first node that match the
/// predicate are shaved off, and a list of those nodes that are
/// shaved off.
fn split_tree_at_predicate<'b, F>(
maybe_breadcrumbs: Option<&'b BreadcrumbTree>,
f: F,
) -> (Option<&'b BreadcrumbTree<'b>>, Vec<&'b BreadcrumbTree<'b>>)
where
F: Fn(&'b BreadcrumbTree) -> bool,
{
match maybe_breadcrumbs {
None => return (None, Vec::new()),
Some(breadcrumbs) => {
let mut passed_nodes: Vec<&'b BreadcrumbTree<'b>> = Vec::new();
for tree_node in breadcrumbs {
passed_nodes.push(tree_node);
if f(tree_node) {
return (tree_node.get_parent(), passed_nodes);
}
}
return (None, passed_nodes);
}
}
Some((parent, new_nodes))*/
todo!()
}
fn preprocess_filters(filters: &Vec<Filter>) -> Vec<Filter> {
@@ -360,6 +321,6 @@ impl<'a> DustRenderer<'a> {
struct BlockContext<'a> {
/// The breadcrumbs at the time of entering the current partial
breadcrumbs: Option<&'a BreadcrumbTree<'a>>,
breadcrumbs: &'a Vec<BreadcrumbTreeElement<'a>>,
blocks: &'a InlinePartialTreeElement<'a>,
}