From 8004d6ee68bb0158f3e02568fdd697a3ab6ec1ae Mon Sep 17 00:00:00 2001 From: Quentin Boyer Date: Wed, 27 Nov 2024 12:25:31 +0100 Subject: [PATCH] Switch from enum_dispatch to a custom macro --- Cargo.lock | 20 +++++++------------- Cargo.toml | 2 +- src/main.rs | 45 ++++++++++++++++++++++++++++++++++++--------- 3 files changed, 44 insertions(+), 23 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c3dd9f9..8759325 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -169,25 +169,13 @@ version = "1.0.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990" -[[package]] -name = "enum_dispatch" -version = "0.3.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa18ce2bc66555b3218614519ac839ddb759a7d6720732f979ef8d13be147ecd" -dependencies = [ - "once_cell", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "euler" version = "0.1.0" dependencies = [ "clap", "color-eyre", - "enum_dispatch", + "paste", ] [[package]] @@ -272,6 +260,12 @@ version = "3.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" +[[package]] +name = "paste" +version = "1.0.15" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a" + [[package]] name = "pin-project-lite" version = "0.2.15" diff --git a/Cargo.toml b/Cargo.toml index ccc1bbc..84a0d79 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,4 +7,4 @@ edition = "2021" [dependencies] clap = { version = "4.5.21", features = ["derive"] } color-eyre = "0.6.3" -enum_dispatch = "0.3.13" +paste = "1.0.15" diff --git a/src/main.rs b/src/main.rs index 516a33f..8214579 100644 --- a/src/main.rs +++ b/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; } -#[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<()> {