feat: add cargo today command (#43)
This commit is contained in:
26
src/main.rs
26
src/main.rs
@@ -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)
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user