@@ -7,6 +7,8 @@ use std::path::PathBuf;
77// Reexport some crates for the generated main
88pub use clap;
99pub use colored;
10+
11+ #[ cfg( feature = "bench" ) ]
1012pub use criterion;
1113
1214use 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