2015: switch to anyhow

This commit is contained in:
Antoine Martin 2020-12-14 20:04:47 +01:00
parent 9a2e37468f
commit 885081d3fe
9 changed files with 31 additions and 35 deletions

1
Cargo.lock generated
View file

@ -17,6 +17,7 @@ dependencies = [
name = "aoc2015" name = "aoc2015"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"anyhow",
"aoc", "aoc",
"criterion", "criterion",
"md-5", "md-5",

View file

@ -13,6 +13,7 @@ criterion = "0.3"
[dependencies] [dependencies]
aoc = { path = "../aoc" } aoc = { path = "../aoc" }
anyhow = "1.0"
md-5 = "0.8" md-5 = "0.8"
[lib] [lib]

View file

@ -1,7 +1,6 @@
use std::fmt::Write; use std::fmt::Write;
use aoc::err; use anyhow::{anyhow, bail, Result};
use aoc::Result;
const INPUT: &str = include_str!("../input/day01.txt"); const INPUT: &str = include_str!("../input/day01.txt");
@ -33,7 +32,7 @@ fn part2(input: &str) -> Result<usize> {
match c { match c {
'(' => sum += 1, '(' => sum += 1,
')' => sum -= 1, ')' => sum -= 1,
_ => return Err(err!("unidentified character in input: {}", c)), _ => bail!("unidentified character in input: {}", c),
}; };
if sum < 0 { if sum < 0 {
@ -43,7 +42,7 @@ fn part2(input: &str) -> Result<usize> {
} }
match res { match res {
0 => Err(err!("never reached the basement...")), 0 => Err(anyhow!("never reached the basement...")),
_ => Ok(res), _ => Ok(res),
} }
} }

View file

@ -1,9 +1,7 @@
use std::error::Error;
use std::fmt::Write; use std::fmt::Write;
use std::str::FromStr; use std::str::FromStr;
use aoc::err; use anyhow::{Context, Result};
use aoc::Result;
const INPUT: &str = include_str!("../input/day02.txt"); const INPUT: &str = include_str!("../input/day02.txt");
@ -64,19 +62,19 @@ struct Present {
} }
impl FromStr for Present { impl FromStr for Present {
type Err = Box<dyn Error>; type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> { fn from_str(s: &str) -> Result<Self> {
let x = s let x = s
.find('x') .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 length = s[..x].parse()?;
let s = &s[(x + 1)..]; let s = &s[(x + 1)..];
let x = s let x = s
.find('x') .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()?; let width = s[..x].parse()?;

View file

@ -1,8 +1,7 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::fmt::Write; use std::fmt::Write;
use aoc::err; use anyhow::{bail, Result};
use aoc::Result;
const INPUT: &str = include_str!("../input/day03.txt"); const INPUT: &str = include_str!("../input/day03.txt");
@ -29,7 +28,7 @@ fn part1(input: &str) -> Result<usize> {
'<' => x -= 1, '<' => x -= 1,
'^' => y -= 1, '^' => y -= 1,
'v' => y += 1, 'v' => y += 1,
_ => return Err(err!("unidentified move: `{}`", c)), _ => bail!("unidentified move: `{}`", c),
} }
houses.insert((x, y)); houses.insert((x, y));
@ -56,7 +55,7 @@ fn part2(input: &str) -> Result<usize> {
'<' => santa_x -= 1, '<' => santa_x -= 1,
'^' => santa_y -= 1, '^' => santa_y -= 1,
'v' => santa_y += 1, 'v' => santa_y += 1,
_ => return Err(err!("unidentified move: `{}`", c)), _ => bail!("unidentified move: `{}`", c),
} }
houses.insert((santa_x, santa_y)); houses.insert((santa_x, santa_y));
@ -66,7 +65,7 @@ fn part2(input: &str) -> Result<usize> {
'<' => robot_x -= 1, '<' => robot_x -= 1,
'^' => robot_y -= 1, '^' => robot_y -= 1,
'v' => robot_y += 1, 'v' => robot_y += 1,
_ => return Err(err!("unidentified move: `{}`", c)), _ => bail!("unidentified move: `{}`", c),
} }
houses.insert((robot_x, robot_y)); houses.insert((robot_x, robot_y));

View file

@ -2,8 +2,7 @@ use std::fmt::Write;
use md5::{Digest, Md5}; use md5::{Digest, Md5};
use aoc::err; use anyhow::{anyhow, Result};
use aoc::Result;
const INPUT: &str = include_str!("../input/day04.txt"); const INPUT: &str = include_str!("../input/day04.txt");
@ -33,7 +32,7 @@ fn part1(input: &str) -> Result<u64> {
} }
} }
Err(err!("couldn't find a suitable number")) Err(anyhow!("couldn't find a suitable number"))
} }
fn part2(input: &str) -> Result<u64> { fn part2(input: &str) -> Result<u64> {
@ -53,7 +52,7 @@ fn part2(input: &str) -> Result<u64> {
} }
} }
Err(err!("couldn't find a suitable number")) Err(anyhow!("couldn't find a suitable number"))
} }
#[cfg(test)] #[cfg(test)]

View file

@ -1,6 +1,6 @@
use std::fmt::Write; use std::fmt::Write;
use aoc::Result; use anyhow::Result;
const INPUT: &str = include_str!("../input/day05.txt"); const INPUT: &str = include_str!("../input/day05.txt");

View file

@ -1,9 +1,7 @@
use std::error::Error;
use std::fmt::Write; use std::fmt::Write;
use std::str::FromStr; use std::str::FromStr;
use aoc::err; use anyhow::{bail, Context, Result};
use aoc::Result;
const INPUT: &str = include_str!("../input/day06.txt"); const INPUT: &str = include_str!("../input/day06.txt");
@ -22,7 +20,7 @@ fn part1(input: &str) -> Result<usize> {
.map(|line| { .map(|line| {
line.trim_end() line.trim_end()
.parse() .parse()
.map_err(|e| err!("couldn't parse instruction: {}", e)) .context("couldn't parse instruction: {}")
}) })
.collect::<Result<Vec<Instruction>>>()?; .collect::<Result<Vec<Instruction>>>()?;
@ -61,7 +59,7 @@ fn part2(input: &str) -> Result<u64> {
.map(|line| { .map(|line| {
line.trim_end() line.trim_end()
.parse() .parse()
.map_err(|e| err!("couldn't parse instruction: {}", e)) .context("couldn't parse instruction: {}")
}) })
.collect::<Result<Vec<Instruction>>>()?; .collect::<Result<Vec<Instruction>>>()?;
@ -119,19 +117,19 @@ impl IntoIterator for Instruction {
} }
impl FromStr for Instruction { impl FromStr for Instruction {
type Err = Box<dyn Error>; type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> { fn from_str(s: &str) -> Result<Self> {
let mut space = s let mut space = s
.find(' ') .find(' ')
.ok_or_else(|| err!("couldn't parse instruction: {}", s))?; .with_context(|| format!("couldn't parse instruction: {}", s))?;
let action = if &s[..space] == "toggle" { let action = if &s[..space] == "toggle" {
Action::Toggle Action::Toggle
} else if &s[..space] == "turn" { } else if &s[..space] == "turn" {
space = s[(space + 1)..] space = s[(space + 1)..]
.find(' ') .find(' ')
.ok_or_else(|| err!("couldn't parse instruction: {}", s))? .with_context(|| format!("couldn't parse instruction: {}", s))?
+ space + space
+ 1; + 1;
if &s[..space] == "turn on" { if &s[..space] == "turn on" {
@ -139,22 +137,22 @@ impl FromStr for Instruction {
} else if &s[..space] == "turn off" { } else if &s[..space] == "turn off" {
Action::TurnOff Action::TurnOff
} else { } else {
return Err(err!("couldn't identify action: {}", &s[..space])); bail!("couldn't identify action: {}", &s[..space]);
} }
} else { } else {
return Err(err!("couldn't identify action: {}", s)); bail!("couldn't identify action: {}", s);
}; };
let s = &s[(space + 1)..]; let s = &s[(space + 1)..];
let comma = s let comma = s
.find(',') .find(',')
.ok_or_else(|| err!("couldn't parse instruction: {}", s))?; .with_context(|| format!("couldn't parse instruction: {}", s))?;
let x = s[..comma].parse()?; let x = s[..comma].parse()?;
let s = &s[(comma + 1)..]; let s = &s[(comma + 1)..];
let space = s let space = s
.find(' ') .find(' ')
.ok_or_else(|| err!("couldn't parse instruction: {}", s))?; .with_context(|| format!("couldn't parse instruction: {}", s))?;
let y = s[..space].parse()?; let y = s[..space].parse()?;
let s = &s[(space + 1)..]; let s = &s[(space + 1)..];
@ -162,12 +160,12 @@ impl FromStr for Instruction {
let through = s let through = s
.find("through ") .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 s = &s[(through + 8)..];
let comma = s let comma = s
.find(',') .find(',')
.ok_or_else(|| err!("couldn't parse instruction: {}", s))?; .with_context(|| format!("couldn't parse instruction: {}", s))?;
let x = s[..comma].parse()?; let x = s[..comma].parse()?;
let s = &s[(comma + 1)..]; let s = &s[(comma + 1)..];

View file

@ -1,5 +1,6 @@
use anyhow::Result;
use aoc::DayFunc; use aoc::DayFunc;
use aoc::Result;
use aoc2015::day01; use aoc2015::day01;
use aoc2015::day02; use aoc2015::day02;