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"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "5b63caa9aa9397e2d9480a9b13673856c78d8ac123288526c37d7839f2a86990"
|
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]]
|
[[package]]
|
||||||
name = "euler"
|
name = "euler"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"color-eyre",
|
"color-eyre",
|
||||||
"enum_dispatch",
|
"paste",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
|
|
@ -272,6 +260,12 @@ version = "3.5.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
|
checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "paste"
|
||||||
|
version = "1.0.15"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pin-project-lite"
|
name = "pin-project-lite"
|
||||||
version = "0.2.15"
|
version = "0.2.15"
|
||||||
|
|
|
||||||
|
|
@ -7,4 +7,4 @@ edition = "2021"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5.21", features = ["derive"] }
|
clap = { version = "4.5.21", features = ["derive"] }
|
||||||
color-eyre = "0.6.3"
|
color-eyre = "0.6.3"
|
||||||
enum_dispatch = "0.3.13"
|
paste = "1.0.15"
|
||||||
|
|
|
||||||
41
src/main.rs
41
src/main.rs
|
|
@ -1,9 +1,10 @@
|
||||||
|
use std::fmt::Display;
|
||||||
|
|
||||||
use clap::{Parser, Subcommand};
|
use clap::{Parser, Subcommand};
|
||||||
use enum_dispatch::enum_dispatch;
|
|
||||||
|
|
||||||
mod one;
|
mod one;
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser, Default)]
|
||||||
struct GlobalArgs {}
|
struct GlobalArgs {}
|
||||||
|
|
||||||
#[derive(Parser)]
|
#[derive(Parser)]
|
||||||
|
|
@ -14,16 +15,42 @@ struct Args {
|
||||||
global: GlobalArgs,
|
global: GlobalArgs,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[enum_dispatch]
|
|
||||||
trait Problem {
|
trait Problem {
|
||||||
fn solve(self, args: GlobalArgs) -> color_eyre::Result<()>;
|
type Solution: Display;
|
||||||
|
|
||||||
|
fn solve(self, args: GlobalArgs) -> color_eyre::Result<Self::Solution>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
macro_rules! problems {
|
||||||
|
($($module:ident = $alias:tt),* $(,)?) => {
|
||||||
|
paste::paste! {
|
||||||
#[derive(Subcommand)]
|
#[derive(Subcommand)]
|
||||||
#[enum_dispatch(Problem)]
|
|
||||||
enum Problems {
|
enum Problems {
|
||||||
#[clap(name = "1")]
|
$(
|
||||||
One(one::Args),
|
#[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<()> {
|
fn main() -> color_eyre::Result<()> {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue