Added the signature for passing filters into the render function.

Added the signature for passing filters into the render function but I am not yet using the filters.
This commit is contained in:
Tom Alexander 2020-04-12 21:54:15 -04:00
parent 70cb107f86
commit 883bda7a78
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
6 changed files with 30 additions and 22 deletions

View File

@ -1,5 +1,6 @@
extern crate nom; extern crate nom;
use parser::Filter;
use renderer::compile_template; use renderer::compile_template;
use renderer::CompiledTemplate; use renderer::CompiledTemplate;
use renderer::ContextElement; use renderer::ContextElement;
@ -73,15 +74,16 @@ fn read_context_from_stdin() -> serde_json::Value {
impl ContextElement for serde_json::Value {} impl ContextElement for serde_json::Value {}
impl Renderable for serde_json::Value { impl Renderable for serde_json::Value {
fn render(&self) -> Result<String, RenderError> { fn render(&self, filters: &Vec<Filter>) -> Result<String, RenderError> {
match self { match self {
serde_json::Value::Null => Ok("".to_owned()), serde_json::Value::Null => Ok("".to_owned()),
serde_json::Value::Bool(boolean) => Ok(boolean.to_string()), serde_json::Value::Bool(boolean) => Ok(boolean.to_string()),
serde_json::Value::Number(num) => Ok(num.to_string()), serde_json::Value::Number(num) => Ok(num.to_string()),
serde_json::Value::String(string) => Ok(string.to_string()), serde_json::Value::String(string) => Ok(string.to_string()),
serde_json::Value::Array(arr) => { serde_json::Value::Array(arr) => {
// TODO: Handle the filters instead of passing a Vec::new()
let rendered: Result<Vec<String>, RenderError> = let rendered: Result<Vec<String>, RenderError> =
arr.iter().map(|val| val.render()).collect(); arr.iter().map(|val| val.render(&Vec::new())).collect();
let rendered_slice: &[String] = &rendered?; let rendered_slice: &[String] = &rendered?;
Ok(rendered_slice.join(",")) Ok(rendered_slice.join(","))
} }

View File

@ -1,7 +1,4 @@
extern crate nom; extern crate nom;
mod parser; pub mod parser;
mod renderer; pub mod renderer;
pub use renderer::compile_template;
pub use renderer::DustRenderer;

View File

@ -9,5 +9,6 @@ pub use node_invoker::Result;
pub use parser::template; pub use parser::template;
pub use parser::Body; pub use parser::Body;
pub use parser::DustTag; pub use parser::DustTag;
pub use parser::Filter;
pub use parser::Template; pub use parser::Template;
pub use parser::TemplateElement; pub use parser::TemplateElement;

View File

@ -45,7 +45,7 @@ pub enum DustTag<'a> {
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
enum Special { pub enum Special {
Space, Space,
NewLine, NewLine,
CarriageReturn, CarriageReturn,
@ -66,11 +66,11 @@ pub struct Path<'a> {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub struct Reference<'a> { pub struct Reference<'a> {
pub path: Path<'a>, pub path: Path<'a>,
filters: Vec<Filter>, pub filters: Vec<Filter>,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
enum Filter { pub enum Filter {
HtmlEncode, HtmlEncode,
DisableHtmlEncode, DisableHtmlEncode,
JavascriptStringEncode, JavascriptStringEncode,
@ -86,20 +86,20 @@ pub struct Span<'a> {
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
struct Container<'a> { pub struct Container<'a> {
path: Path<'a>, path: Path<'a>,
contents: Option<Body<'a>>, contents: Option<Body<'a>>,
else_contents: Option<Body<'a>>, else_contents: Option<Body<'a>>,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
struct NamedBlock<'a> { pub struct NamedBlock<'a> {
name: &'a str, name: &'a str,
contents: Option<Body<'a>>, contents: Option<Body<'a>>,
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
struct ParameterizedBlock<'a> { pub struct ParameterizedBlock<'a> {
name: &'a str, name: &'a str,
params: Vec<KVPair<'a>>, params: Vec<KVPair<'a>>,
contents: Option<Body<'a>>, contents: Option<Body<'a>>,
@ -107,7 +107,7 @@ struct ParameterizedBlock<'a> {
} }
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
struct Partial<'a> { pub struct Partial<'a> {
name: String, name: String,
params: Vec<KVPair<'a>>, params: Vec<KVPair<'a>>,
} }

View File

@ -1,5 +1,6 @@
use crate::parser::Filter;
use crate::renderer::errors::RenderError; use crate::renderer::errors::RenderError;
pub trait Renderable { pub trait Renderable {
fn render(&self) -> Result<String, RenderError>; fn render(&self, filters: &Vec<Filter>) -> Result<String, RenderError>;
} }

View File

@ -90,7 +90,7 @@ impl<'a> DustRenderer<'a> {
// If reference does not exist in the context, it becomes an empty string // If reference does not exist in the context, it becomes an empty string
return Ok("".to_owned()); return Ok("".to_owned());
} else { } else {
return val?.render(); return val?.render(&reference.filters);
} }
} }
_ => (), // TODO: Implement the rest _ => (), // TODO: Implement the rest
@ -115,6 +115,7 @@ fn walk_path<'a>(
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::parser::Filter;
#[test] #[test]
fn test_walk_path() { fn test_walk_path() {
@ -123,19 +124,22 @@ mod tests {
impl<I: ContextElement> ContextElement for HashMap<&str, I> {} impl<I: ContextElement> ContextElement for HashMap<&str, I> {}
impl Renderable for u32 { impl Renderable for u32 {
fn render(&self) -> Result<String, RenderError> { fn render(&self, filters: &Vec<Filter>) -> Result<String, RenderError> {
// TODO: handle the filters
Ok(self.to_string()) Ok(self.to_string())
} }
} }
impl Renderable for &str { impl Renderable for &str {
fn render(&self) -> Result<String, RenderError> { fn render(&self, filters: &Vec<Filter>) -> Result<String, RenderError> {
// TODO: handle the filters
Ok(self.to_string()) Ok(self.to_string())
} }
} }
impl<I: ContextElement> Renderable for HashMap<&str, I> { impl<I: ContextElement> Renderable for HashMap<&str, I> {
fn render(&self) -> Result<String, RenderError> { fn render(&self, filters: &Vec<Filter>) -> Result<String, RenderError> {
// TODO: handle the filters
Err(RenderError::CantRender { elem: self }) Err(RenderError::CantRender { elem: self })
} }
} }
@ -186,20 +190,23 @@ mod tests {
.cloned() .cloned()
.collect(); .collect();
assert_eq!( assert_eq!(
walk_path(&context, &vec!["cat"]).unwrap().render().unwrap(), walk_path(&context, &vec!["cat"])
.unwrap()
.render(&Vec::new())
.unwrap(),
"kitty".to_owned() "kitty".to_owned()
); );
assert_eq!( assert_eq!(
walk_path(&number_context, &vec!["tiger"]) walk_path(&number_context, &vec!["tiger"])
.unwrap() .unwrap()
.render() .render(&Vec::new())
.unwrap(), .unwrap(),
"3".to_owned() "3".to_owned()
); );
assert_eq!( assert_eq!(
walk_path(&deep_context, &vec!["tiger", "food"]) walk_path(&deep_context, &vec!["tiger", "food"])
.unwrap() .unwrap()
.render() .render(&Vec::new())
.unwrap(), .unwrap(),
"people".to_owned() "people".to_owned()
); );