Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize cmd-impl #289

Merged
merged 2 commits into from
Nov 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion bin/subalfred/src/command/impl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ version = "0.9.0-rc16"
proc-macro = true

[dependencies]
proc-macro2 = { version = "1.0" }
quote = { version = "1.0" }
syn = { version = "1.0", features = ["full"] }

Expand Down
53 changes: 53 additions & 0 deletions bin/subalfred/src/command/impl/debug/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 17 additions & 21 deletions bin/subalfred/src/command/impl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,25 @@
// proc-macro
use proc_macro::TokenStream;
// crates.io
use proc_macro2::TokenStream as TokenStream2;
use syn::*;

/// Quickly define and implement a command containing serval subcommands.
#[proc_macro_attribute]
pub fn cmd(_: TokenStream, input: TokenStream) -> TokenStream {
let item_enum = syn::parse_macro_input!(input as ItemEnum);
let cmd_enum = syn::parse_macro_input!(input as ItemEnum);

// #[cfg(feature = "debug")]
// dbg!(&item_enum);
// dbg!(&cmd_enum);

let ItemEnum { attrs, vis, ident, variants, .. } = item_enum;
let variant_runs = variants
.iter()
.map(|v| v.ident.to_string().parse::<TokenStream2>().unwrap())
.map(|n| {
quote::quote! {
Self::#n(cmd) => { cmd.run() }
}
})
.collect::<Vec<_>>();
let variants = variants
let ItemEnum {
attrs: cmd_attrs, vis: cmd_vis, ident: cmd_name, variants: cmd_variants, ..
} = cmd_enum;
let cmd_variants_names =
cmd_variants.iter().map(|variant| variant.ident.clone()).collect::<Vec<_>>();
let cmd_variants = cmd_variants
.into_iter()
.map(|Variant { attrs, ident, .. }| {
let cmd = format!("{}Cmd", ident).parse::<TokenStream2>().unwrap();
let cmd = quote::format_ident!("{ident}Cmd");

quote::quote! {
#(#attrs)*
Expand All @@ -40,14 +34,16 @@ pub fn cmd(_: TokenStream, input: TokenStream) -> TokenStream {

quote::quote! {
#[derive(Debug, clap::Subcommand)]
#(#attrs)*
#vis enum #ident {
#(#variants,)*
#(#cmd_attrs)*
#cmd_vis enum #cmd_name {
#(#cmd_variants,)*
}
impl #ident {
#vis fn run(&self) -> crate::prelude::Result<()> {
impl #cmd_name {
#cmd_vis fn run(&self) -> crate::prelude::Result<()> {
match self {
#(#variant_runs,)*
#(
Self::#cmd_variants_names(cmd) => cmd.run(),
)*
}
}
}
Expand Down