diff options
author | Tomas <maklins08@gmail.com> | 2018-11-05 14:07:19 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-11-05 20:24:39 +0100 |
commit | 962c24eaed9bcf30e82dc5ed06c6513b00dba3cf (patch) | |
tree | 8b09de21e820c5676df18bcb7d80b5d7da5fd5e7 /askama_shared | |
parent | 1b3c5a86963dc7623e7cc8d1155c19f85830df9e (diff) | |
download | askama-962c24eaed9bcf30e82dc5ed06c6513b00dba3cf.tar.gz askama-962c24eaed9bcf30e82dc5ed06c6513b00dba3cf.tar.bz2 askama-962c24eaed9bcf30e82dc5ed06c6513b00dba3cf.zip |
Add f64 filter
Diffstat (limited to 'askama_shared')
-rw-r--r-- | askama_shared/src/filters/mod.rs | 20 |
1 files changed, 19 insertions, 1 deletions
diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs index 404abd7..a6d2ba6 100644 --- a/askama_shared/src/filters/mod.rs +++ b/askama_shared/src/filters/mod.rs @@ -22,7 +22,7 @@ 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; 20] = [ +pub const BUILT_IN_FILTERS: [&str; 21] = [ "abs", "capitalize", "center", @@ -30,6 +30,7 @@ pub const BUILT_IN_FILTERS: [&str; 20] = [ "escape", "format", "indent", + "into_f64", "into_isize", "join", "linebreaks", @@ -161,6 +162,14 @@ pub fn indent(s: &fmt::Display, width: &usize) -> Result<String> { Ok(indented) } +/// Casts number to f64 +pub fn into_f64<T>(number: T) -> Result<f64> +where + T: NumCast, +{ + number.to_f64().ok_or(Fmt(fmt::Error)) +} + /// Casts number to isize pub fn into_isize<T>(number: T) -> Result<isize> where @@ -313,6 +322,15 @@ mod tests { } #[test] + fn test_into_f64() { + assert_eq!(into_f64(1).unwrap(), 1.0 as f64); + assert_eq!(into_f64(1.9).unwrap(), 1.9 as f64); + assert_eq!(into_f64(-1.9).unwrap(), -1.9 as f64); + assert_eq!(into_f64(INFINITY as f32).unwrap(), INFINITY); + assert_eq!(into_f64(-INFINITY as f32).unwrap(), -INFINITY); + } + + #[test] fn test_into_isize() { assert_eq!(into_isize(1).unwrap(), 1 as isize); assert_eq!(into_isize(1.9).unwrap(), 1 as isize); |