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 ### 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 ```sh
# example: `cargo today` on December 1st # 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 args::{parse, AppArguments};
use chrono::Local;
mod args { mod args {
use advent_of_code::template::Day; use advent_of_code::template::Day;
@ -100,13 +104,19 @@ fn main() {
submit, submit,
} => solve::handle(day, release, time, dhat, submit), } => solve::handle(day, release, time, dhat, submit),
AppArguments::Today => { AppArguments::Today => {
match Local::now().try_into() { match Day::today() {
Ok(day) => { Some(day) => {
scaffold::handle(day); scaffold::handle(day);
download::handle(day); download::handle(day);
read::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::error::Error;
use std::fmt::Display; use std::fmt::Display;
use std::str::FromStr; use std::str::FromStr;
@ -38,15 +38,15 @@ impl Day {
} }
} }
impl TryFrom<DateTime<Local>> for Day { impl Day {
type Error = &'static str; /// Returns the current day if it's between the 1st and the 25th of december, `None` otherwise.
pub fn today() -> Option<Self> {
fn try_from(date: DateTime<Local>) -> Result<Self, Self::Error> { let today = Local::now();
let day = date.day(); if today.month() == 12 && today.day() <= 25 {
if date.month() != 12 || day == 0 || day > 25 { Self::new(u8::try_from(today.day()).ok()?)
return Err("Cannot run this command on a day outside of the Advent of Code days."); } else {
None
} }
Ok(Self(day as u8))
} }
} }