aoc: refactor main() function
This commit is contained in:
parent
c4d9f6c2db
commit
5bee7a0ce7
|
@ -1,6 +1,32 @@
|
||||||
|
use std::env;
|
||||||
|
|
||||||
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||||
|
|
||||||
#[macro_export]
|
#[macro_export]
|
||||||
macro_rules! err {
|
macro_rules! err {
|
||||||
($($string:expr),+) => (Box::<dyn std::error::Error>::from(format!($($string),+)))
|
($($string:expr),+) => (Box::<dyn std::error::Error>::from(format!($($string),+)))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type DayFunc = fn() -> Result<()>;
|
||||||
|
|
||||||
|
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");
|
||||||
|
days[day - 1]().unwrap_or_else(|e| eprintln!("error running day specified: {}", e));
|
||||||
|
}
|
||||||
|
None => {
|
||||||
|
for (i, day) in days.iter().enumerate() {
|
||||||
|
let i = i + 1;
|
||||||
|
println!("day{}: ", i);
|
||||||
|
day().unwrap_or_else(|e| eprintln!("error running day {}: {}", i, e));
|
||||||
|
println!();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use std::env;
|
|
||||||
|
|
||||||
use aoc::Result;
|
use aoc::Result;
|
||||||
|
|
||||||
use aoc2015::day01;
|
use aoc2015::day01;
|
||||||
|
@ -7,23 +5,5 @@ use aoc2015::day01;
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let days: &[fn() -> Result<()>] = &[day01::run];
|
let days: &[fn() -> Result<()>] = &[day01::run];
|
||||||
|
|
||||||
let mut args = env::args();
|
aoc::run(days)
|
||||||
args.next();
|
|
||||||
|
|
||||||
match args.next() {
|
|
||||||
Some(arg) => {
|
|
||||||
let day: usize = arg.parse().expect("Please provide a day number");
|
|
||||||
days[day - 1]().unwrap_or_else(|e| eprintln!("error running day specified: {}", e));
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
for (i, day) in days.iter().enumerate() {
|
|
||||||
let i = i + 1;
|
|
||||||
println!("day{}: ", i);
|
|
||||||
day().unwrap_or_else(|e| eprintln!("error running day {}: {}", i, e));
|
|
||||||
println!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
use std::env;
|
|
||||||
|
|
||||||
use aoc::Result;
|
use aoc::Result;
|
||||||
|
|
||||||
use aoc2018::day01;
|
use aoc2018::day01;
|
||||||
|
@ -11,23 +9,5 @@ use aoc2018::day05;
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run, day04::run, day05::run];
|
let days: &[fn() -> Result<()>] = &[day01::run, day02::run, day03::run, day04::run, day05::run];
|
||||||
|
|
||||||
let mut args = env::args();
|
aoc::run(days)
|
||||||
args.next();
|
|
||||||
|
|
||||||
match args.next() {
|
|
||||||
Some(arg) => {
|
|
||||||
let day: usize = arg.parse().expect("Please provide a day number");
|
|
||||||
days[day - 1]().unwrap_or_else(|e| eprintln!("error running day specified: {}", e));
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
for (i, day) in days.iter().enumerate() {
|
|
||||||
let i = i + 1;
|
|
||||||
println!("day{}: ", i);
|
|
||||||
day().unwrap_or_else(|e| eprintln!("error running day {}: {}", i, e));
|
|
||||||
println!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,30 +1,10 @@
|
||||||
use std::env;
|
use aoc::Result;
|
||||||
|
|
||||||
use aoc2019::day01;
|
use aoc2019::day01;
|
||||||
use aoc2019::day02;
|
use aoc2019::day02;
|
||||||
|
|
||||||
use aoc2019::Result;
|
|
||||||
|
|
||||||
fn main() -> Result<()> {
|
fn main() -> Result<()> {
|
||||||
let days: &[fn() -> Result<()>] = &[day01::run, day02::run];
|
let days: &[fn() -> Result<()>] = &[day01::run, day02::run];
|
||||||
|
|
||||||
let mut args = env::args();
|
aoc::run(days)
|
||||||
args.next();
|
|
||||||
|
|
||||||
match args.next() {
|
|
||||||
Some(arg) => {
|
|
||||||
let day: usize = arg.parse().expect("Please provide a day number");
|
|
||||||
days[day - 1]().unwrap_or_else(|e| eprintln!("error running day specified: {}", e));
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
for (i, day) in days.iter().enumerate() {
|
|
||||||
let i = i + 1;
|
|
||||||
println!("day{}: ", i);
|
|
||||||
day().unwrap_or_else(|e| eprintln!("error running day {}: {}", i, e));
|
|
||||||
println!();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(())
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue