From 921dab6a765607b26c30be92aaaa6dd993854927 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Sp=C3=B6ttel?= <1682504+fspoettel@users.noreply.github.com> Date: Sat, 9 Dec 2023 14:41:21 +0100 Subject: [PATCH] refactor: use feature flag for `chrono` --- .cargo/config.toml | 2 +- Cargo.toml | 11 ++++++----- README.md | 11 ++++++++++- src/main.rs | 17 ++++++++++------- src/template/day.rs | 5 ++++- 5 files changed, 31 insertions(+), 15 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index b1b286e..96b289c 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -1,5 +1,5 @@ [alias] -today = "run --quiet --release -- today" +today = "run --quiet --release --features today -- today" scaffold = "run --quiet --release -- scaffold" download = "run --quiet --release -- download" read = "run --quiet --release -- read" diff --git a/Cargo.toml b/Cargo.toml index eee67f2..371b2bc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,15 +10,16 @@ publish = false [lib] doctest = false +[profile.dhat] +inherits = "release" +debug = 1 + [features] +today = ["chrono"] test_lib = [] dhat-heap = ["dhat"] [dependencies] -chrono = "0.4.31" +chrono = { version = "0.4.31", optional = true } pico-args = "0.5.0" dhat = { version = "0.3.2", optional = true } - -[profile.dhat] -inherits = "release" -debug = 1 diff --git a/README.md b/README.md index 929d594..e138e7e 100644 --- a/README.md +++ b/README.md @@ -161,7 +161,16 @@ cargo read ### Scaffold, download and read in one go -This command runs `cargo scaffold --download`, and `cargo read` for the current day. +> [!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 diff --git a/src/main.rs b/src/main.rs index fb93716..95dcb2e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,11 +1,11 @@ -use std::process; - -use advent_of_code::template::{ - commands::{all, download, read, scaffold, solve}, - Day, -}; +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; @@ -21,7 +21,6 @@ mod args { day: Day, download: bool, }, - Today, Solve { day: Day, release: bool, @@ -33,6 +32,8 @@ mod args { release: bool, time: bool, }, + #[cfg(feature = "today")] + Today, } pub fn parse() -> Result> { @@ -60,6 +61,7 @@ mod args { time: args.contains("--time"), dhat: args.contains("--dhat"), }, + #[cfg(feature = "today")] Some("today") => AppArguments::Today, Some(x) => { eprintln!("Unknown command: {x}"); @@ -103,6 +105,7 @@ fn main() { dhat, submit, } => solve::handle(day, release, time, dhat, submit), + #[cfg(feature = "today")] AppArguments::Today => { match Day::today() { Some(day) => { diff --git a/src/template/day.rs b/src/template/day.rs index 60f7570..ca264b8 100644 --- a/src/template/day.rs +++ b/src/template/day.rs @@ -1,8 +1,10 @@ -use chrono::{Datelike, Local}; 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 @@ -38,6 +40,7 @@ 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 {