Switch from enum_dispatch to a custom macro
This commit is contained in:
parent
2d4cec8392
commit
8004d6ee68
3 changed files with 44 additions and 23 deletions
45
src/main.rs
45
src/main.rs
|
|
@ -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<()> {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue