Switch from enum_dispatch to a custom macro

This commit is contained in:
Quentin Boyer 2024-11-27 12:25:31 +01:00
parent 2d4cec8392
commit 8004d6ee68
3 changed files with 44 additions and 23 deletions

View file

@ -1,9 +1,10 @@
use std::fmt::Display;
use clap::{Parser, Subcommand};
use enum_dispatch::enum_dispatch;
mod one;
#[derive(Parser)]
#[derive(Parser, Default)]
struct GlobalArgs {}
#[derive(Parser)]
@ -14,16 +15,42 @@ struct Args {
global: GlobalArgs,
}
#[enum_dispatch]
trait Problem {
fn solve(self, args: GlobalArgs) -> color_eyre::Result<()>;
type Solution: Display;
fn solve(self, args: GlobalArgs) -> color_eyre::Result<Self::Solution>;
}
#[derive(Subcommand)]
#[enum_dispatch(Problem)]
enum Problems {
#[clap(name = "1")]
One(one::Args),
macro_rules! problems {
($($module:ident = $alias:tt),* $(,)?) => {
paste::paste! {
#[derive(Subcommand)]
enum Problems {
$(
#[clap(name = stringify!($alias))]
[<$module:camel>]($module::Args),
)*
}
impl Problems {
pub fn solve(self, args: GlobalArgs) -> color_eyre::Result<()> {
match self {
$(
Self::[<$module:camel>](i) => {
println!("Solution: {}", i.solve(args)?);
}
)*
}
Ok(())
}
}
}
};
}
problems! {
one = 1,
two = 2,
}
fn main() -> color_eyre::Result<()> {