Some refactoring.

This commit is contained in:
Moridius 2020-09-10 08:01:43 +02:00
parent 17086e7da6
commit ecf967e3f5
1 changed files with 18 additions and 11 deletions

View File

@ -13,18 +13,18 @@ fn command_to_date(command: &str) -> Result<Date<Local>, ()> {
}
fn calc_date(command: &str, today: Date<Local>) -> Result<Date<Local>, ()> {
let wday = today.weekday().num_days_from_monday() as i64;
if command == "today" {
return Ok(today);
let offset = if command == "today" {
Ok(0)
} else if command == "tomorrow" {
return Ok(today + Duration::days(1));
Ok(1)
} else if command == "yesterday" {
return Ok(today - Duration::days(1));
Ok(-1)
} else if command == "daybeforeyesterday" {
return Ok(today - Duration::days(2));
Ok(-2)
} else if command == "dayaftertomorrow" {
return Ok(today + Duration::days(2));
Ok(2)
} else if command.starts_with("next") || command.starts_with("last") {
let wday = today.weekday().num_days_from_monday() as i64;
let target = match &command[4..] {
"monday" => Ok(0),
"tuesday" => Ok(1),
@ -38,13 +38,20 @@ fn calc_date(command: &str, today: Date<Local>) -> Result<Date<Local>, ()> {
if let Ok(target) = target {
if command.starts_with("next") {
let offset = (target - wday + 7 - 1) % 7 + 1;
Ok((target - wday + 7 - 1) % 7 + 1)
} else {
Ok(-((wday - target + 7 - 1) % 7 + 1))
}
} else {
Err(())
}
} else {
return Err(());
};
if let Ok(offset) = offset {
return Ok(today + Duration::days(offset));
}
let offset = (wday - target + 7 - 1) % 7 + 1;
return Ok(today - Duration::days(offset));
}
};
Err(())
}