Rewrite function to get current day and address comments
This commit is contained in:
parent
56341aca1a
commit
764d8a2505
3 changed files with 25 additions and 15 deletions
|
@ -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
|
||||
|
|
20
src/main.rs
20
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)
|
||||
}
|
||||
};
|
||||
}
|
||||
},
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue