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
6 changed files with 30 additions and 22 deletions

View File

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

View File

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