From 5efa650b67d98568a77785f83685f53e11893643 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 11 Apr 2020 22:19:54 -0400 Subject: [PATCH] Switching to a combined ContextElement trait for Walkable and Renderable --- src/renderer/mod.rs | 1 + src/renderer/renderer.rs | 44 ++++++++++++++++++++++++++++++++-------- src/renderer/walkable.rs | 6 +++--- 3 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 81b7e79..c80f1a5 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -9,4 +9,5 @@ pub use renderable::Renderable; pub use renderer::compile_template; pub use renderer::CompiledTemplate; pub use renderer::DustRenderer; +pub use walkable::ContextElement; pub use walkable::Walkable; diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index fbb79dd..88e2986 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -111,9 +111,32 @@ impl<'a> DustRenderer<'a> { #[cfg(test)] mod tests { use super::*; + use crate::renderer::walkable::ContextElement; - impl Walkable for HashMap<&str, I> { - fn walk(&self, segment: &str) -> &dyn Walkable { + impl ContextElement for u32 {} + impl ContextElement for &str {} + impl ContextElement for HashMap<&str, I> {} + + impl Renderable for u32 { + fn render(&self) -> std::string::String { + self.to_string() + } + } + + impl Renderable for &str { + fn render(&self) -> std::string::String { + self.to_string() + } + } + + impl Renderable for HashMap<&str, I> { + fn render(&self) -> std::string::String { + panic!("Attempted to render a hashmap"); + } + } + + impl Walkable for HashMap<&str, I> { + fn walk(&self, segment: &str) -> &dyn ContextElement { self.get(segment).unwrap() } @@ -123,7 +146,7 @@ mod tests { } impl Walkable for &str { - fn walk(&self, segment: &str) -> &dyn Walkable { + fn walk(&self, segment: &str) -> &dyn ContextElement { panic!("Tried to walk down a str"); } @@ -133,7 +156,7 @@ mod tests { } impl Walkable for u32 { - fn walk(&self, segment: &str) -> &dyn Walkable { + fn walk(&self, segment: &str) -> &dyn ContextElement { panic!("Tried to walk down a str"); } @@ -142,7 +165,10 @@ mod tests { } } - fn do_the_walk<'a>(context: &'a dyn Walkable, path: &Vec<&str>) -> &'a dyn Walkable { + fn do_the_walk<'a>( + context: &'a dyn ContextElement, + path: &Vec<&str>, + ) -> &'a dyn ContextElement { let mut output = context; for elem in path.iter() { @@ -171,11 +197,11 @@ mod tests { .iter() .cloned() .collect(); - assert_eq!(do_the_walk(&context, &vec!["cat"]).val(), "kitty"); - // assert_eq!(do_the_walk(&number_context, &vec!["tiger"]), &3); + assert_eq!(do_the_walk(&context, &vec!["cat"]).render(), "kitty"); + assert_eq!(do_the_walk(&number_context, &vec!["tiger"]).render(), "3"); // assert_eq!( - // do_the_walk(&deep_context, &vec!["tiger"]), - // &[("food", "people")].iter().cloned().collect() + // do_the_walk(&deep_context, &vec!["tiger", "food"]).render(), + // "people" // ); } } diff --git a/src/renderer/walkable.rs b/src/renderer/walkable.rs index e8bf09f..22b7426 100644 --- a/src/renderer/walkable.rs +++ b/src/renderer/walkable.rs @@ -1,9 +1,9 @@ use super::renderable::Renderable; -pub trait Walkable { - // type Output: Walkable; +pub trait ContextElement: Walkable + Renderable {} - fn walk(&self, segment: &str) -> &dyn Walkable; +pub trait Walkable { + fn walk(&self, segment: &str) -> &dyn ContextElement; fn val(&self) -> String; }