diff --git a/template/day-n/Cargo.toml b/template/day-n/Cargo.toml index 20c5d95..8135e28 100644 --- a/template/day-n/Cargo.toml +++ b/template/day-n/Cargo.toml @@ -10,3 +10,8 @@ repository.workspace = true [dependencies] nom = { workspace = true } itertools = {workspace = true } +error-stack = {workspace = true} + +[dev-dependencies] +test-log = {workspace=true} + diff --git a/template/day-n/src/main.rs b/template/day-n/src/main.rs index 07ab7f3..bd05632 100644 --- a/template/day-n/src/main.rs +++ b/template/day-n/src/main.rs @@ -5,8 +5,8 @@ use {{crate_name}}::part2; fn main() { let input = include_str!("./input.txt"); - let part1_result = part1(input); + let part1_result = part1(input).expect("part 1 should have no error"); println!("part 1: {part1_result}"); - let part2_result = part2(input); + let part2_result = part2(input).expect("part 2 should have no error"); println!("part 2: {part2_result}"); } diff --git a/template/day-n/src/part1.rs b/template/day-n/src/part1.rs index a7db908..74c80b9 100644 --- a/template/day-n/src/part1.rs +++ b/template/day-n/src/part1.rs @@ -1,8 +1,23 @@ #![warn(clippy::all, clippy::pedantic)] -#[must_use] -pub fn part1 (_input: &str) -> String { - "Not Finished".to_string() +use std::fmt::Display; + +use error_stack::{Context, Result}; + +// {{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") + } +} + +pub fn part1 (_input: &str) -> Result { + Ok("Not Finished".to_string()) } #[cfg(test)] @@ -11,9 +26,10 @@ mod test { const INPUT: &str = ""; - #[test] + #[test_log::test] + #[test_log(default_log_filter = "trace")] fn part1_works() { - let result = part1(INPUT); + let result = part1(INPUT).unwrap(); assert_eq!(result, "Not Finished".to_string()); } } diff --git a/template/day-n/src/part2.rs b/template/day-n/src/part2.rs index 8f15571..5169fc1 100644 --- a/template/day-n/src/part2.rs +++ b/template/day-n/src/part2.rs @@ -1,8 +1,22 @@ #![warn(clippy::all, clippy::pedantic)] -#[must_use] -pub fn part2 (_input: &str) -> String { - "Not Finished".to_string() +use std::fmt::Display; + +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") + } +} + +pub fn part2 (_input: &str) -> Result { + Ok("Not Finished".to_string()) } #[cfg(test)] @@ -11,9 +25,10 @@ mod test { const INPUT: &str = ""; - #[test] + #[test_log::test] + #[test_log(default_log_filter = "trace")] fn part2_works() { - let result = part2(INPUT); + let result = part2(INPUT).unwrap(); assert_eq!(result, "Not Finished".to_string()); } }