Skip to content

Commit

Permalink
Remove make_* functions
Browse files Browse the repository at this point in the history
And add a currently failing test that
needs to pass for 1.0
  • Loading branch information
Vincent Prouillet committed Nov 19, 2018
1 parent 780c5d6 commit f0995f8
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 114 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- Now requires Rust 1.30
- Removed error-chain errors and added rich Error enum instead
- Filter, Tester and Function are now traits and now take borrowed values instead of owned

## 0.11.19 (2018-10-31)

Expand Down
7 changes: 3 additions & 4 deletions examples/basic/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ extern crate serde_json;

use std::collections::HashMap;

use tera::{Tera, Context, make_filter, Result};
use serde_json::value::{Value, to_value};

use serde_json::value::{to_value, Value};
use tera::{Context, Result, Tera};

lazy_static! {
pub static ref TEMPLATES: Tera = {
Expand All @@ -20,7 +19,7 @@ lazy_static! {
}
};
tera.autoescape_on(vec!["html", ".sql"]);
tera.register_filter("do_nothing", &make_filter(do_nothing_filter));
tera.register_filter("do_nothing", do_nothing_filter);
tera
};
}
Expand Down
5 changes: 4 additions & 1 deletion src/builtins/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ pub trait Filter: Sync + Send {
fn filter(&self, value: &Value, args: &HashMap<String, Value>) -> Result<Value>;
}

impl<F> Filter for F where F: Fn(&Value, &HashMap<String, Value>) -> Result<Value> + Sync + Send {
impl<F> Filter for F
where
F: Fn(&Value, &HashMap<String, Value>) -> Result<Value> + Sync + Send,
{
fn filter(&self, value: &Value, args: &HashMap<String, Value>) -> Result<Value> {
self(value, args)
}
Expand Down
27 changes: 15 additions & 12 deletions src/builtins/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ pub trait Function: Sync + Send {
fn call(&self, args: &HashMap<String, Value>) -> Result<Value>;
}

impl<F> Function for F where F: Fn(&HashMap<String, Value>) -> Result<Value> + Sync + Send {
impl<F> Function for F
where
F: Fn(&HashMap<String, Value>) -> Result<Value> + Sync + Send,
{
fn call(&self, args: &HashMap<String, Value>) -> Result<Value> {
self(args)
}
Expand All @@ -23,9 +26,9 @@ pub fn range(args: &HashMap<String, Value>) -> Result<Value> {
Ok(v) => v,
Err(_) => {
return Err(Error::msg(format!(
"Global function `range` received start={} but `start` can only be a number",
val
)))
"Global function `range` received start={} but `start` can only be a number",
val
)))
}
},
None => 0,
Expand All @@ -35,9 +38,9 @@ pub fn range(args: &HashMap<String, Value>) -> Result<Value> {
Ok(v) => v,
Err(_) => {
return Err(Error::msg(format!(
"Global function `range` received step_by={} but `step` can only be a number",
val
)))
"Global function `range` received step_by={} but `step` can only be a number",
val
)))
}
},
None => 1,
Expand All @@ -53,9 +56,7 @@ pub fn range(args: &HashMap<String, Value>) -> Result<Value> {
}
},
None => {
return Err(Error::msg(
"Global function `range` was called without a `end` argument",
))
return Err(Error::msg("Global function `range` was called without a `end` argument"))
}
};

Expand Down Expand Up @@ -89,7 +90,8 @@ pub fn now(args: &HashMap<String, Value>) -> Result<Value> {
Some(val) => match from_value::<bool>(val.clone()) {
Ok(v) => v,
Err(_) => return Err(Error::msg(format!(
"Global function `now` received timestamp={} but `timestamp` can only be a boolean", val
"Global function `now` received timestamp={} but `timestamp` can only be a boolean",
val
))),
},
None => false,
Expand All @@ -115,7 +117,8 @@ pub fn throw(args: &HashMap<String, Value>) -> Result<Value> {
Some(val) => match from_value::<String>(val.clone()) {
Ok(v) => Err(Error::msg(v)),
Err(_) => Err(Error::msg(format!(
"Global function `throw` received message={} but `message` can only be a string", val
"Global function `throw` received message={} but `message` can only be a string",
val
))),
},
None => Err(Error::msg("Global function `throw` was called without a `message` argument")),
Expand Down
24 changes: 14 additions & 10 deletions src/builtins/testers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ pub trait Test: Sync + Send {
fn test(&self, value: Option<&Value>, args: &[Value]) -> Result<bool>;
}

impl<F> Test for F where F: Fn(Option<&Value>, &[Value]) -> Result<bool> + Sync + Send {
impl<F> Test for F
where
F: Fn(Option<&Value>, &[Value]) -> Result<bool> + Sync + Send,
{
fn test(&self, value: Option<&Value>, args: &[Value]) -> Result<bool> {
self(value, args)
}
Expand Down Expand Up @@ -259,17 +262,17 @@ mod tests {
&[to_value("hello").unwrap()],
)
.unwrap());
assert!(!starting_with(Some(&to_value("hello").unwrap()), &[to_value("hi").unwrap()],)
.unwrap());
assert!(
!starting_with(Some(&to_value("hello").unwrap()), &[to_value("hi").unwrap()],).unwrap()
);
}

#[test]
fn test_ending_with() {
assert!(ending_with(
Some(&to_value("helloworld").unwrap()),
&[to_value("world").unwrap()],
)
.unwrap());
assert!(
ending_with(Some(&to_value("helloworld").unwrap()), &[to_value("world").unwrap()],)
.unwrap()
);
assert!(
!ending_with(Some(&to_value("hello").unwrap()), &[to_value("hi").unwrap()],).unwrap()
);
Expand Down Expand Up @@ -315,7 +318,8 @@ mod tests {
assert_eq!(matching(Some(&container), &[needle]).unwrap(), expected);
}

assert!(matching(Some(&to_value("").unwrap()), &[to_value("(Invalid regex").unwrap()])
.is_err());
assert!(
matching(Some(&to_value("").unwrap()), &[to_value("(Invalid regex").unwrap()]).is_err()
);
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub use errors::{Error, ErrorKind, Result};
pub use serde_json::value::{from_value, to_value, Map, Number, Value};
#[doc(hidden)]
pub use template::Template;
pub use tera::{Tera, make_test, make_function, make_filter};
pub use tera::Tera;
pub use utils::escape_html;

// Exposes the AST if one needs it but changing the AST is not considered
Expand Down
44 changes: 30 additions & 14 deletions src/renderer/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,24 @@ use std::sync::atomic::{AtomicUsize, Ordering};

use serde_json::Value;

use builtins::functions::Function;
use context::Context;
use errors::Result;
use tera::{Tera, make_function};
use builtins::functions::Function;
use tera::{Tera};

use super::Review;

fn render_template(content: &str, context: &Context) -> Result<String> {
let mut tera = Tera::default();
tera.add_raw_template("hello.html", content).unwrap();
tera.register_function("get_number", &make_function(|_: &HashMap<String, Value>| Ok(Value::Number(10.into()))));
tera.register_function("get_string", &make_function(|_: &HashMap<String, Value>| Ok(Value::String("Hello".to_string()))));
tera.register_function(
"get_number",
|_: &HashMap<String, Value>| Ok(Value::Number(10.into())),
);
tera.register_function(
"get_string",
|_: &HashMap<String, Value>| Ok(Value::String("Hello".to_string())),
);

tera.render("hello.html", context)
}
Expand Down Expand Up @@ -655,21 +661,31 @@ fn can_use_concat_to_push_to_array() {
assert_eq!(result.unwrap(), "[0, 1, 2, 3, 4]");
}

#[test]
fn stateful_global_fn() {
struct Next(AtomicUsize);
struct Next(AtomicUsize);

impl Function for Next {
fn call(&self, _args: &HashMap<String, Value>) -> Result<Value> {
Ok(Value::Number(self.0.fetch_add(1, Ordering::Relaxed).into()))
}
impl Function for Next {
fn call(&self, _args: &HashMap<String, Value>) -> Result<Value> {
Ok(Value::Number(self.0.fetch_add(1, Ordering::Relaxed).into()))
}
}

lazy_static! {
static ref NEXT_GLOBAL: Next = Next(AtomicUsize::new(1));
}

#[test]
fn stateful_global_fn() {
let mut tera = Tera::default();
tera.add_raw_template("fn.html", "<h1>{{ get_next() }}, {{ get_next() }}, {{ get_next() }}...</h1>").unwrap();
tera.register_function("get_next", &make_function(Next(AtomicUsize::new(1))));
tera.add_raw_template(
"fn.html",
"<h1>{{ get_next() }}, {{ get_next_borrowed() }}, {{ get_next() }}...</h1>",
)
.unwrap();

tera.register_function("get_next", Next(AtomicUsize::new(1)));
tera.register_function("get_next_borrowed", &NEXT_GLOBAL);

let result = tera.render("fn.html", &Context::new());

assert_eq!(result.unwrap(), "<h1>1, 2, 3...</h1>".to_owned());
assert_eq!(result.unwrap(), "<h1>1, 1, 2...</h1>".to_owned());
}
Loading

0 comments on commit f0995f8

Please sign in to comment.