diff --git a/README.md b/README.md index 81e882c..929d594 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,7 @@ cargo read ### 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 diff --git a/src/main.rs b/src/main.rs index a489636..fb93716 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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) + } }; } }, diff --git a/src/template/day.rs b/src/template/day.rs index f929635..60f7570 100644 --- a/src/template/day.rs +++ b/src/template/day.rs @@ -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> for Day { - type Error = &'static str; - - fn try_from(date: DateTime) -> Result { - 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 { + 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)) } }