diff --git a/template/day-n/Cargo.toml b/template/day-n/Cargo.toml index 8135e28..cc7613c 100644 --- a/template/day-n/Cargo.toml +++ b/template/day-n/Cargo.toml @@ -7,11 +7,13 @@ repository.workspace = true # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + [dependencies] -nom = { workspace = true } -itertools = {workspace = true } -error-stack = {workspace = true} +nom.workspace = true +itertools.workspace = true +log.workspace = true +error-stack.workspace = true +thiserror.workspace = true [dev-dependencies] -test-log = {workspace=true} - +test-log.workspace = true diff --git a/template/day-n/src/main.rs b/template/day-n/src/main.rs index bd05632..64b3152 100644 --- a/template/day-n/src/main.rs +++ b/template/day-n/src/main.rs @@ -1,12 +1,24 @@ #![warn(clippy::all, clippy::pedantic)] -use {{crate_name}}::part1; -use {{crate_name}}::part2; +use {{ crate_name }}::part1; +use {{ crate_name }}::part2; -fn main() { - let input = include_str!("./input.txt"); - let part1_result = part1(input).expect("part 1 should have no error"); - println!("part 1: {part1_result}"); - let part2_result = part2(input).expect("part 2 should have no error"); - println!("part 2: {part2_result}"); +use error_stack::{Result, ResultExt}; +use thiserror::Error; + +#[derive(Debug, Error)] +enum {{ project-name | upper_camel_case }}Error { + #[error("Part 1 failed")] + Part1Error, + #[error("Part 2 failed")] + Part2Error, +} + +fn main() -> Result<(), {{ project-name | upper_camel_case }}Error> { + let input = include_str!("./input.txt"); + let part1_result = part1(input).change_context({{ project-name | upper_camel_case }}Error::Part1Error)?; + println!("part 1: {part1_result}"); + let part2_result = part2(input).change_context({{ project-name | upper_camel_case }}Error::Part2Error)?; + println!("part 2: {part2_result}"); + Ok(()) } diff --git a/template/day-n/src/part1.rs b/template/day-n/src/part1.rs index 74c80b9..990eb7e 100644 --- a/template/day-n/src/part1.rs +++ b/template/day-n/src/part1.rs @@ -1,22 +1,16 @@ #![warn(clippy::all, clippy::pedantic)] -use std::fmt::Display; - -use error_stack::{Context, Result}; +use error_stack::Result; +use thiserror::Error; // {{project-name}} -#[derive(Debug)] -pub struct {{project-name|upper_camel_case}}Part1Error; - -impl Context for {{project-name|upper_camel_case}}Part1Error {} - -impl Display for {{project-name|upper_camel_case}}Part1Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "day 1 part 1 error") - } +#[derive(Debug, Error)] +pub enum {{ project-name | upper_camel_case }}Part1Error{ + #[error("Problem parsing {{ project-name | title_case }}")] + ParseError, } -pub fn part1 (_input: &str) -> Result { +pub fn part1 (_input: &str) -> Result { Ok("Not Finished".to_string()) } diff --git a/template/day-n/src/part2.rs b/template/day-n/src/part2.rs index 5169fc1..613c075 100644 --- a/template/day-n/src/part2.rs +++ b/template/day-n/src/part2.rs @@ -1,18 +1,13 @@ #![warn(clippy::all, clippy::pedantic)] -use std::fmt::Display; +use error_stack::Result; +use thiserror::Error; -use error_stack::{Context, Result}; - -#[derive(Debug)] -pub struct {{ project-name | upper_camel_case }}Part2Error; - -impl Context for {{ project-name | upper_camel_case }}Part2Error {} - -impl Display for {{ project-name | upper_camel_case }}Part2Error { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - write!(f, "day 1 part 1 error") - } +// {{project-name}} +#[derive(Debug, Error)] +pub enum {{ project-name | upper_camel_case }}Part2Error{ + #[error("Problem parsing {{project-name | title_case }}")] + ParseError, } pub fn part2 (_input: &str) -> Result { diff --git a/template/year-n/Cargo.toml b/template/year-n/Cargo.toml index 63fa9e9..07fcd65 100644 --- a/template/year-n/Cargo.toml +++ b/template/year-n/Cargo.toml @@ -25,6 +25,7 @@ num-traits = "0.2.17" rustworkx-core = "0.13.2" pathfinding = "4.8.0" test-log = {version="0.2.14", features=["default", "unstable"]} +thiserror = "1.0.56" [profile.dhat]