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
|
### 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
|
||||||
|
|
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 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)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
|
@ -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))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue