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

29 lines
745 B
Rust
Raw Normal View History

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
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")?;
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))?;
println!("{}", res);
2019-12-02 16:05:24 +01:00
}
}
}
Ok(())
}