Skip to content

Commit

Permalink
Add f64 filter
Browse files Browse the repository at this point in the history
  • Loading branch information
tizgafa committed Nov 5, 2018
1 parent 974e37b commit 2120853
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions askama_shared/src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub const BUILT_IN_FILTERS: [&str; 21] = [
"escape",
"format",
"indent",
"into_f64",
"into_isize",
"join",
"linebreaks",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -312,6 +321,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);
Expand Down

0 comments on commit 2120853

Please sign in to comment.