diff options
author | bott <mhpoin@gmail.com> | 2018-09-22 04:09:46 +0200 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-09-22 07:11:14 +0200 |
commit | 99612c53e89383ca344c3971a6d8c1b99ae41024 (patch) | |
tree | 21bd7e77a377d024665fa7eb7cdafc629e68dff6 /askama_shared | |
parent | 94bb47a78d32c5a5162ce5475b8a4c310940fef2 (diff) | |
download | askama-99612c53e89383ca344c3971a6d8c1b99ae41024.tar.gz askama-99612c53e89383ca344c3971a6d8c1b99ae41024.tar.bz2 askama-99612c53e89383ca344c3971a6d8c1b99ae41024.zip |
Add absolute value filter
Diffstat (limited to 'askama_shared')
-rw-r--r-- | askama_shared/Cargo.toml | 1 | ||||
-rw-r--r-- | askama_shared/src/filters/mod.rs | 22 | ||||
-rw-r--r-- | askama_shared/src/lib.rs | 1 |
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; |