diff --git a/src/bin.rs b/src/bin.rs index 1fa2daa..eb4bbea 100644 --- a/src/bin.rs +++ b/src/bin.rs @@ -1,5 +1,6 @@ extern crate nom; +use parser::Filter; use renderer::compile_template; use renderer::CompiledTemplate; use renderer::ContextElement; @@ -73,15 +74,16 @@ fn read_context_from_stdin() -> serde_json::Value { impl ContextElement for serde_json::Value {} impl Renderable for serde_json::Value { - fn render(&self) -> Result { + fn render(&self, filters: &Vec) -> Result { match self { serde_json::Value::Null => Ok("".to_owned()), serde_json::Value::Bool(boolean) => Ok(boolean.to_string()), serde_json::Value::Number(num) => Ok(num.to_string()), serde_json::Value::String(string) => Ok(string.to_string()), serde_json::Value::Array(arr) => { + // TODO: Handle the filters instead of passing a Vec::new() let rendered: Result, RenderError> = - arr.iter().map(|val| val.render()).collect(); + arr.iter().map(|val| val.render(&Vec::new())).collect(); let rendered_slice: &[String] = &rendered?; Ok(rendered_slice.join(",")) } diff --git a/src/lib.rs b/src/lib.rs index 745bad9..9299281 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,4 @@ extern crate nom; -mod parser; -mod renderer; - -pub use renderer::compile_template; -pub use renderer::DustRenderer; +pub mod parser; +pub mod renderer; diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 193516e..7d3b3da 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -9,5 +9,6 @@ pub use node_invoker::Result; pub use parser::template; pub use parser::Body; pub use parser::DustTag; +pub use parser::Filter; pub use parser::Template; pub use parser::TemplateElement; diff --git a/src/parser/parser.rs b/src/parser/parser.rs index 211edac..4e49bc7 100644 --- a/src/parser/parser.rs +++ b/src/parser/parser.rs @@ -45,7 +45,7 @@ pub enum DustTag<'a> { } #[derive(Clone, Debug, PartialEq)] -enum Special { +pub enum Special { Space, NewLine, CarriageReturn, @@ -66,11 +66,11 @@ pub struct Path<'a> { #[derive(Clone, Debug, PartialEq)] pub struct Reference<'a> { pub path: Path<'a>, - filters: Vec, + pub filters: Vec, } #[derive(Clone, Debug, PartialEq)] -enum Filter { +pub enum Filter { HtmlEncode, DisableHtmlEncode, JavascriptStringEncode, @@ -86,20 +86,20 @@ pub struct Span<'a> { } #[derive(Clone, Debug, PartialEq)] -struct Container<'a> { +pub struct Container<'a> { path: Path<'a>, contents: Option>, else_contents: Option>, } #[derive(Clone, Debug, PartialEq)] -struct NamedBlock<'a> { +pub struct NamedBlock<'a> { name: &'a str, contents: Option>, } #[derive(Clone, Debug, PartialEq)] -struct ParameterizedBlock<'a> { +pub struct ParameterizedBlock<'a> { name: &'a str, params: Vec>, contents: Option>, @@ -107,7 +107,7 @@ struct ParameterizedBlock<'a> { } #[derive(Clone, Debug, PartialEq)] -struct Partial<'a> { +pub struct Partial<'a> { name: String, params: Vec>, } diff --git a/src/renderer/renderable.rs b/src/renderer/renderable.rs index e9a2c87..228f8db 100644 --- a/src/renderer/renderable.rs +++ b/src/renderer/renderable.rs @@ -1,5 +1,6 @@ +use crate::parser::Filter; use crate::renderer::errors::RenderError; pub trait Renderable { - fn render(&self) -> Result; + fn render(&self, filters: &Vec) -> Result; } diff --git a/src/renderer/renderer.rs b/src/renderer/renderer.rs index 467daa7..ea9acbb 100644 --- a/src/renderer/renderer.rs +++ b/src/renderer/renderer.rs @@ -90,7 +90,7 @@ impl<'a> DustRenderer<'a> { // If reference does not exist in the context, it becomes an empty string return Ok("".to_owned()); } else { - return val?.render(); + return val?.render(&reference.filters); } } _ => (), // TODO: Implement the rest @@ -115,6 +115,7 @@ fn walk_path<'a>( #[cfg(test)] mod tests { use super::*; + use crate::parser::Filter; #[test] fn test_walk_path() { @@ -123,19 +124,22 @@ mod tests { impl ContextElement for HashMap<&str, I> {} impl Renderable for u32 { - fn render(&self) -> Result { + fn render(&self, filters: &Vec) -> Result { + // TODO: handle the filters Ok(self.to_string()) } } impl Renderable for &str { - fn render(&self) -> Result { + fn render(&self, filters: &Vec) -> Result { + // TODO: handle the filters Ok(self.to_string()) } } impl Renderable for HashMap<&str, I> { - fn render(&self) -> Result { + fn render(&self, filters: &Vec) -> Result { + // TODO: handle the filters Err(RenderError::CantRender { elem: self }) } } @@ -186,20 +190,23 @@ mod tests { .cloned() .collect(); assert_eq!( - walk_path(&context, &vec!["cat"]).unwrap().render().unwrap(), + walk_path(&context, &vec!["cat"]) + .unwrap() + .render(&Vec::new()) + .unwrap(), "kitty".to_owned() ); assert_eq!( walk_path(&number_context, &vec!["tiger"]) .unwrap() - .render() + .render(&Vec::new()) .unwrap(), "3".to_owned() ); assert_eq!( walk_path(&deep_context, &vec!["tiger", "food"]) .unwrap() - .render() + .render(&Vec::new()) .unwrap(), "people".to_owned() );