Skip to content

Commit

Permalink
Enable using day aliases
Browse files Browse the repository at this point in the history
  • Loading branch information
onatm committed Aug 9, 2021
1 parent b21f505 commit deaa42c
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 6 deletions.
9 changes: 9 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ edition = "2018"

[dependencies]
nom="^4"
linked_hash_set="^0.1.4"
linked_hash_set="^0.1.4"
lazy_static = "1.4.0"
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ croon consists of two main parts:
- CLI entry point
- Library to parse cron expressions

It has two external dependencies for development convenience: `nom` and `linked_hash_set`.
It has two external dependencies for development convenience: `nom`, `linked_hash_set` and `lazy_static`.

### How it works

Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#[macro_use]
extern crate lazy_static;

pub mod cron_table;
mod error;
mod expression;
Expand Down
59 changes: 55 additions & 4 deletions src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use nom::{types::CompleteStr, *};

use crate::{
Expand All @@ -17,28 +19,57 @@ impl Parser {
}
}

lazy_static! {
static ref ALIAS_MAP: HashMap<&'static str, u32> = {
let mut m = HashMap::new();
m.insert("MON", 1);
m.insert("TUE", 2);
m.insert("WED", 3);
m.insert("THU", 4);
m.insert("FRI", 5);
m.insert("SAT", 6);
m.insert("SUN", 0);
m
};
}

named!(
number<CompleteStr, u32>,
map_res!(ws!(digit), |x: CompleteStr| x.0.parse())
);

named!(
exact<CompleteStr, CronBaseExpression>,
do_parse!(n: number >> (CronBaseExpression::Exact(n)))
do_parse!(n: alt!(number | day) >> (CronBaseExpression::Exact(n)))
);

fn map_alias_to_number(alias: String) -> Result<u32, Error> {
if let Some(value) = ALIAS_MAP.get(&*alias) {
Ok(value.clone())
} else {
Err(Error)
}
}

named!(
day<CompleteStr, u32>,
do_parse!(
day: alt!(tag!("MON") | tag!("TUE") | tag!("WED") | tag!("THU") | tag!("FRI") | tag!("SAT") | tag!("SUN")) >> (map_alias_to_number(day.0.to_string()).unwrap())
)
);

named!(
range<CompleteStr, CronBaseExpression>,
complete!(do_parse!(
start: number >> tag!("-") >> end: number >> (CronBaseExpression::Range(start, end))
start: alt!(number | day) >> tag!("-") >> end: alt!(number| day) >> (CronBaseExpression::Range(start, end))
))
);

named!(all<CompleteStr, CronBaseExpression>, do_parse!(tag!("*") >> (CronBaseExpression::All)));

named!(
cron_base_expression<CompleteStr, CronBaseExpression>,
alt!(all | range | exact)
alt!(all | range | exact )
);

named!(
Expand All @@ -65,7 +96,7 @@ named!(
command<CompleteStr, String>,
do_parse!(
multispace0 >>
command: take_while!(|c: char| c.is_alphanumeric() || c == '/' || c.is_whitespace()) >> (String::from(command.0))
command: take_while!(|c: char| c.is_alphanumeric() || c == '\'' || c == '/' || c.is_whitespace()) >> (String::from(command.0))
)
);

Expand Down Expand Up @@ -101,11 +132,26 @@ mod test {
assert!(number(CompleteStr("AAA")).is_err());
}

#[test]
fn test_valid_day() {
day(CompleteStr("MON")).unwrap();
}

#[test]
fn test_invalid_alias() {
assert!(day(CompleteStr("MoN")).is_err());
}

#[test]
fn test_valid_range() {
range(CompleteStr("2-4")).unwrap();
}

#[test]
fn test_valid_range_of_number_and_day() {
range(CompleteStr("MON-4")).unwrap();
}

#[test]
fn test_invalid_range() {
assert!(range(CompleteStr("2-A")).is_err());
Expand Down Expand Up @@ -141,6 +187,11 @@ mod test {
cron_expression_list(CompleteStr("4,2")).unwrap();
}

#[test]
fn test_valid_alias_list() {
cron_expression_list(CompleteStr("MON,2")).unwrap();
}

#[test]
fn test_invalid_number_list() {
assert!(cron_expression_list(CompleteStr("A,4,2")).is_err());
Expand Down

0 comments on commit deaa42c

Please sign in to comment.