switching 2023 day1 and day2 to this error and error-stack
This commit is contained in:
@@ -14,6 +14,7 @@ error-stack.workspace = true
|
||||
itertools.workspace = true
|
||||
log.workspace = true
|
||||
nom.workspace = true
|
||||
thiserror.workspace = true
|
||||
|
||||
[dev-dependencies]
|
||||
test-log = {workspace=true}
|
||||
|
||||
@@ -3,10 +3,22 @@
|
||||
use day_1::part1::part1;
|
||||
use day_1::part2::part2;
|
||||
|
||||
fn main() {
|
||||
let input = include_str!("./input.txt");
|
||||
let part1_result = part1(input).unwrap();
|
||||
println!("part 1: {part1_result}");
|
||||
let part2_result = part2(input).unwrap();
|
||||
println!("part 2: {part2_result}");
|
||||
use error_stack::{Result, ResultExt};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
enum Day1Error {
|
||||
#[error("Part 1 failed")]
|
||||
Part1Error,
|
||||
#[error("Part 2 failed")]
|
||||
Part2Error,
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Day1Error> {
|
||||
let input = include_str!("./input.txt");
|
||||
let part1_result = part1(input).change_context(Day1Error::Part1Error)?;
|
||||
println!("part 1: {part1_result}");
|
||||
let part2_result = part2(input).change_context(Day1Error::Part2Error)?;
|
||||
println!("part 2: {part2_result}");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
#![warn(clippy::all, clippy::pedantic)]
|
||||
|
||||
use std::fmt::Display;
|
||||
|
||||
use log::trace;
|
||||
use nom::{
|
||||
self,
|
||||
@@ -9,17 +7,15 @@ use nom::{
|
||||
multi::separated_list1,
|
||||
};
|
||||
|
||||
use error_stack::{Context, Report, Result, ResultExt};
|
||||
use error_stack::{Report, Result, ResultExt};
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Day1Part1Error;
|
||||
|
||||
impl Context for Day1Part1Error {}
|
||||
|
||||
impl Display for Day1Part1Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "day 1 part 1 error")
|
||||
}
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Day1Part1Error {
|
||||
#[error("Problem parsing Day 1")]
|
||||
ParseError,
|
||||
#[error("Day 1 Input parsed to Empty")]
|
||||
EmptyInput,
|
||||
}
|
||||
|
||||
/// Day-1 part 1 of AC2023
|
||||
@@ -32,21 +28,16 @@ impl Display for Day1Part1Error {
|
||||
pub fn part1(input: &str) -> Result<String, Day1Part1Error> {
|
||||
let (_input, values) = parse_input(input)
|
||||
.map_err(|x| Report::from(x.to_owned()))
|
||||
.change_context(Day1Part1Error)?;
|
||||
.change_context(Day1Part1Error::ParseError)?;
|
||||
trace!("{values:?}");
|
||||
values
|
||||
.iter()
|
||||
.map(|v| {
|
||||
v.first()
|
||||
.and_then(|first| v.last().map(|last| *first * 10 + *last))
|
||||
.ok_or(Day1Part1Error)
|
||||
})
|
||||
.try_fold(0_u32, |sum, number| {
|
||||
let Ok(number) = number else {
|
||||
return Err(Report::from(Day1Part1Error));
|
||||
};
|
||||
Ok(sum + number)
|
||||
.ok_or(Day1Part1Error::EmptyInput)
|
||||
})
|
||||
.try_fold(0_u32, |sum, number| Ok(sum + number?))
|
||||
.map(|x| x.to_string())
|
||||
}
|
||||
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
#![warn(clippy::all, clippy::pedantic)]
|
||||
|
||||
use std::{fmt::Display, ops::Not};
|
||||
use std::ops::Not;
|
||||
|
||||
use error_stack::{Context, Report, Result};
|
||||
use error_stack::{report, Result};
|
||||
use log::trace;
|
||||
use thiserror::Error;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Day1Part2Error;
|
||||
|
||||
impl Context for Day1Part2Error {}
|
||||
|
||||
impl Display for Day1Part2Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "day 1 part 2 error")
|
||||
}
|
||||
#[derive(Debug, Error)]
|
||||
pub enum Day1Part2Error {
|
||||
#[error("Problem parsing Day-1 Part2")]
|
||||
ParseError,
|
||||
#[error("Day 1 Input parsed to Empty")]
|
||||
EmptyInput,
|
||||
}
|
||||
|
||||
/// Day 1 Part 2 of AOC2023
|
||||
@@ -34,14 +32,9 @@ pub fn part2(input: &str) -> Result<String, Day1Part2Error> {
|
||||
.map(|v| {
|
||||
v.first()
|
||||
.and_then(|first| v.last().map(|last| *first * 10 + *last))
|
||||
.ok_or(Day1Part2Error)
|
||||
})
|
||||
.try_fold(0_u32, |sum, number| {
|
||||
let Ok(number) = number else {
|
||||
return Err(Report::from(Day1Part2Error));
|
||||
};
|
||||
Ok(sum + number)
|
||||
.ok_or(report!(Day1Part2Error::EmptyInput))
|
||||
})
|
||||
.try_fold(0_u32, |sum, number| Ok(sum + number?))
|
||||
.map(|x| x.to_string())
|
||||
}
|
||||
|
||||
@@ -80,7 +73,7 @@ fn parse_line(line: &str) -> Result<Vec<u32>, Day1Part2Error> {
|
||||
.is_empty()
|
||||
.not()
|
||||
.then_some(numbers)
|
||||
.ok_or(Report::from(Day1Part2Error))
|
||||
.ok_or(report!(Day1Part2Error::ParseError))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Reference in New Issue
Block a user