initial walkable implementation

This commit is contained in:
Tom Alexander 2020-04-11 20:31:44 -04:00
parent 15c8ee57a7
commit d5c3985c29
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
2 changed files with 42 additions and 0 deletions

View File

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

View File

@ -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<Output = C>,
{
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");
}
}