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"
version = "0.1.0"
dependencies = [
"anyhow",
"aoc",
"criterion",
"md-5",

View file

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

View file

@ -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<usize> {
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<usize> {
}
match res {
0 => Err(err!("never reached the basement...")),
0 => Err(anyhow!("never reached the basement...")),
_ => Ok(res),
}
}

View file

@ -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<dyn Error>;
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
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()?;

View file

@ -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<usize> {
'<' => 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<usize> {
'<' => 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<usize> {
'<' => 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));

View file

@ -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<u64> {
}
}
Err(err!("couldn't find a suitable number"))
Err(anyhow!("couldn't find a suitable number"))
}
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)]

View file

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

View file

@ -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<usize> {
.map(|line| {
line.trim_end()
.parse()
.map_err(|e| err!("couldn't parse instruction: {}", e))
.context("couldn't parse instruction: {}")
})
.collect::<Result<Vec<Instruction>>>()?;
@ -61,7 +59,7 @@ fn part2(input: &str) -> Result<u64> {
.map(|line| {
line.trim_end()
.parse()
.map_err(|e| err!("couldn't parse instruction: {}", e))
.context("couldn't parse instruction: {}")
})
.collect::<Result<Vec<Instruction>>>()?;
@ -119,19 +117,19 @@ impl IntoIterator for Instruction {
}
impl FromStr for Instruction {
type Err = Box<dyn Error>;
type Err = anyhow::Error;
fn from_str(s: &str) -> Result<Self> {
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)..];

View file

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