Skip to content

Commit

Permalink
Merge pull request #182 from epage/cgen
Browse files Browse the repository at this point in the history
Expose `{{  "now" | date "%Y" }}` for carg-generate
  • Loading branch information
epage authored Jul 30, 2018
2 parents a9c61be + 6a1e0a0 commit 46212e2
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ extern crate serde;
extern crate serde_yaml;

mod error;
mod filters;
mod parser;
mod tags;
mod template;
mod value;

pub mod compiler;
pub mod filters;
pub mod interpreter;
pub mod tags;

pub use error::Error;
pub use parser::{Parser, ParserBuilder};
Expand Down
40 changes: 35 additions & 5 deletions src/value/scalar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,21 @@ mod friendly_date {
}

fn parse_date(s: &str) -> Option<Date> {
let formats = ["%d %B %Y %H:%M:%S %z", "%Y-%m-%d %H:%M:%S %z"];
formats
.iter()
.filter_map(|f| Date::parse_from_str(s, f).ok())
.next()
match s {
"now" | "today" => {
let now = chrono::offset::Utc::now();
let now = now.naive_utc();
let now = chrono::DateTime::from_utc(now, chrono::offset::FixedOffset::east(0));
Some(now)
}
_ => {
let formats = ["%d %B %Y %H:%M:%S %z", "%Y-%m-%d %H:%M:%S %z"];
formats
.iter()
.filter_map(|f| Date::parse_from_str(s, f).ok())
.next()
}
}
}

#[cfg(test)]
Expand Down Expand Up @@ -457,4 +467,24 @@ mod test {
assert_eq!(empty, TRUE);
assert!(empty.is_truthy());
}

#[test]
fn parse_date_empty_is_bad() {
assert!(parse_date("").is_none());
}

#[test]
fn parse_date_bad() {
assert!(parse_date("aaaaa").is_none());
}

#[test]
fn parse_date_now() {
assert!(parse_date("now").is_some());
}

#[test]
fn parse_date_today() {
assert!(parse_date("today").is_some());
}
}

0 comments on commit 46212e2

Please sign in to comment.