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:
parent
70cb107f86
commit
883bda7a78
@ -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<String, RenderError> {
|
||||
fn render(&self, filters: &Vec<Filter>) -> Result<String, RenderError> {
|
||||
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<Vec<String>, 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(","))
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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<Filter>,
|
||||
pub filters: Vec<Filter>,
|
||||
}
|
||||
|
||||
#[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<Body<'a>>,
|
||||
else_contents: Option<Body<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
struct NamedBlock<'a> {
|
||||
pub struct NamedBlock<'a> {
|
||||
name: &'a str,
|
||||
contents: Option<Body<'a>>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
struct ParameterizedBlock<'a> {
|
||||
pub struct ParameterizedBlock<'a> {
|
||||
name: &'a str,
|
||||
params: Vec<KVPair<'a>>,
|
||||
contents: Option<Body<'a>>,
|
||||
@ -107,7 +107,7 @@ struct ParameterizedBlock<'a> {
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
struct Partial<'a> {
|
||||
pub struct Partial<'a> {
|
||||
name: String,
|
||||
params: Vec<KVPair<'a>>,
|
||||
}
|
||||
|
@ -1,5 +1,6 @@
|
||||
use crate::parser::Filter;
|
||||
use crate::renderer::errors::RenderError;
|
||||
|
||||
pub trait Renderable {
|
||||
fn render(&self) -> Result<String, RenderError>;
|
||||
fn render(&self, filters: &Vec<Filter>) -> Result<String, RenderError>;
|
||||
}
|
||||
|
@ -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<I: ContextElement> ContextElement for HashMap<&str, I> {}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
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())
|
||||
}
|
||||
}
|
||||
|
||||
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 })
|
||||
}
|
||||
}
|
||||
@ -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()
|
||||
);
|
||||
|
Loading…
x
Reference in New Issue
Block a user