adventurous/solve.rs
1use std::fmt::{Debug, Display};
2
3use anyhow::Result;
4
5use crate::Input;
6
7/// A trait for solving an Advent of Code puzzle.
8///
9/// This trait is automatically implemented for functions that take a reference
10/// to an [`Input`] and return a [`Result`] containing a type that implements
11/// [`Debug`], [`Display`], and [`PartialEq`]:
12///
13/// ```
14/// use adventurous::Input;
15/// use anyhow::Result;
16///
17/// fn part_one(input: &Input) -> Result<usize> {
18/// Ok(42)
19/// }
20/// ```
21pub trait Solve {
22 /// The answer to the puzzle.
23 type Answer: Debug + Display + PartialEq;
24
25 /// Produces an answer from the provided [`Input`].
26 fn solve(&self, input: &Input) -> Result<Self::Answer>;
27}
28
29impl<A, F> Solve for F
30where
31 A: Debug + Display + PartialEq,
32 F: Fn(&Input) -> Result<A>,
33{
34 type Answer = A;
35
36 fn solve(&self, input: &Input) -> Result<Self::Answer> {
37 self(input)
38 }
39}