feat: add cargo today command (#43)

This commit is contained in:
Tom Van Eyck
2023-12-09 14:43:37 +01:00
committed by GitHub
parent b696aa45da
commit f8a1368765
6 changed files with 238 additions and 6 deletions

View File

@@ -1,6 +1,11 @@
use advent_of_code::template::commands::{all, download, read, scaffold, solve};
use args::{parse, AppArguments};
#[cfg(feature = "today")]
use advent_of_code::template::Day;
#[cfg(feature = "today")]
use std::process;
mod args {
use advent_of_code::template::Day;
use std::process;
@@ -27,6 +32,8 @@ mod args {
release: bool,
time: bool,
},
#[cfg(feature = "today")]
Today,
}
pub fn parse() -> Result<AppArguments, Box<dyn std::error::Error>> {
@@ -54,6 +61,8 @@ mod args {
time: args.contains("--time"),
dhat: args.contains("--dhat"),
},
#[cfg(feature = "today")]
Some("today") => AppArguments::Today,
Some(x) => {
eprintln!("Unknown command: {x}");
process::exit(1);
@@ -96,6 +105,23 @@ fn main() {
dhat,
submit,
} => solve::handle(day, release, time, dhat, submit),
#[cfg(feature = "today")]
AppArguments::Today => {
match Day::today() {
Some(day) => {
scaffold::handle(day);
download::handle(day);
read::handle(day)
}
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

@@ -2,6 +2,9 @@ use std::error::Error;
use std::fmt::Display;
use std::str::FromStr;
#[cfg(feature = "today")]
use chrono::{Datelike, Local};
/// A valid day number of advent (i.e. an integer in range 1 to 25).
///
/// # Display
@@ -37,6 +40,19 @@ impl Day {
}
}
#[cfg(feature = "today")]
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
}
}
}
impl Display for Day {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:02}", self.0)