Skip to content

Commit 083cc88

Browse files
committed
refactor: error handling
1 parent 22f1a77 commit 083cc88

File tree

2 files changed

+23
-29
lines changed

2 files changed

+23
-29
lines changed

src/days/mod.rs

+16-19
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use std::{fmt::Display, fs::read_to_string};
1+
use std::{fmt::Display, fs, path::Path, time::Instant};
22

3-
use anyhow::{anyhow, Result};
3+
use anyhow::{anyhow, Context, Result};
44
use winnow::{PResult, Parser as _};
55

6-
use crate::Instant;
7-
86
pub mod day01;
97
pub mod day02;
108
pub mod day03;
@@ -44,26 +42,25 @@ pub trait Day {
4442

4543
fn part_2(input: &Self::Input) -> Self::Output2;
4644

47-
fn parse_file(fp: &str) -> Result<Self::Input> {
48-
let input_string = read_to_string(fp)?;
45+
fn parse_file(path: impl AsRef<Path>) -> Result<Self::Input> {
46+
let input_string = fs::read_to_string(path).context("reading the input file")?;
4947
let input = Self::parser
5048
.parse(&input_string)
51-
.map_err(|e| anyhow!(e.to_string()))?;
49+
.map_err(|e| anyhow!(e.to_string()))
50+
.context("running the parser")?;
5251
Ok(input)
5352
}
5453

5554
#[allow(clippy::cast_precision_loss)]
56-
fn run_day(fp: &str) {
57-
match Self::parse_file(fp) {
58-
Err(e) => println!("{e:?}"),
59-
Ok(input) => {
60-
let before1 = Instant::now();
61-
println!("Part 1: {}", Self::part_1(&input));
62-
println!("Part 1 took {:?}", before1.elapsed());
63-
let before2 = Instant::now();
64-
println!("Part 2: {}", Self::part_2(&input));
65-
println!("Part 2 took {:?}", before2.elapsed());
66-
}
67-
}
55+
fn run_day(path: impl AsRef<Path>) -> Result<()> {
56+
let input = Self::parse_file(path)?;
57+
58+
let before1 = Instant::now();
59+
println!("Part 1: {}", Self::part_1(&input));
60+
println!("Part 1 took {:?}", before1.elapsed());
61+
let before2 = Instant::now();
62+
println!("Part 2: {}", Self::part_2(&input));
63+
println!("Part 2 took {:?}", before2.elapsed());
64+
Ok(())
6865
}
6966
}

src/main.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{fs, time::Instant};
1+
use std::fs;
22

33
use anyhow::{bail, Context as _, Result};
44
use chrono::{Datelike, Local};
@@ -47,18 +47,15 @@ fn main() -> Result<()> {
4747
match cli.command {
4848
Commands::Run { day, all } => {
4949
if all {
50-
run_all_days();
51-
return Ok(());
50+
return run_all_days();
5251
}
5352
if let Some(day) = day {
54-
run_day(parse_day(&day)?);
55-
return Ok(());
53+
return run_day(parse_day(&day)?);
5654
}
5755
println!("No day parameter specified, attempting to run today's code");
5856
let now_day = get_today()?;
5957
println!("Running day {now_day}");
60-
run_day(now_day);
61-
Ok(())
58+
run_day(now_day)
6259
}
6360
Commands::GetInput { day, all } => {
6461
if all {
@@ -95,11 +92,11 @@ fn parse_day(day: &str) -> Result<u32> {
9592
Ok(out)
9693
}
9794

98-
fn run_all_days() {
99-
(1..=25).for_each(run_day);
95+
fn run_all_days() -> Result<()> {
96+
(1..=25).try_for_each(run_day)
10097
}
10198

102-
fn run_day(day: u32) {
99+
fn run_day(day: u32) -> Result<()> {
103100
println!("======== DAY {day} ========");
104101
// I'd like to do this with a macro, but I'm not sure how to do it.
105102
let input_fp = &format!("inputs/day{day:02}.txt");

0 commit comments

Comments
 (0)