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

31 lines
827 B
Rust
Raw Normal View History

#![warn(clippy::explicit_iter_loop, clippy::redundant_closure_for_method_calls)]
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(())
}