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
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 238 additions and 6 deletions

View file

@ -1,4 +1,5 @@
[alias] [alias]
today = "run --quiet --release --features today -- today"
scaffold = "run --quiet --release -- scaffold" scaffold = "run --quiet --release -- scaffold"
download = "run --quiet --release -- download" download = "run --quiet --release -- download"
read = "run --quiet --release -- read" read = "run --quiet --release -- read"

156
Cargo.lock generated
View file

@ -21,10 +21,26 @@ checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe"
name = "advent_of_code" name = "advent_of_code"
version = "0.9.5" version = "0.9.5"
dependencies = [ dependencies = [
"chrono",
"dhat", "dhat",
"pico-args", "pico-args",
] ]
[[package]]
name = "android-tzdata"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e999941b234f3131b00bc13c22d06e8c5ff726d1b6318ac7eb276997bbb4fef0"
[[package]]
name = "android_system_properties"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "819e7219dbd41043ac279b19830f2efc897156490d7fd6ea916720117ee66311"
dependencies = [
"libc",
]
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.1.0" version = "1.1.0"
@ -52,6 +68,12 @@ version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bumpalo"
version = "3.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f30e7476521f6f8af1a1c4c0b8cc94f0bee37d91763d0ca2665f299b6cd8aec"
[[package]] [[package]]
name = "cc" name = "cc"
version = "1.0.83" version = "1.0.83"
@ -67,6 +89,26 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "chrono"
version = "0.4.31"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f2c685bad3eb3d45a01354cedb7d5faa66194d1d58ba6e267a8de788f79db38"
dependencies = [
"android-tzdata",
"iana-time-zone",
"js-sys",
"num-traits",
"wasm-bindgen",
"windows-targets",
]
[[package]]
name = "core-foundation-sys"
version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]] [[package]]
name = "dhat" name = "dhat"
version = "0.3.2" version = "0.3.2"
@ -89,12 +131,44 @@ version = "0.28.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253" checksum = "4271d37baee1b8c7e4b708028c57d816cf9d2434acb33a549475f78c181f6253"
[[package]]
name = "iana-time-zone"
version = "0.1.58"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8326b86b6cff230b97d0d312a6c40a60726df3332e721f72a1b035f451663b20"
dependencies = [
"android_system_properties",
"core-foundation-sys",
"iana-time-zone-haiku",
"js-sys",
"wasm-bindgen",
"windows-core",
]
[[package]]
name = "iana-time-zone-haiku"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f31827a206f56af32e590ba56d5d2d085f558508192593743f16b2306495269f"
dependencies = [
"cc",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.9" version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38" checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
[[package]]
name = "js-sys"
version = "0.3.66"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cee9c64da59eae3b50095c18d3e74f8b73c0b86d2792824ff01bbce68ba229ca"
dependencies = [
"wasm-bindgen",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.4.0"
@ -117,6 +191,12 @@ dependencies = [
"scopeguard", "scopeguard",
] ]
[[package]]
name = "log"
version = "0.4.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.6.4" version = "2.6.4"
@ -142,6 +222,15 @@ dependencies = [
"sys-info", "sys-info",
] ]
[[package]]
name = "num-traits"
version = "0.2.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "object" name = "object"
version = "0.32.1" version = "0.32.1"
@ -153,9 +242,9 @@ dependencies = [
[[package]] [[package]]
name = "once_cell" name = "once_cell"
version = "1.18.0" version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd8b5dd2ae5ed71462c540258bedcb51965123ad7e7ccf4b9a8cafaa4a63576d" checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92"
[[package]] [[package]]
name = "parking_lot" name = "parking_lot"
@ -307,6 +396,69 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "wasm-bindgen"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ed0d4f68a3015cc185aff4db9506a015f4b96f95303897bfa23f846db54064e"
dependencies = [
"cfg-if",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b56f625e64f3a1084ded111c4d5f477df9f8c92df113852fa5a374dbda78826"
dependencies = [
"bumpalo",
"log",
"once_cell",
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0162dbf37223cd2afce98f3d0785506dcb8d266223983e4b5b525859e6e182b2"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
]
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0eb82fcb7930ae6219a7ecfd55b217f5f0893484b7a13022ebb2b2bf20b5283"
dependencies = [
"proc-macro2",
"quote",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.89"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7ab9b36309365056cd639da3134bf87fa8f3d86008abf99e612384a6eecd459f"
[[package]]
name = "windows-core"
version = "0.51.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1f8cf84f35d2db49a46868f947758c7a1138116f7fac3bc844f43ade1292e64"
dependencies = [
"windows-targets",
]
[[package]] [[package]]
name = "windows-targets" name = "windows-targets"
version = "0.48.5" version = "0.48.5"

View file

@ -10,14 +10,16 @@ publish = false
[lib] [lib]
doctest = false doctest = false
[profile.dhat]
inherits = "release"
debug = 1
[features] [features]
today = ["chrono"]
test_lib = [] test_lib = []
dhat-heap = ["dhat"] dhat-heap = ["dhat"]
[dependencies] [dependencies]
chrono = { version = "0.4.31", optional = true }
pico-args = "0.5.0" pico-args = "0.5.0"
dhat = { version = "0.3.2", optional = true } dhat = { version = "0.3.2", optional = true }
[profile.dhat]
inherits = "release"
debug = 1

View file

@ -159,6 +159,41 @@ cargo read <day>
# ...the input... # ...the input...
``` ```
### Scaffold, download and read in one go
> [!IMPORTANT]
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
During december, the `today` shorthand command can be used to:
- scaffold a solution for the current day
- download its input
- and read the puzzle
in one go.
```sh
# example: `cargo today` on December 1st
cargo today
# output:
# Created module file "src/bin/01.rs"
# Created empty input file "data/inputs/01.txt"
# Created empty example file "data/examples/01.txt"
# ---
# 🎄 Type `cargo solve 01` to run your solution.
# [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool
# [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md'
# [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt'
# ---
# 🎄 Successfully wrote input to "data/inputs/01.txt".
# 🎄 Successfully wrote puzzle to "data/puzzles/01.md".
#
# Loaded session cookie from "/Users/<snip>/.adventofcode.session".
# Fetching puzzle for day 1, 2022...
# ...the input...
```
## Optional template features ## Optional template features
### Configure aoc-cli integration ### Configure aoc-cli integration

View file

@ -1,6 +1,11 @@
use advent_of_code::template::commands::{all, download, read, scaffold, solve}; use advent_of_code::template::commands::{all, download, read, scaffold, solve};
use args::{parse, AppArguments}; use args::{parse, AppArguments};
#[cfg(feature = "today")]
use advent_of_code::template::Day;
#[cfg(feature = "today")]
use std::process;
mod args { mod args {
use advent_of_code::template::Day; use advent_of_code::template::Day;
use std::process; use std::process;
@ -27,6 +32,8 @@ mod args {
release: bool, release: bool,
time: bool, time: bool,
}, },
#[cfg(feature = "today")]
Today,
} }
pub fn parse() -> Result<AppArguments, Box<dyn std::error::Error>> { pub fn parse() -> Result<AppArguments, Box<dyn std::error::Error>> {
@ -54,6 +61,8 @@ mod args {
time: args.contains("--time"), time: args.contains("--time"),
dhat: args.contains("--dhat"), dhat: args.contains("--dhat"),
}, },
#[cfg(feature = "today")]
Some("today") => AppArguments::Today,
Some(x) => { Some(x) => {
eprintln!("Unknown command: {x}"); eprintln!("Unknown command: {x}");
process::exit(1); process::exit(1);
@ -96,6 +105,23 @@ fn main() {
dhat, dhat,
submit, submit,
} => solve::handle(day, release, time, 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::fmt::Display;
use std::str::FromStr; 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). /// A valid day number of advent (i.e. an integer in range 1 to 25).
/// ///
/// # Display /// # 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 { impl Display for Day {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:02}", self.0) write!(f, "{:02}", self.0)