Skip to content

Commit

Permalink
add date filter
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Stepanov committed Jun 13, 2016
1 parent a9aae23 commit d823bf6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ build = "build.rs"
[dependencies]
regex = "0.1"
lazy_static = "0.1.15"
chrono = "0.2.22"
clippy = {version = "0.0", optional = true}

[build-dependencies]
Expand Down
26 changes: 26 additions & 0 deletions src/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::error::Error;
use value::Value;
use value::Value::*;

use chrono::DateTime;

use self::FilterError::*;

#[derive(Debug)]
Expand Down Expand Up @@ -232,6 +234,25 @@ pub fn last(input: &Value, _args: &[Value]) -> FilterResult {
}
}

pub fn date(input: &Value, args: &[Value]) -> FilterResult {
if args.len() != 1 {
return Err(FilterError::InvalidArgumentCount(format!("expected 1, {} given", args.len())));
}

let date = match input {
&Value::Str(ref s) => try!(DateTime::parse_from_str(&s, "%d %B %Y %H:%M:%S %z")
.map_err(|e| FilterError::InvalidType(format!("Invalid date format: {}", e)))),
_ => return Err(FilterError::InvalidType("String expected".to_owned())),
};

let format = match args[0] {
Value::Str(ref s) => s,
_ => return Err(InvalidArgument(0, "Str expected".to_owned())),
};

Ok(Value::Str(format!("{}", date.format(format))))
}

#[cfg(test)]
mod tests {

Expand Down Expand Up @@ -383,4 +404,9 @@ mod tests {
assert_eq!(unit!(last, Array(vec![tos!("test"), tos!("last")])), tos!("last"));
assert_eq!(unit!(last, Array(vec![])), tos!(""));
}

#[test]
fn unit_date() {
assert_eq!(unit!(date, tos!("13 Jun 2016 02:30:00 +0300"), &[tos!("%Y-%m-%d")]), tos!("2016-06-13"));
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
#[macro_use]
extern crate lazy_static;
extern crate regex;
extern crate chrono;

use std::collections::HashMap;
use lexer::Element;
Expand Down
3 changes: 2 additions & 1 deletion src/template.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use Renderable;
use context::Context;
use filters::{size, upcase, downcase, capitalize, minus, plus, times, divided_by, ceil, floor, round, prepend, append, first, last, pluralize, replace};
use filters::{size, upcase, downcase, capitalize, minus, plus, times, divided_by, ceil, floor, round, prepend, append, first, last, pluralize, replace, date};
use error::Result;

pub struct Template {
Expand All @@ -27,6 +27,7 @@ impl Renderable for Template {
context.add_filter("append", Box::new(append));
context.add_filter("replace", Box::new(replace));
context.add_filter("pluralize", Box::new(pluralize));
context.add_filter("date", Box::new(date));

let mut buf = String::new();
for el in &self.elements {
Expand Down

0 comments on commit d823bf6

Please sign in to comment.