aoc_next/
macros.rs

1// SPDX-FileCopyrightText: 2020 Florian Warzecha <liketechnik@disroot.org>
2//
3// SPDX-License-Identifier: MPL-2.0
4//! Helper macros to ease the declaration of the functions that contain your solutions of the
5//! exercises.
6//!
7//! * Use [`solution!()`] to declare a new solution inside of
8//! [`crate::app::Aoc#structfield.solutions`].
9//! * The library currently expects that you want to parse the input. Use either:
10//!     * [`failable_parser!()`] if the parsing function returns a [`std::result::Result`];
11//!     * [`parser!()`] if the parsing function returns the plain return value.
12//! * Use [`solver!()`] to point to the function that solves the exercise.
13
14/// Declare a new solution:
15///
16/// * `$day` is the day of the exercise this solution solves. This is used to download the input
17/// file automatically.
18/// * `$parser` is an instance of [`crate::parser::Parsing`]. (See [`failable_parser!()`] or
19/// [`parser!()`])
20/// * `$solver` is an instance of [`crate::solution::Solver`]. (See [`solver!()`])
21#[macro_export]
22macro_rules! solution {
23    ($day:expr, $parser:expr, $solver:expr) => {{
24        &$crate::Solution {
25            day: $day,
26            parser: $parser,
27            solver: $solver,
28        }
29    }};
30}
31
32/// Declare a new failable parser.
33///
34/// The function has to take a `&`[`std::str`].
35/// The return type must be a [`std::result::Result`],
36/// though the inner types have no restriction, as long
37/// as the [`std::result::Result::Ok`] type matches the parameter type of the solving function.
38#[macro_export]
39macro_rules! failable_parser {
40    ($parser:expr) => {{
41        $crate::FailableParser {
42            run: $parser,
43            name: stringify!($parser),
44        }
45    }};
46}
47
48/// Declare a new infailable parser.
49///
50/// The function has to take a `&`[`std::str`].
51/// The return type has no restriction, as long
52/// as it matches the parameter type of the solving function.
53#[macro_export]
54macro_rules! parser {
55    ($parser:expr) => {{
56        $crate::Parser {
57            run: $parser,
58            name: stringify!($parser),
59        }
60    }};
61}
62
63/// Declare a new solver, e. g. a funtion that solves an exercise.
64///
65/// The function's parameter type has to match the return value of
66/// the parser used for the [`crate::solution::Solution`].
67/// The return value must implement [`std::fmt::Display`],
68/// so the result can be printed to stdout.
69#[macro_export]
70macro_rules! solver {
71    ($solver:expr) => {{
72        $crate::Solver {
73            marker: ::std::marker::PhantomData,
74            name: stringify!($solver),
75            run: $solver,
76        }
77    }};
78}