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
20
Cargo.lock
generated
20
Cargo.lock
generated
|
|
@ -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"
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
|
|
|
|||
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