Rewrite function to get current day and address comments

This commit is contained in:
Tom Van Eyck 2023-12-09 00:20:00 +01:00
parent 56341aca1a
commit 764d8a2505
3 changed files with 25 additions and 15 deletions

View file

@ -161,7 +161,7 @@ cargo read <day>
### Scaffold, download and read in one go
This command runs `cargo scaffold`, `cargo download` and `cargo read` for the current day.
This command runs `cargo scaffold --download`, and `cargo read` for the current day.
```sh
# example: `cargo today` on December 1st

View file

@ -1,6 +1,10 @@
use advent_of_code::template::commands::{all, download, read, scaffold, solve};
use std::process;
use advent_of_code::template::{
commands::{all, download, read, scaffold, solve},
Day,
};
use args::{parse, AppArguments};
use chrono::Local;
mod args {
use advent_of_code::template::Day;
@ -100,13 +104,19 @@ fn main() {
submit,
} => solve::handle(day, release, time, dhat, submit),
AppArguments::Today => {
match Local::now().try_into() {
Ok(day) => {
match Day::today() {
Some(day) => {
scaffold::handle(day);
download::handle(day);
read::handle(day)
}
Err(e) => println!("{e}"),
None => {
eprintln!(
"`today` command can only be run between the 1st and \
the 25th of december. Please use `scaffold` with a specific day."
);
process::exit(1)
}
};
}
},

View file

@ -1,4 +1,4 @@
use chrono::{DateTime, Datelike, Local};
use chrono::{Datelike, Local};
use std::error::Error;
use std::fmt::Display;
use std::str::FromStr;
@ -38,15 +38,15 @@ impl Day {
}
}
impl TryFrom<DateTime<Local>> for Day {
type Error = &'static str;
fn try_from(date: DateTime<Local>) -> Result<Self, Self::Error> {
let day = date.day();
if date.month() != 12 || day == 0 || day > 25 {
return Err("Cannot run this command on a day outside of the Advent of Code days.");
impl Day {
/// Returns the current day if it's between the 1st and the 25th of december, `None` otherwise.
pub fn today() -> Option<Self> {
let today = Local::now();
if today.month() == 12 && today.day() <= 25 {
Self::new(u8::try_from(today.day()).ok()?)
} else {
None
}
Ok(Self(day as u8))
}
}