2019-12-02 16:05:24 +01:00
|
|
|
use std::env;
|
|
|
|
|
2020-12-14 18:08:16 +01:00
|
|
|
use anyhow::{Context, Result};
|
2019-12-02 16:05:24 +01:00
|
|
|
|
2019-12-06 19:06:49 +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) => {
|
2020-12-14 18:08:16 +01:00
|
|
|
let day: usize = arg.parse().context("couldn't parse day number")?;
|
|
|
|
let res = days[day - 1]().context("error running day specified")?;
|
2019-12-06 19:06:49 +01:00
|
|
|
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-14 18:08:16 +01:00
|
|
|
let res = day().with_context(|| format!("error running day {}", i))?;
|
2019-12-06 19:06:49 +01:00
|
|
|
println!("{}", res);
|
2019-12-02 16:05:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|