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.
- Loading branch information
Showing
4 changed files
with
125 additions
and
3 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
target/ | ||
fuzz/corpus |
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,80 @@ | ||
use chrono::{ParseWeekdayError, Weekday}; | ||
|
||
const WEEKDAY_CONVERSION_MAP: [(&str, &str); 3] = | ||
[("tues", "tue"), ("thurs", "thu"), ("thus", "thu")]; | ||
|
||
pub(crate) fn parse_weekday(s: &str) -> Result<Weekday, ParseWeekdayError> { | ||
let mut s = s.trim().to_lowercase(); | ||
for (before, after) in WEEKDAY_CONVERSION_MAP { | ||
if s == before { | ||
s = String::from(after); | ||
} | ||
} | ||
|
||
s.trim().parse::<Weekday>() | ||
} | ||
|
||
#[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), | ||
("wednesday", Wed), | ||
("thu", Thu), | ||
("thursday", Thu), | ||
("fri", Fri), | ||
("friday", Fri), | ||
("sat", Sat), | ||
("saturday", Sat), | ||
("sun", Sun), | ||
("sunday", Sun), | ||
]; | ||
|
||
for day in days.iter() { | ||
let mut test_strings = vec![]; | ||
test_strings.push(String::from(day.0)); | ||
test_strings.push(format!(" {}", day.0)); | ||
test_strings.push(format!(" {} ", day.0)); | ||
test_strings.push(format!("{} ", day.0)); | ||
test_strings.push(format!(" {}", day.0.to_uppercase())); | ||
|
||
let (left, right) = day.0.split_at(1); | ||
test_strings.push(format!("{}{}", left.to_uppercase(), right.to_lowercase())); | ||
test_strings.push(format!("{}{}", left.to_lowercase(), right.to_uppercase())); | ||
|
||
for str in test_strings.iter() { | ||
assert!(parse_weekday(str).is_ok()); | ||
assert_eq!(parse_weekday(str).unwrap(), day.1); | ||
} | ||
} | ||
} | ||
|
||
#[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_err()); | ||
} | ||
} | ||
} |