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

refactor(derive): Always do app_methods last #2819

Merged
merged 2 commits into from
Oct 7, 2021
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
10 changes: 2 additions & 8 deletions clap_derive/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -764,11 +764,12 @@ impl Attrs {

/// generate methods from attributes on top of struct or enum
pub fn top_level_methods(&self) -> TokenStream {
let version = &self.version;
let author = &self.author;
let methods = &self.methods;
let doc_comment = &self.doc_comment;

quote!( #(#doc_comment)* #author #(#methods)*)
quote!( #(#doc_comment)* #author #version #(#methods)*)
}

/// generate methods on top of a field
Expand All @@ -778,13 +779,6 @@ impl Attrs {
quote!( #(#doc_comment)* #(#methods)* )
}

pub fn version(&self) -> TokenStream {
self.version
.clone()
.map(|m| m.to_token_stream())
.unwrap_or_default()
}

pub fn cased_name(&self) -> TokenStream {
self.name.clone().translate(*self.casing)
}
Expand Down
4 changes: 1 addition & 3 deletions clap_derive/src/derives/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -350,12 +350,10 @@ pub fn gen_augment(
});

let app_methods = parent_attribute.top_level_methods();
let version = parent_attribute.version();
quote! {{
#( #args )*
let #app_var = #app_var#app_methods;
#subcmd
#app_var#version
#app_var#app_methods
}}
}

Expand Down
10 changes: 3 additions & 7 deletions clap_derive/src/derives/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,12 @@ fn gen_augment(

let name = attrs.cased_name();
let from_attrs = attrs.top_level_methods();
let version = attrs.version();
let subcommand = quote! {
let app = app.subcommand({
let #app_var = clap::App::new(#name);
let #app_var = #arg_block;
let #app_var = #app_var.setting(::clap::AppSettings::SubcommandRequiredElseHelp);
#app_var#from_attrs#version
#app_var#from_attrs
});
};
Some(subcommand)
Expand Down Expand Up @@ -257,12 +256,11 @@ fn gen_augment(

let name = attrs.cased_name();
let from_attrs = attrs.top_level_methods();
let version = attrs.version();
let subcommand = quote! {
let app = app.subcommand({
let #app_var = clap::App::new(#name);
let #app_var = #arg_block;
#app_var#from_attrs#version
#app_var#from_attrs
});
};
Some(subcommand)
Expand All @@ -272,11 +270,9 @@ fn gen_augment(
.collect();

let app_methods = parent_attribute.top_level_methods();
let version = parent_attribute.version();
quote! {
let app = app #app_methods;
#( #subcommands )*;
app #version
app #app_methods
}
}

Expand Down