Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f8a1368

Browse files
authored
feat: add cargo today command (#43)
1 parent b696aa4 commit f8a1368

File tree

6 files changed

+238
-6
lines changed

6 files changed

+238
-6
lines changed

‎.cargo/config.toml

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
[alias]
2+
today = "run --quiet --release --features today -- today"
23
scaffold = "run --quiet --release -- scaffold"
34
download = "run --quiet --release -- download"
45
read = "run --quiet --release -- read"

‎Cargo.lock

+154-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+6-4
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ publish = false
1010
[lib]
1111
doctest = false
1212

13+
[profile.dhat]
14+
inherits = "release"
15+
debug = 1
16+
1317
[features]
18+
today = ["chrono"]
1419
test_lib = []
1520
dhat-heap = ["dhat"]
1621

1722
[dependencies]
23+
chrono = { version = "0.4.31", optional = true }
1824
pico-args = "0.5.0"
1925
dhat = { version = "0.3.2", optional = true }
20-
21-
[profile.dhat]
22-
inherits = "release"
23-
debug = 1

‎README.md

+35
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,41 @@ cargo read <day>
159159
# ...the input...
160160
```
161161

162+
### Scaffold, download and read in one go
163+
164+
> [!IMPORTANT]
165+
> This command requires [installing the aoc-cli crate](#configure-aoc-cli-integration).
166+
167+
During december, the `today` shorthand command can be used to:
168+
169+
- scaffold a solution for the current day
170+
- download its input
171+
- and read the puzzle
172+
173+
in one go.
174+
175+
```sh
176+
# example: `cargo today` on December 1st
177+
cargo today
178+
179+
# output:
180+
# Created module file "src/bin/01.rs"
181+
# Created empty input file "data/inputs/01.txt"
182+
# Created empty example file "data/examples/01.txt"
183+
# ---
184+
# 🎄 Type `cargo solve 01` to run your solution.
185+
# [INFO aoc] 🎄 aoc-cli - Advent of Code command-line tool
186+
# [INFO aoc_client] 🎅 Saved puzzle to 'data/puzzles/01.md'
187+
# [INFO aoc_client] 🎅 Saved input to 'data/inputs/01.txt'
188+
# ---
189+
# 🎄 Successfully wrote input to "data/inputs/01.txt".
190+
# 🎄 Successfully wrote puzzle to "data/puzzles/01.md".
191+
#
192+
# Loaded session cookie from "/Users/<snip>/.adventofcode.session".
193+
# Fetching puzzle for day 1, 2022...
194+
# ...the input...
195+
```
196+
162197
## Optional template features
163198

164199
### Configure aoc-cli integration

‎src/main.rs

+26
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
use advent_of_code::template::commands::{all, download, read, scaffold, solve};
22
use args::{parse, AppArguments};
33

4+
#[cfg(feature = "today")]
5+
use advent_of_code::template::Day;
6+
#[cfg(feature = "today")]
7+
use std::process;
8+
49
mod args {
510
use advent_of_code::template::Day;
611
use std::process;
@@ -27,6 +32,8 @@ mod args {
2732
release: bool,
2833
time: bool,
2934
},
35+
#[cfg(feature = "today")]
36+
Today,
3037
}
3138

3239
pub fn parse() -> Result<AppArguments, Box<dyn std::error::Error>> {
@@ -54,6 +61,8 @@ mod args {
5461
time: args.contains("--time"),
5562
dhat: args.contains("--dhat"),
5663
},
64+
#[cfg(feature = "today")]
65+
Some("today") => AppArguments::Today,
5766
Some(x) => {
5867
eprintln!("Unknown command: {x}");
5968
process::exit(1);
@@ -96,6 +105,23 @@ fn main() {
96105
dhat,
97106
submit,
98107
} => solve::handle(day, release, time, dhat, submit),
108+
#[cfg(feature = "today")]
109+
AppArguments::Today => {
110+
match Day::today() {
111+
Some(day) => {
112+
scaffold::handle(day);
113+
download::handle(day);
114+
read::handle(day)
115+
}
116+
None => {
117+
eprintln!(
118+
"`today` command can only be run between the 1st and \
119+
the 25th of december. Please use `scaffold` with a specific day."
120+
);
121+
process::exit(1)
122+
}
123+
};
124+
}
99125
},
100126
};
101127
}

‎src/template/day.rs

+16
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ use std::error::Error;
22
use std::fmt::Display;
33
use std::str::FromStr;
44

5+
#[cfg(feature = "today")]
6+
use chrono::{Datelike, Local};
7+
58
/// A valid day number of advent (i.e. an integer in range 1 to 25).
69
///
710
/// # Display
@@ -37,6 +40,19 @@ impl Day {
3740
}
3841
}
3942

43+
#[cfg(feature = "today")]
44+
impl Day {
45+
/// Returns the current day if it's between the 1st and the 25th of december, `None` otherwise.
46+
pub fn today() -> Option<Self> {
47+
let today = Local::now();
48+
if today.month() == 12 && today.day() <= 25 {
49+
Self::new(u8::try_from(today.day()).ok()?)
50+
} else {
51+
None
52+
}
53+
}
54+
}
55+
4056
impl Display for Day {
4157
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
4258
write!(f, "{:02}", self.0)

0 commit comments

Comments
 (0)