advent-of-code/aoc/src/lib.rs

34 lines
929 B
Rust
Raw Normal View History

2019-12-02 16:05:24 +01:00
use std::env;
2019-12-01 22:22:12 +01:00
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),+)))
}
2019-12-02 16:05:24 +01:00
pub type DayFunc = fn() -> Result<String>;
2019-12-02 16:05:24 +01:00
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");
2020-12-01 16:30:43 +01:00
let res = days[day - 1]().map_err(|e| err!("error running day specified: {}", e))?;
println!("{}", res);
2019-12-02 16:05:24 +01:00
}
None => {
for (i, day) in days.iter().enumerate() {
let i = i + 1;
println!("day{}: ", i);
2020-12-01 16:30:43 +01:00
let res = day().map_err(|e| err!("error running day {}: {}", i, e))?;
println!("{}", res);
2019-12-02 16:05:24 +01:00
}
}
}
Ok(())
}