diff --git a/Cargo.lock b/Cargo.lock index 9526d3f..c7a74d2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -17,6 +17,7 @@ dependencies = [ name = "aoc2015" version = "0.1.0" dependencies = [ + "anyhow", "aoc", "criterion", "md-5", diff --git a/aoc2015/Cargo.toml b/aoc2015/Cargo.toml index 2144355..1c46488 100644 --- a/aoc2015/Cargo.toml +++ b/aoc2015/Cargo.toml @@ -13,6 +13,7 @@ criterion = "0.3" [dependencies] aoc = { path = "../aoc" } +anyhow = "1.0" md-5 = "0.8" [lib] diff --git a/aoc2015/src/day01.rs b/aoc2015/src/day01.rs index e722345..2ee1bbd 100644 --- a/aoc2015/src/day01.rs +++ b/aoc2015/src/day01.rs @@ -1,7 +1,6 @@ use std::fmt::Write; -use aoc::err; -use aoc::Result; +use anyhow::{anyhow, bail, Result}; const INPUT: &str = include_str!("../input/day01.txt"); @@ -33,7 +32,7 @@ fn part2(input: &str) -> Result { match c { '(' => sum += 1, ')' => sum -= 1, - _ => return Err(err!("unidentified character in input: {}", c)), + _ => bail!("unidentified character in input: {}", c), }; if sum < 0 { @@ -43,7 +42,7 @@ fn part2(input: &str) -> Result { } match res { - 0 => Err(err!("never reached the basement...")), + 0 => Err(anyhow!("never reached the basement...")), _ => Ok(res), } } diff --git a/aoc2015/src/day02.rs b/aoc2015/src/day02.rs index ba6818d..bb5a7d6 100644 --- a/aoc2015/src/day02.rs +++ b/aoc2015/src/day02.rs @@ -1,9 +1,7 @@ -use std::error::Error; use std::fmt::Write; use std::str::FromStr; -use aoc::err; -use aoc::Result; +use anyhow::{Context, Result}; const INPUT: &str = include_str!("../input/day02.txt"); @@ -64,19 +62,19 @@ struct Present { } impl FromStr for Present { - type Err = Box; + type Err = anyhow::Error; fn from_str(s: &str) -> Result { let x = s .find('x') - .ok_or_else(|| err!("couldn't find first `x` in {}", s))?; + .with_context(|| format!("couldn't find first `x` in {}", s))?; let length = s[..x].parse()?; let s = &s[(x + 1)..]; let x = s .find('x') - .ok_or_else(|| err!("couldn't find second `x` in {}", s))?; + .with_context(|| format!("couldn't find second `x` in {}", s))?; let width = s[..x].parse()?; diff --git a/aoc2015/src/day03.rs b/aoc2015/src/day03.rs index b91c8ed..3c25b69 100644 --- a/aoc2015/src/day03.rs +++ b/aoc2015/src/day03.rs @@ -1,8 +1,7 @@ use std::collections::HashSet; use std::fmt::Write; -use aoc::err; -use aoc::Result; +use anyhow::{bail, Result}; const INPUT: &str = include_str!("../input/day03.txt"); @@ -29,7 +28,7 @@ fn part1(input: &str) -> Result { '<' => x -= 1, '^' => y -= 1, 'v' => y += 1, - _ => return Err(err!("unidentified move: `{}`", c)), + _ => bail!("unidentified move: `{}`", c), } houses.insert((x, y)); @@ -56,7 +55,7 @@ fn part2(input: &str) -> Result { '<' => santa_x -= 1, '^' => santa_y -= 1, 'v' => santa_y += 1, - _ => return Err(err!("unidentified move: `{}`", c)), + _ => bail!("unidentified move: `{}`", c), } houses.insert((santa_x, santa_y)); @@ -66,7 +65,7 @@ fn part2(input: &str) -> Result { '<' => robot_x -= 1, '^' => robot_y -= 1, 'v' => robot_y += 1, - _ => return Err(err!("unidentified move: `{}`", c)), + _ => bail!("unidentified move: `{}`", c), } houses.insert((robot_x, robot_y)); diff --git a/aoc2015/src/day04.rs b/aoc2015/src/day04.rs index b59e0fe..beeb0e8 100644 --- a/aoc2015/src/day04.rs +++ b/aoc2015/src/day04.rs @@ -2,8 +2,7 @@ use std::fmt::Write; use md5::{Digest, Md5}; -use aoc::err; -use aoc::Result; +use anyhow::{anyhow, Result}; const INPUT: &str = include_str!("../input/day04.txt"); @@ -33,7 +32,7 @@ fn part1(input: &str) -> Result { } } - Err(err!("couldn't find a suitable number")) + Err(anyhow!("couldn't find a suitable number")) } fn part2(input: &str) -> Result { @@ -53,7 +52,7 @@ fn part2(input: &str) -> Result { } } - Err(err!("couldn't find a suitable number")) + Err(anyhow!("couldn't find a suitable number")) } #[cfg(test)] diff --git a/aoc2015/src/day05.rs b/aoc2015/src/day05.rs index 41c15cd..e696295 100644 --- a/aoc2015/src/day05.rs +++ b/aoc2015/src/day05.rs @@ -1,6 +1,6 @@ use std::fmt::Write; -use aoc::Result; +use anyhow::Result; const INPUT: &str = include_str!("../input/day05.txt"); diff --git a/aoc2015/src/day06.rs b/aoc2015/src/day06.rs index 245aa1f..3213d07 100644 --- a/aoc2015/src/day06.rs +++ b/aoc2015/src/day06.rs @@ -1,9 +1,7 @@ -use std::error::Error; use std::fmt::Write; use std::str::FromStr; -use aoc::err; -use aoc::Result; +use anyhow::{bail, Context, Result}; const INPUT: &str = include_str!("../input/day06.txt"); @@ -22,7 +20,7 @@ fn part1(input: &str) -> Result { .map(|line| { line.trim_end() .parse() - .map_err(|e| err!("couldn't parse instruction: {}", e)) + .context("couldn't parse instruction: {}") }) .collect::>>()?; @@ -61,7 +59,7 @@ fn part2(input: &str) -> Result { .map(|line| { line.trim_end() .parse() - .map_err(|e| err!("couldn't parse instruction: {}", e)) + .context("couldn't parse instruction: {}") }) .collect::>>()?; @@ -119,19 +117,19 @@ impl IntoIterator for Instruction { } impl FromStr for Instruction { - type Err = Box; + type Err = anyhow::Error; fn from_str(s: &str) -> Result { let mut space = s .find(' ') - .ok_or_else(|| err!("couldn't parse instruction: {}", s))?; + .with_context(|| format!("couldn't parse instruction: {}", s))?; let action = if &s[..space] == "toggle" { Action::Toggle } else if &s[..space] == "turn" { space = s[(space + 1)..] .find(' ') - .ok_or_else(|| err!("couldn't parse instruction: {}", s))? + .with_context(|| format!("couldn't parse instruction: {}", s))? + space + 1; if &s[..space] == "turn on" { @@ -139,22 +137,22 @@ impl FromStr for Instruction { } else if &s[..space] == "turn off" { Action::TurnOff } else { - return Err(err!("couldn't identify action: {}", &s[..space])); + bail!("couldn't identify action: {}", &s[..space]); } } else { - return Err(err!("couldn't identify action: {}", s)); + bail!("couldn't identify action: {}", s); }; let s = &s[(space + 1)..]; let comma = s .find(',') - .ok_or_else(|| err!("couldn't parse instruction: {}", s))?; + .with_context(|| format!("couldn't parse instruction: {}", s))?; let x = s[..comma].parse()?; let s = &s[(comma + 1)..]; let space = s .find(' ') - .ok_or_else(|| err!("couldn't parse instruction: {}", s))?; + .with_context(|| format!("couldn't parse instruction: {}", s))?; let y = s[..space].parse()?; let s = &s[(space + 1)..]; @@ -162,12 +160,12 @@ impl FromStr for Instruction { let through = s .find("through ") - .ok_or_else(|| err!("couldn't parse instruction: {}", s))?; + .with_context(|| format!("couldn't parse instruction: {}", s))?; let s = &s[(through + 8)..]; let comma = s .find(',') - .ok_or_else(|| err!("couldn't parse instruction: {}", s))?; + .with_context(|| format!("couldn't parse instruction: {}", s))?; let x = s[..comma].parse()?; let s = &s[(comma + 1)..]; diff --git a/aoc2015/src/main.rs b/aoc2015/src/main.rs index d712f92..6dfde82 100644 --- a/aoc2015/src/main.rs +++ b/aoc2015/src/main.rs @@ -1,5 +1,6 @@ +use anyhow::Result; + use aoc::DayFunc; -use aoc::Result; use aoc2015::day01; use aoc2015::day02;