From d5c3985c295e1ca104565728413608b53485d41e Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sat, 11 Apr 2020 20:31:44 -0400 Subject: [PATCH] initial walkable implementation --- src/renderer/mod.rs | 2 ++ src/renderer/renderer.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 6f91eb7..81b7e79 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -3,8 +3,10 @@ mod errors; mod renderable; mod renderer; +mod walkable; pub use renderable::Renderable; pub use renderer::compile_template; pub use renderer::CompiledTemplate; pub use renderer::DustRenderer; +pub use walkable::Walkable; diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 99f3aba..383e99f 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -5,6 +5,7 @@ use crate::parser::TemplateElement; use crate::renderer::errors::CompileError; use crate::renderer::errors::RenderError; use crate::renderer::renderable::Renderable; +use crate::renderer::walkable::Walkable; use std::collections::HashMap; use std::ops::Index; @@ -93,3 +94,42 @@ impl<'a> DustRenderer<'a> { Ok("".to_owned()) } } + +fn walk_path<'a, C>(context: &'a C, path: &Vec<&str>) -> &'a C +where + C: Walkable, +{ + let mut output: &C = context; + + for elem in path.iter() { + output = context.walk(elem); + } + + output +} + +#[cfg(test)] +mod tests { + use super::*; + + impl<'a> Walkable for HashMap<&str, &str> { + type Output = str; + + fn walk(&self, segment: &str) -> &str { + self.get(segment).unwrap() + } + } + + #[test] + fn test_walk_path() { + let context: HashMap<&str, &str> = [ + ("cat", "kitty"), + ("dog", "doggy"), + ("tiger", "murder kitty"), + ] + .iter() + .cloned() + .collect(); + assert_eq!(context.walk("cat"), "kitty"); + } +}