aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-21 20:57:38 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2017-08-21 20:59:25 +0200
commitec5a2ebbb375d5ad49daeba72ec6aeb874d4a81e (patch)
treecd941e6011138a7a212c8cbe6b65bde91413d60b
parent4f4b5a3a65eed09fbb9578ce9df72076e0b05f66 (diff)
downloadaskama-ec5a2ebbb375d5ad49daeba72ec6aeb874d4a81e.tar.gz
askama-ec5a2ebbb375d5ad49daeba72ec6aeb874d4a81e.tar.bz2
askama-ec5a2ebbb375d5ad49daeba72ec6aeb874d4a81e.zip
Change filters to return Result
Diffstat (limited to '')
-rw-r--r--askama/src/filters.rs51
-rw-r--r--askama_derive/src/generator.rs3
2 files changed, 29 insertions, 25 deletions
diff --git a/askama/src/filters.rs b/askama/src/filters.rs
index ccbf1a0..b8673e6 100644
--- a/askama/src/filters.rs
+++ b/askama/src/filters.rs
@@ -3,13 +3,14 @@
//! Contains all the built-in filter functions for use in templates.
//! Currently, there is no way to define filters outside this module.
use std::fmt;
+use super::Result;
fn escapable(b: &u8) -> bool {
*b == b'<' || *b == b'>' || *b == b'&'
}
/// Escapes `&`, `<` and `>` in strings
-pub fn escape(s: &fmt::Display) -> String {
+pub fn escape(s: &fmt::Display) -> Result<String> {
let s = format!("{}", s);
let mut found = Vec::new();
for (i, b) in s.as_bytes().iter().enumerate() {
@@ -18,7 +19,7 @@ pub fn escape(s: &fmt::Display) -> String {
}
}
if found.is_empty() {
- return s;
+ return Ok(s);
}
let bytes = s.as_bytes();
@@ -41,11 +42,11 @@ pub fn escape(s: &fmt::Display) -> String {
res.extend(&bytes[start..]);
}
- String::from_utf8(res).unwrap()
+ Ok(String::from_utf8(res).unwrap())
}
/// Alias for the `escape()` filter
-pub fn e(s: &fmt::Display) -> String {
+pub fn e(s: &fmt::Display) -> Result<String> {
escape(s)
}
@@ -58,31 +59,31 @@ pub fn e(s: &fmt::Display) -> String {
pub fn format() { }
/// Converts to lowercase.
-pub fn lower(s: &fmt::Display) -> String {
+pub fn lower(s: &fmt::Display) -> Result<String> {
let s = format!("{}", s);
- s.to_lowercase()
+ Ok(s.to_lowercase())
}
/// Alias for the `lower()` filter.
-pub fn lowercase(s: &fmt::Display) -> String {
+pub fn lowercase(s: &fmt::Display) -> Result<String> {
lower(s)
}
/// Converts to uppercase.
-pub fn upper(s: &fmt::Display) -> String {
+pub fn upper(s: &fmt::Display) -> Result<String> {
let s = format!("{}", s);
- s.to_uppercase()
+ Ok(s.to_uppercase())
}
/// Alias for the `upper()` filter.
-pub fn uppercase(s: &fmt::Display) -> String {
+pub fn uppercase(s: &fmt::Display) -> Result<String> {
upper(s)
}
/// Strip leading and trailing whitespace.
-pub fn trim(s: &fmt::Display) -> String {
+pub fn trim(s: &fmt::Display) -> Result<String> {
let s = format!("{}", s);
- s.trim().to_owned()
+ Ok(s.trim().to_owned())
}
#[cfg(test)]
@@ -90,30 +91,30 @@ mod tests {
use super::*;
#[test]
fn test_escape() {
- assert_eq!(escape(&""), "");
- assert_eq!(escape(&"<&>"), "&lt;&amp;&gt;");
- assert_eq!(escape(&"bla&"), "bla&amp;");
- assert_eq!(escape(&"<foo"), "&lt;foo");
+ assert_eq!(escape(&"").unwrap(), "");
+ assert_eq!(escape(&"<&>").unwrap(), "&lt;&amp;&gt;");
+ assert_eq!(escape(&"bla&").unwrap(), "bla&amp;");
+ assert_eq!(escape(&"<foo").unwrap(), "&lt;foo");
}
#[test]
fn test_lower() {
- assert_eq!(lower(&"Foo"), "foo");
- assert_eq!(lower(&"FOO"), "foo");
- assert_eq!(lower(&"FooBar"), "foobar");
- assert_eq!(lower(&"foo"), "foo");
+ assert_eq!(lower(&"Foo").unwrap(), "foo");
+ assert_eq!(lower(&"FOO").unwrap(), "foo");
+ assert_eq!(lower(&"FooBar").unwrap(), "foobar");
+ assert_eq!(lower(&"foo").unwrap(), "foo");
}
#[test]
fn test_upper() {
- assert_eq!(upper(&"Foo"), "FOO");
- assert_eq!(upper(&"FOO"), "FOO");
- assert_eq!(upper(&"FooBar"), "FOOBAR");
- assert_eq!(upper(&"foo"), "FOO");
+ assert_eq!(upper(&"Foo").unwrap(), "FOO");
+ assert_eq!(upper(&"FOO").unwrap(), "FOO");
+ assert_eq!(upper(&"FooBar").unwrap(), "FOOBAR");
+ assert_eq!(upper(&"foo").unwrap(), "FOO");
}
#[test]
fn test_trim() {
- assert_eq!(trim(&" Hello\tworld\t"), "Hello\tworld");
+ assert_eq!(trim(&" Hello\tworld\t").unwrap(), "Hello\tworld");
}
}
diff --git a/askama_derive/src/generator.rs b/askama_derive/src/generator.rs
index 60d028b..3288e22 100644
--- a/askama_derive/src/generator.rs
+++ b/askama_derive/src/generator.rs
@@ -227,6 +227,9 @@ impl<'a> Generator<'a> {
self.visit_expr(arg);
}
self.write(")");
+ if name != "format" {
+ self.write("?");
+ }
}
fn visit_binop(&mut self, op: &str, left: &Expr, right: &Expr) {