use color-eyre for error handling

This commit is contained in:
Joseph Montanaro
2021-12-01 15:38:32 -08:00
parent 7b4cb884c1
commit 6f60951b94
5 changed files with 288 additions and 12 deletions

View File

@ -1,15 +1,15 @@
use std::io::{BufReader, BufRead};
use std::fs::File;
use color_eyre::eyre;
pub fn iter_nums(filename: &str) -> impl Iterator<Item=i32> {
let file = File::open(filename).expect("File not found! Make sure that your current directory is the project root.");
pub fn iter_nums(filename: &str) -> eyre::Result< impl Iterator<Item=eyre::Result<i32>> > {
let file = File::open(filename)?;
let reader = BufReader::new(file);
reader.lines().map(|line|
line
.expect("Couldn't split on lines I guess?")
.parse::<i32>()
.expect("Could not parse integer!")
)
let it = reader.lines().map(|line|
Ok(line?.parse::<i32>()?)
);
Ok(it)
}

View File

@ -1,17 +1,21 @@
use color_eyre::eyre;
mod lib;
use lib::iter_nums;
fn main() {
day_one();
fn main() -> eyre::Result<()> {
day_one()
}
fn day_one() {
fn day_one() -> eyre::Result<()> {
let mut single_increases = 0;
let mut window_increases = 0;
let mut prev = (None, None, None);
for n in iter_nums("data/01.txt") {
for res in iter_nums("data/01.txt")? {
let n = res?;
if let Some(p) = prev.2 {
// apparently we can't combine "if let" and regular boolean conditions
if n > p {
@ -30,4 +34,5 @@ fn day_one() {
println!("One: {}", single_increases);
println!("Two: {}", window_increases);
Ok(())
}