From 60b53987378e60f55ee029eaf25569b2ad5f7ddc Mon Sep 17 00:00:00 2001 From: Antoine Martin Date: Mon, 14 Dec 2020 20:24:09 +0100 Subject: [PATCH] 2018: switch to anyhow --- Cargo.lock | 1 + aoc2018/Cargo.toml | 1 + aoc2018/src/day01.rs | 2 +- aoc2018/src/day02.rs | 2 +- aoc2018/src/day03.rs | 20 ++++++++------------ aoc2018/src/day04.rs | 38 ++++++++++++++++++-------------------- aoc2018/src/day05.rs | 2 +- aoc2018/src/main.rs | 3 ++- 8 files changed, 33 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c7a74d2..09224eb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -27,6 +27,7 @@ dependencies = [ name = "aoc2018" version = "0.1.0" dependencies = [ + "anyhow", "aoc", "criterion", ] diff --git a/aoc2018/Cargo.toml b/aoc2018/Cargo.toml index 2ea04c2..8833b86 100644 --- a/aoc2018/Cargo.toml +++ b/aoc2018/Cargo.toml @@ -13,6 +13,7 @@ criterion = "0.3" [dependencies] aoc = { path = "../aoc" } +anyhow = "1.0" [lib] path = "src/lib.rs" diff --git a/aoc2018/src/day01.rs b/aoc2018/src/day01.rs index 6ea274b..d0666d0 100644 --- a/aoc2018/src/day01.rs +++ b/aoc2018/src/day01.rs @@ -1,7 +1,7 @@ use std::collections::HashSet; use std::fmt::Write; -use aoc::Result; +use anyhow::Result; const INPUT: &str = include_str!("../input/day01.txt"); diff --git a/aoc2018/src/day02.rs b/aoc2018/src/day02.rs index cae3cab..84e2b4a 100644 --- a/aoc2018/src/day02.rs +++ b/aoc2018/src/day02.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use std::fmt::Write; -use aoc::Result; +use anyhow::Result; const INPUT: &str = include_str!("../input/day02.txt"); diff --git a/aoc2018/src/day03.rs b/aoc2018/src/day03.rs index 0c5ee04..f8cb724 100644 --- a/aoc2018/src/day03.rs +++ b/aoc2018/src/day03.rs @@ -1,11 +1,9 @@ use std::collections::HashMap; use std::collections::HashSet; -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/day03.txt"); @@ -27,7 +25,7 @@ struct Claim { } impl FromStr for Claim { - type Err = Box; + type Err = anyhow::Error; /// Parses a claim from a line /// @@ -41,22 +39,20 @@ impl FromStr for Claim { let s = &s[1..]; // find ' @ ' separator - let at = s - .find(" @ ") - .ok_or_else(|| err!("` @ ` delimiter not found"))?; + let at = s.find(" @ ").context("` @ ` delimiter not found")?; let id = s[..at].parse()?; let s = &s[(at + 3)..]; // parse 'X,Y: WxH - let comma = s.find(',').ok_or_else(|| err!("`,` delimiter not found"))?; - let colon = s.find(':').ok_or_else(|| err!("`:` delimiter not found"))?; + let comma = s.find(',').context("`,` delimiter not found")?; + let colon = s.find(':').context("`:` delimiter not found")?; let x = s[..comma].parse()?; let y = s[(comma + 1)..colon].parse()?; // reduce line to 'WxH' let s = &s[(colon + 2)..]; - let sep = s.find('x').ok_or_else(|| err!("`x` delimiter not found"))?; + let sep = s.find('x').context("`x` delimiter not found")?; let width = s[..sep].parse()?; let height = s[(sep + 1)..].parse()?; @@ -77,7 +73,7 @@ fn part1(input: &str) -> Result { for line in input.lines() { let claim: Claim = line .parse() - .or_else(|e| Err(err!("couldn't parse line: `{}`, {}", line, e)))?; + .with_context(|| format!("couldn't parse line: `{}`", line))?; for i in 0..claim.width { for j in 0..claim.height { @@ -106,7 +102,7 @@ fn part2(input: &str) -> Result { for line in input.lines() { let claim: Claim = line .parse() - .or_else(|e| Err(err!("couldn't parse line: `{}`, {}", line, e)))?; + .with_context(|| format!("couldn't parse line: `{}`", line))?; set.insert(claim.id); for i in 0..claim.width { diff --git a/aoc2018/src/day04.rs b/aoc2018/src/day04.rs index 6ff8cab..b8c837b 100644 --- a/aoc2018/src/day04.rs +++ b/aoc2018/src/day04.rs @@ -1,10 +1,8 @@ use std::collections::HashMap; -use std::error::Error; use std::fmt::Write; use std::str::FromStr; -use aoc::err; -use aoc::Result; +use anyhow::{anyhow, bail, Context, Result}; const INPUT: &str = include_str!("../input/day04.txt"); @@ -30,7 +28,7 @@ enum Event { } impl FromStr for Event { - type Err = Box; + type Err = anyhow::Error; fn from_str(s: &str) -> Result { if s.find("wakes up").is_some() { @@ -38,13 +36,13 @@ impl FromStr for Event { } else if s.find("falls asleep").is_some() { Ok(Event::FallAsleep) } else if s.find("begins shift").is_some() { - let pound = s.find('#').ok_or_else(|| err!("`#` not found"))?; + let pound = s.find('#').context("`#` not found")?; let s = &s[(pound + 1)..]; - let space = s.find(' ').ok_or_else(|| err!("` ` not found after `#`"))?; + let space = s.find(' ').context("` ` not found after `#`")?; let id = s[..space].parse()?; Ok(Event::ShiftChange(id)) } else { - Err(err!("unknown event type")) + Err(anyhow!("unknown event type")) } } } @@ -60,28 +58,28 @@ struct Date { } impl FromStr for Date { - type Err = Box; + type Err = anyhow::Error; fn from_str(s: &str) -> Result { - let lbracket = s.find('[').ok_or_else(|| err!("`[` not found"))?; + let lbracket = s.find('[').context("`[` not found")?; let s = &s[(lbracket + 1)..]; - let dash = s.find('-').ok_or_else(|| err!("`-` not found"))?; + let dash = s.find('-').context("`-` not found")?; let year = s[..dash].parse()?; let s = &s[(dash + 1)..]; - let dash = s.find('-').ok_or_else(|| err!("`-` not found"))?; + let dash = s.find('-').context("`-` not found")?; let month = s[..dash].parse()?; let s = &s[(dash + 1)..]; - let space = s.find(' ').ok_or_else(|| err!("` ` not found"))?; + let space = s.find(' ').context("` ` not found")?; let day = s[..space].parse()?; let s = &s[(space + 1)..]; - let colon = s.find(':').ok_or_else(|| err!("`:` not found"))?; + let colon = s.find(':').context("`:` not found")?; let hour = s[..colon].parse()?; let s = &s[(colon + 1)..]; - let rbracket = s.find(']').ok_or_else(|| err!("`]` not found"))?; + let rbracket = s.find(']').context("`]` not found")?; let minute = s[..rbracket].parse()?; @@ -102,11 +100,11 @@ struct LogEntry { } impl FromStr for LogEntry { - type Err = Box; + type Err = anyhow::Error; fn from_str(s: &str) -> Result { - let date: Date = s.parse().map_err(|e| err!("couldn't parse date: {}", e))?; - let event = s.parse().map_err(|e| err!("couldn't parse event: {}", e))?; + let date: Date = s.parse().context("couldn't parse date")?; + let event = s.parse().context("couldn't parse event")?; let entry = LogEntry { date, event }; @@ -134,7 +132,7 @@ fn part1(input: &str) -> Result { match guard_id { Some(id) => map.entry(id).or_default().push(log_entry), - None => return Err(err!("event before first shift")), + None => bail!("event before first shift"), } } @@ -157,13 +155,13 @@ fn part1(input: &str) -> Result { } fell_asleep = None; } - None => return Err(err!("woke up before falling asleep")), + None => bail!("woke up before falling asleep"), }, } } if fell_asleep.is_some() { - return Err(err!("fell asleep but never woke up!")); + bail!("fell asleep but never woke up!"); } sleep_freq_per_guard.insert(id, sleep_freq); diff --git a/aoc2018/src/day05.rs b/aoc2018/src/day05.rs index a17af8c..9b2c52a 100644 --- a/aoc2018/src/day05.rs +++ b/aoc2018/src/day05.rs @@ -1,7 +1,7 @@ use std::collections::HashSet; use std::fmt::Write; -use aoc::Result; +use anyhow::Result; const INPUT: &str = include_str!("../input/day05.txt"); diff --git a/aoc2018/src/main.rs b/aoc2018/src/main.rs index a4b9ebf..ec54580 100644 --- a/aoc2018/src/main.rs +++ b/aoc2018/src/main.rs @@ -1,5 +1,6 @@ +use anyhow::Result; + use aoc::DayFunc; -use aoc::Result; use aoc2018::day01; use aoc2018::day02;