Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add int and float filters #146

Merged
merged 2 commits into from
Nov 5, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 44 additions & 1 deletion askama_shared/src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ mod json;
#[cfg(feature = "serde-json")]
pub use self::json::json;

use error::Error::Fmt;
use num_traits::cast::NumCast;
use num_traits::Signed;
use std::fmt;

Expand All @@ -20,14 +22,16 @@ 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; 19] = [
pub const BUILT_IN_FILTERS: [&str; 21] = [
"abs",
"capitalize",
"center",
"e",
"escape",
"format",
"indent",
"into_f64",
"into_isize",
"join",
"linebreaks",
"linebreaksbr",
Expand Down Expand Up @@ -158,6 +162,22 @@ 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
T: NumCast,
{
number.to_isize().ok_or(Fmt(fmt::Error))
}

/// Joins iterable into a string separated by provided argument
pub fn join<T, I, S>(input: I, separator: S) -> Result<String>
where
Expand Down Expand Up @@ -246,6 +266,7 @@ pub fn wordcount(s: &fmt::Display) -> Result<usize> {
#[cfg(test)]
mod tests {
use super::*;
use std::f64::INFINITY;

#[test]
fn test_linebreaks() {
Expand Down Expand Up @@ -300,6 +321,28 @@ 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);
assert_eq!(into_isize(-1.9).unwrap(), -1 as isize);
assert_eq!(into_isize(1.5 as f64).unwrap(), 1 as isize);
assert_eq!(into_isize(-1.5 as f64).unwrap(), -1 as isize);
match into_isize(INFINITY) {
Err(Fmt(fmt::Error)) => assert!(true),
_ => assert!(false, "Should return error of type Err(Fmt(fmt::Error))"),
};
}

#[test]
fn test_join() {
assert_eq!(
Expand Down