forked from uutils/parse_datetime
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This commit resolves issue uutils#23. Adds parse_weekday function that uses chrono weekday parser with a map for edge cases. Adds tests cases to make sure it works correctly. Use nom for parsing.
- Loading branch information
Showing
4 changed files
with
191 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
use chrono::Weekday; | ||
use nom::branch::alt; | ||
use nom::bytes::complete::tag; | ||
use nom::combinator::value; | ||
use nom::{self, IResult}; | ||
|
||
pub(crate) fn parse_weekday(s: &str) -> Option<Weekday> { | ||
let s = s.trim().to_lowercase(); | ||
let s = s.as_str(); | ||
|
||
let parse_result: IResult<&str, Weekday> = nom::combinator::all_consuming(alt(( | ||
value(Weekday::Mon, alt((tag("monday"), tag("mon")))), | ||
value(Weekday::Tue, alt((tag("tuesday"), tag("tues"), tag("tue")))), | ||
value( | ||
Weekday::Wed, | ||
alt((tag("wednesday"), tag("wednes"), tag("wed"))), | ||
), | ||
value( | ||
Weekday::Thu, | ||
alt((tag("thursday"), tag("thurs"), tag("thur"), tag("thu"))), | ||
), | ||
value(Weekday::Fri, alt((tag("friday"), tag("fri")))), | ||
value(Weekday::Sat, alt((tag("saturday"), tag("sat")))), | ||
value(Weekday::Sun, alt((tag("sunday"), tag("sun")))), | ||
)))(s); | ||
|
||
match parse_result { | ||
Ok((_, weekday)) => Some(weekday), | ||
Err(_) => None, | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use chrono::Weekday::*; | ||
|
||
use crate::parse_weekday::parse_weekday; | ||
|
||
#[test] | ||
fn test_valid_weekdays() { | ||
let days = [ | ||
("mon", Mon), | ||
("monday", Mon), | ||
("tue", Tue), | ||
("tues", Tue), | ||
("tuesday", Tue), | ||
("wed", Wed), | ||
("wednes", Wed), | ||
("wednesday", Wed), | ||
("thu", Thu), | ||
("thursday", Thu), | ||
("fri", Fri), | ||
("friday", Fri), | ||
("sat", Sat), | ||
("saturday", Sat), | ||
("sun", Sun), | ||
("sunday", Sun), | ||
]; | ||
|
||
for (name, weekday) in days { | ||
assert_eq!(parse_weekday(name), Some(weekday)); | ||
assert_eq!(parse_weekday(&format!(" {}", name)), Some(weekday)); | ||
assert_eq!(parse_weekday(&format!(" {} ", name)), Some(weekday)); | ||
assert_eq!(parse_weekday(&format!("{} ", name)), Some(weekday)); | ||
|
||
let (left, right) = name.split_at(1); | ||
let (test_str1, test_str2) = ( | ||
format!("{}{}", left.to_uppercase(), right.to_lowercase()), | ||
format!("{}{}", left.to_lowercase(), right.to_uppercase()), | ||
); | ||
|
||
assert_eq!(parse_weekday(&test_str1), Some(weekday)); | ||
assert_eq!(parse_weekday(&test_str2), Some(weekday)); | ||
} | ||
} | ||
|
||
#[test] | ||
fn test_invalid_weekdays() { | ||
let days = [ | ||
"mond", | ||
"tuesda", | ||
"we", | ||
"th", | ||
"fr", | ||
"sa", | ||
"su", | ||
"garbageday", | ||
"tomorrow", | ||
"yesterday", | ||
]; | ||
for day in days { | ||
assert!(parse_weekday(day).is_none()); | ||
} | ||
} | ||
} |