advent-of-code/aoc/src/lib.rs
Antoine Martin 4bff689da0 aoc: stop printing inside run functions
This will make benching implementations easier
2019-12-06 19:06:49 +01:00

35 lines
955 B
Rust

use std::env;
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
#[macro_export]
macro_rules! err {
($($string:expr),+) => (Box::<dyn std::error::Error>::from(format!($($string),+)))
}
pub type DayFunc = fn() -> Result<String>;
pub fn run(days: &[DayFunc]) -> Result<()> {
let mut args = env::args();
args.next();
match args.next() {
Some(arg) => {
let day: usize = arg.parse().expect("Please provide a day number");
let res =
days[day - 1]().or_else(|e| Err(err!("error running day specified: {}", e)))?;
println!("{}", res);
}
None => {
for (i, day) in days.iter().enumerate() {
let i = i + 1;
println!("day{}: ", i);
let res = day().or_else(|e| Err(err!("error running day {}: {}", i, e)))?;
println!("{}", res);
}
}
}
Ok(())
}