[rs] Detect day based on input file name

This commit is contained in:
Joscha 2022-12-02 12:49:16 +01:00
parent 930b3196fe
commit 214eb5b258

View file

@ -1,27 +1,73 @@
mod d01; mod d01;
use std::fs; use std::path::{Path, PathBuf};
use std::path::PathBuf; use std::str::FromStr;
use std::{fmt, fs};
use clap::{Parser, ValueEnum}; use clap::Parser;
#[derive(Clone, Copy, PartialEq, Eq, ValueEnum)]
enum Day { enum Day {
D01, Y2022D01,
}
impl Day {
fn from_path(path: &Path) -> Option<Self> {
Self::from_str(path.file_stem()?.to_str()?).ok()
}
}
impl fmt::Display for Day {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Day::Y2022D01 => "2022_01",
}
.fmt(f)
}
}
impl FromStr for Day {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(match s {
"2022_01" => Self::Y2022D01,
_ => return Err(()),
})
}
} }
#[derive(Parser)] #[derive(Parser)]
struct Args { struct Args {
day: Day, files: Vec<PathBuf>,
file: PathBuf,
} }
fn main() -> anyhow::Result<()> { fn main() -> anyhow::Result<()> {
let args = Args::parse(); let args = Args::parse();
let input = fs::read_to_string(&args.file)?;
match args.day { if args.files.is_empty() {
Day::D01 => d01::solve(input)?, eprintln!("No days specified");
}
let mut first_day = true;
for file in args.files {
if !first_day {
println!();
}
first_day = false;
let day = match Day::from_path(&file) {
Some(day) => day,
None => {
eprintln!("### Could not determine day: {file:?}");
continue;
}
};
println!("### Solving day {day}");
let input = fs::read_to_string(file)?;
match day {
Day::Y2022D01 => d01::solve(input)?,
}
} }
Ok(()) Ok(())