aboutsummaryrefslogtreecommitdiffstats
path: root/askama_shared
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--askama_shared/Cargo.toml1
-rw-r--r--askama_shared/src/filters/mod.rs22
-rw-r--r--askama_shared/src/lib.rs1
3 files changed, 23 insertions, 1 deletions
diff --git a/askama_shared/Cargo.toml b/askama_shared/Cargo.toml
index a206da6..9106e96 100644
--- a/askama_shared/Cargo.toml
+++ b/askama_shared/Cargo.toml
@@ -16,6 +16,7 @@ rocket = []
actix-web = []
[dependencies]
+num-traits = "0.2.6"
serde = "1.0"
serde_derive = "1.0"
serde_json = { version = "1.0", optional = true }
diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs
index 181e27b..6fccf76 100644
--- a/askama_shared/src/filters/mod.rs
+++ b/askama_shared/src/filters/mod.rs
@@ -10,6 +10,7 @@ mod json;
#[cfg(feature = "serde-json")]
pub use self::json::json;
+use num_traits::Signed;
use std::fmt;
use super::Result;
@@ -19,7 +20,8 @@ use escaping::{self, MarkupDisplay};
// Askama or should refer to a local `filters` module. It should contain all the
// filters shipped with Askama, even the optional ones (since optional inclusion
// in the const vector based on features seems impossible right now).
-pub const BUILT_IN_FILTERS: [&str; 14] = [
+pub const BUILT_IN_FILTERS: [&str; 15] = [
+ "abs",
"e",
"escape",
"format",
@@ -155,6 +157,14 @@ where
Ok(rv)
}
+/// Absolute value
+pub fn abs<T>(number: T) -> Result<T>
+where
+ T: Signed,
+{
+ Ok(number.abs())
+}
+
#[cfg(test)]
mod tests {
use super::*;
@@ -239,4 +249,14 @@ mod tests {
"foo, bar"
);
}
+
+ #[test]
+ fn test_abs() {
+ assert_eq!(abs(1).unwrap(), 1);
+ assert_eq!(abs(-1).unwrap(), 1);
+ assert_eq!(abs(1.0).unwrap(), 1.0);
+ assert_eq!(abs(-1.0).unwrap(), 1.0);
+ assert_eq!(abs(1.0 as f64).unwrap(), 1.0 as f64);
+ assert_eq!(abs(-1.0 as f64).unwrap(), 1.0 as f64);
+ }
}
diff --git a/askama_shared/src/lib.rs b/askama_shared/src/lib.rs
index ad8a1b5..5df6998 100644
--- a/askama_shared/src/lib.rs
+++ b/askama_shared/src/lib.rs
@@ -1,5 +1,6 @@
#![cfg_attr(feature = "cargo-clippy", allow(unused_parens))]
+extern crate num_traits;
extern crate serde;
#[macro_use]
extern crate serde_derive;