Some refactoring.
This commit is contained in:
parent
17086e7da6
commit
ecf967e3f5
31
src/main.rs
31
src/main.rs
|
@ -13,18 +13,18 @@ fn command_to_date(command: &str) -> Result<Date<Local>, ()> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn calc_date(command: &str, today: Date<Local>) -> Result<Date<Local>, ()> {
|
fn calc_date(command: &str, today: Date<Local>) -> Result<Date<Local>, ()> {
|
||||||
let wday = today.weekday().num_days_from_monday() as i64;
|
let offset = if command == "today" {
|
||||||
if command == "today" {
|
Ok(0)
|
||||||
return Ok(today);
|
|
||||||
} else if command == "tomorrow" {
|
} else if command == "tomorrow" {
|
||||||
return Ok(today + Duration::days(1));
|
Ok(1)
|
||||||
} else if command == "yesterday" {
|
} else if command == "yesterday" {
|
||||||
return Ok(today - Duration::days(1));
|
Ok(-1)
|
||||||
} else if command == "daybeforeyesterday" {
|
} else if command == "daybeforeyesterday" {
|
||||||
return Ok(today - Duration::days(2));
|
Ok(-2)
|
||||||
} else if command == "dayaftertomorrow" {
|
} else if command == "dayaftertomorrow" {
|
||||||
return Ok(today + Duration::days(2));
|
Ok(2)
|
||||||
} else if command.starts_with("next") || command.starts_with("last") {
|
} 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..] {
|
let target = match &command[4..] {
|
||||||
"monday" => Ok(0),
|
"monday" => Ok(0),
|
||||||
"tuesday" => Ok(1),
|
"tuesday" => Ok(1),
|
||||||
|
@ -38,13 +38,20 @@ fn calc_date(command: &str, today: Date<Local>) -> Result<Date<Local>, ()> {
|
||||||
|
|
||||||
if let Ok(target) = target {
|
if let Ok(target) = target {
|
||||||
if command.starts_with("next") {
|
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));
|
return Ok(today + Duration::days(offset));
|
||||||
}
|
}
|
||||||
let offset = (wday - target + 7 - 1) % 7 + 1;
|
|
||||||
return Ok(today - Duration::days(offset));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Err(())
|
Err(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue