Skip to content

Commit 64dcb9e

Browse files
committed
Cargo.toml: make criterion togglable
1 parent 16587b7 commit 64dcb9e

File tree

3 files changed

+77
-36
lines changed

3 files changed

+77
-36
lines changed

Cargo.toml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,15 @@ readme = "README.md"
99
keywords = ["advent", "aoc", "macro"]
1010
edition = "2018"
1111

12+
[features]
13+
# Criterion is a quite heavy dependencies which will not be used in many cases.
14+
bench = ["criterion"]
15+
1216
[dependencies]
1317
attohttpc = { version = "0.16.0", default_features = false, features = ["tls"] }
1418
clap = "3.0.0-beta.2"
1519
colored = "2.0.0"
16-
criterion = "0.3.3"
1720
dirs = "3.0.1"
21+
22+
# Optional dependencies
23+
criterion = { version = "0.3.3", optional = true }

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,18 @@ output. Also, the generator can output a structure that contains borrowed data
6666
from the original input file.
6767

6868

69+
Benchmarks
70+
----------
71+
72+
You can run Criterion benchmarks by running `cargo run --release -- --bench`,
73+
but first you need to enable the `bench` feature in your *cargo.toml*:
74+
75+
```toml
76+
[dependencies]
77+
aoc-main = { version = "*", features = ["bench"] }
78+
```
79+
80+
6981
Fetching your AOC token
7082
-----------------------
7183

src/lib.rs

Lines changed: 58 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use std::path::PathBuf;
77
// Reexport some crates for the generated main
88
pub use clap;
99
pub use colored;
10+
11+
#[cfg(feature = "bench")]
1012
pub use criterion;
1113

1214
use clap::Clap;
@@ -42,14 +44,13 @@ impl Opt {
4244
}
4345

4446
#[macro_export]
45-
macro_rules! main {
47+
macro_rules! base_main {
4648
( year $year: expr; $( $tail: tt )* ) => {
4749
use std::fs::read_to_string;
4850
use std::io::Read;
4951
use std::time::Instant;
5052

5153
use $crate::clap::Clap;
52-
use $crate::criterion::*;
5354
use $crate::{bench_day, extract_day, parse, run_day};
5455

5556
const YEAR: u16 = $year;
@@ -59,45 +60,55 @@ macro_rules! main {
5960

6061
if opt.bench {
6162
bench();
62-
}
63-
64-
if opt.days.is_empty() {
65-
opt.days = parse!(extract_day {}; $( $tail )*)
66-
.iter()
67-
.map(|s| s[3..].to_string())
68-
.collect();
6963
} else {
70-
let days = parse! { extract_day {}; $( $tail )* };
71-
72-
let ignored_days: Vec<_> = opt.days
73-
.iter()
74-
.filter(|day| !days.contains(&format!("day{}", day).as_str()))
75-
.map(String::as_str)
76-
.collect();
64+
if opt.days.is_empty() {
65+
opt.days = parse!(extract_day {}; $( $tail )*)
66+
.iter()
67+
.map(|s| s[3..].to_string())
68+
.collect();
69+
} else {
70+
let days = parse! { extract_day {}; $( $tail )* };
71+
72+
let ignored_days: Vec<_> = opt.days
73+
.iter()
74+
.filter(|day| !days.contains(&format!("day{}", day).as_str()))
75+
.map(String::as_str)
76+
.collect();
77+
78+
if !ignored_days.is_empty() {
79+
eprintln!(r"/!\ Ignoring unimplemented days: {}", ignored_days.join(", "));
80+
}
81+
82+
opt.days = opt.days
83+
.into_iter()
84+
.filter(|day| days.contains(&format!("day{}", day).as_str()))
85+
.collect();
86+
}
7787

78-
if !ignored_days.is_empty() {
79-
eprintln!(r"/!\ Ignoring unimplemented days: {}", ignored_days.join(", "));
88+
if opt.days.len() > 1 && (opt.stdin || opt.file.is_some()) {
89+
eprintln!(r"/!\ You are using a personalized output over several days which can");
90+
eprintln!(r" be missleading. If you only intend to run solutions for a");
91+
eprintln!(r" specific day, you can specify it by using the `-d DAY_NUM` flag.");
8092
}
8193

82-
opt.days = opt.days
83-
.into_iter()
84-
.filter(|day| days.contains(&format!("day{}", day).as_str()))
85-
.collect();
94+
for (i, day) in opt.days.iter().enumerate() {
95+
parse! {
96+
run_day { i, format!("day{}", day), YEAR, opt };
97+
$( $tail )*
98+
};
99+
}
86100
}
101+
}
102+
}
103+
}
87104

88-
if opt.days.len() > 1 && (opt.stdin || opt.file.is_some()) {
89-
eprintln!(r"/!\ You are using a personalized output over several days which can");
90-
eprintln!(r" be missleading. If you only intend to run solutions for a");
91-
eprintln!(r" specific day, you can specify it by using the `-d DAY_NUM` flag.");
92-
}
105+
#[cfg(feature = "bench")]
106+
#[macro_export]
107+
macro_rules! main {
108+
( year $year: expr; $( $tail: tt )* ) => {
109+
$crate::base_main! { year $year; $( $tail )* }
93110

94-
for (i, day) in opt.days.iter().enumerate() {
95-
parse! {
96-
run_day { i, format!("day{}", day), YEAR, opt };
97-
$( $tail )*
98-
};
99-
}
100-
}
111+
use $crate::criterion::Criterion;
101112

102113
fn bench() {
103114
let mut criterion = Criterion::default().configure_from_args();
@@ -109,5 +120,17 @@ macro_rules! main {
109120

110121
criterion.final_summary();
111122
}
112-
};
123+
}
124+
}
125+
126+
#[cfg(not(feature = "bench"))]
127+
#[macro_export]
128+
macro_rules! main {
129+
( year $year: expr; $( $tail: tt )* ) => {
130+
$crate::base_main! { year $year; $( $tail )* }
131+
132+
fn bench() {
133+
println!("Benchmarks not available, please enable `bench` feature for cargo-main.");
134+
}
135+
}
113136
}

0 commit comments

Comments
 (0)