mirror of
https://github.com/clap-rs/clap
synced 2024-11-10 14:54:15 +00:00
refactor(derive): Be explicit in what is being parsed
This commit is contained in:
parent
87cc6f9a1e
commit
78dc60bd37
5 changed files with 49 additions and 19 deletions
|
@ -54,7 +54,37 @@ pub struct Attrs {
|
|||
}
|
||||
|
||||
impl Attrs {
|
||||
pub fn from_struct(
|
||||
pub fn from_args_struct(
|
||||
span: Span,
|
||||
attrs: &[Attribute],
|
||||
name: Name,
|
||||
argument_casing: Sp<CasingStyle>,
|
||||
env_casing: Sp<CasingStyle>,
|
||||
) -> Self {
|
||||
Self::from_struct(span, attrs, name, argument_casing, env_casing)
|
||||
}
|
||||
|
||||
pub fn from_subcommand_enum(
|
||||
span: Span,
|
||||
attrs: &[Attribute],
|
||||
name: Name,
|
||||
argument_casing: Sp<CasingStyle>,
|
||||
env_casing: Sp<CasingStyle>,
|
||||
) -> Self {
|
||||
Self::from_struct(span, attrs, name, argument_casing, env_casing)
|
||||
}
|
||||
|
||||
pub fn from_value_enum(
|
||||
span: Span,
|
||||
attrs: &[Attribute],
|
||||
name: Name,
|
||||
argument_casing: Sp<CasingStyle>,
|
||||
env_casing: Sp<CasingStyle>,
|
||||
) -> Self {
|
||||
Self::from_struct(span, attrs, name, argument_casing, env_casing)
|
||||
}
|
||||
|
||||
fn from_struct(
|
||||
span: Span,
|
||||
attrs: &[Attribute],
|
||||
name: Name,
|
||||
|
@ -90,7 +120,7 @@ impl Attrs {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_variant(
|
||||
pub fn from_subcommand_variant(
|
||||
variant: &Variant,
|
||||
struct_casing: Sp<CasingStyle>,
|
||||
env_casing: Sp<CasingStyle>,
|
||||
|
@ -233,7 +263,7 @@ impl Attrs {
|
|||
}
|
||||
}
|
||||
|
||||
pub fn from_field(
|
||||
pub fn from_args_field(
|
||||
field: &Field,
|
||||
struct_casing: Sp<CasingStyle>,
|
||||
env_casing: Sp<CasingStyle>,
|
||||
|
|
|
@ -57,7 +57,7 @@ pub fn gen_for_struct(
|
|||
) -> TokenStream {
|
||||
let from_arg_matches = gen_from_arg_matches_for_struct(struct_name, generics, fields, attrs);
|
||||
|
||||
let attrs = Attrs::from_struct(
|
||||
let attrs = Attrs::from_args_struct(
|
||||
Span::call_site(),
|
||||
attrs,
|
||||
Name::Derived(struct_name.clone()),
|
||||
|
@ -103,7 +103,7 @@ pub fn gen_from_arg_matches_for_struct(
|
|||
fields: &Punctuated<Field, Comma>,
|
||||
attrs: &[Attribute],
|
||||
) -> TokenStream {
|
||||
let attrs = Attrs::from_struct(
|
||||
let attrs = Attrs::from_args_struct(
|
||||
Span::call_site(),
|
||||
attrs,
|
||||
Name::Derived(struct_name.clone()),
|
||||
|
@ -164,7 +164,7 @@ pub fn gen_augment(
|
|||
override_required: bool,
|
||||
) -> TokenStream {
|
||||
let mut subcmds = fields.iter().filter_map(|field| {
|
||||
let attrs = Attrs::from_field(
|
||||
let attrs = Attrs::from_args_field(
|
||||
field,
|
||||
parent_attribute.casing(),
|
||||
parent_attribute.env_casing(),
|
||||
|
@ -210,7 +210,7 @@ pub fn gen_augment(
|
|||
}
|
||||
|
||||
let args = fields.iter().filter_map(|field| {
|
||||
let attrs = Attrs::from_field(
|
||||
let attrs = Attrs::from_args_field(
|
||||
field,
|
||||
parent_attribute.casing(),
|
||||
parent_attribute.env_casing(),
|
||||
|
@ -342,7 +342,7 @@ pub fn gen_augment(
|
|||
|
||||
pub fn gen_constructor(fields: &Punctuated<Field, Comma>, parent_attribute: &Attrs) -> TokenStream {
|
||||
let fields = fields.iter().map(|field| {
|
||||
let attrs = Attrs::from_field(
|
||||
let attrs = Attrs::from_args_field(
|
||||
field,
|
||||
parent_attribute.casing(),
|
||||
parent_attribute.env_casing(),
|
||||
|
@ -409,7 +409,7 @@ pub fn gen_updater(
|
|||
use_self: bool,
|
||||
) -> TokenStream {
|
||||
let fields = fields.iter().map(|field| {
|
||||
let attrs = Attrs::from_field(
|
||||
let attrs = Attrs::from_args_field(
|
||||
field,
|
||||
parent_attribute.casing(),
|
||||
parent_attribute.env_casing(),
|
||||
|
|
|
@ -30,7 +30,7 @@ pub fn gen_for_struct(
|
|||
) -> TokenStream {
|
||||
let app_name = env::var("CARGO_PKG_NAME").ok().unwrap_or_default();
|
||||
|
||||
let attrs = Attrs::from_struct(
|
||||
let attrs = Attrs::from_args_struct(
|
||||
Span::call_site(),
|
||||
attrs,
|
||||
Name::Assigned(quote!(#app_name)),
|
||||
|
@ -75,7 +75,7 @@ pub fn gen_for_struct(
|
|||
pub fn gen_for_enum(enum_name: &Ident, generics: &Generics, attrs: &[Attribute]) -> TokenStream {
|
||||
let app_name = env::var("CARGO_PKG_NAME").ok().unwrap_or_default();
|
||||
|
||||
let attrs = Attrs::from_struct(
|
||||
let attrs = Attrs::from_subcommand_enum(
|
||||
Span::call_site(),
|
||||
attrs,
|
||||
Name::Assigned(quote!(#app_name)),
|
||||
|
|
|
@ -45,7 +45,7 @@ pub fn gen_for_enum(
|
|||
) -> TokenStream {
|
||||
let from_arg_matches = gen_from_arg_matches_for_enum(enum_name, generics, attrs, e);
|
||||
|
||||
let attrs = Attrs::from_struct(
|
||||
let attrs = Attrs::from_subcommand_enum(
|
||||
Span::call_site(),
|
||||
attrs,
|
||||
Name::Derived(enum_name.clone()),
|
||||
|
@ -94,7 +94,7 @@ fn gen_from_arg_matches_for_enum(
|
|||
attrs: &[Attribute],
|
||||
e: &DataEnum,
|
||||
) -> TokenStream {
|
||||
let attrs = Attrs::from_struct(
|
||||
let attrs = Attrs::from_subcommand_enum(
|
||||
Span::call_site(),
|
||||
attrs,
|
||||
Name::Derived(name.clone()),
|
||||
|
@ -148,7 +148,7 @@ fn gen_augment(
|
|||
let subcommands: Vec<_> = variants
|
||||
.iter()
|
||||
.filter_map(|variant| {
|
||||
let attrs = Attrs::from_variant(
|
||||
let attrs = Attrs::from_subcommand_variant(
|
||||
variant,
|
||||
parent_attribute.casing(),
|
||||
parent_attribute.env_casing(),
|
||||
|
@ -337,7 +337,7 @@ fn gen_has_subcommand(
|
|||
let (flatten_variants, variants): (Vec<_>, Vec<_>) = variants
|
||||
.iter()
|
||||
.filter_map(|variant| {
|
||||
let attrs = Attrs::from_variant(
|
||||
let attrs = Attrs::from_subcommand_variant(
|
||||
variant,
|
||||
parent_attribute.casing(),
|
||||
parent_attribute.env_casing(),
|
||||
|
@ -407,7 +407,7 @@ fn gen_from_arg_matches(
|
|||
let (flatten_variants, variants): (Vec<_>, Vec<_>) = variants
|
||||
.iter()
|
||||
.filter_map(|variant| {
|
||||
let attrs = Attrs::from_variant(
|
||||
let attrs = Attrs::from_subcommand_variant(
|
||||
variant,
|
||||
parent_attribute.casing(),
|
||||
parent_attribute.env_casing(),
|
||||
|
@ -556,7 +556,7 @@ fn gen_update_from_arg_matches(
|
|||
let (flatten, variants): (Vec<_>, Vec<_>) = variants
|
||||
.iter()
|
||||
.filter_map(|variant| {
|
||||
let attrs = Attrs::from_variant(
|
||||
let attrs = Attrs::from_subcommand_variant(
|
||||
variant,
|
||||
parent_attribute.casing(),
|
||||
parent_attribute.env_casing(),
|
||||
|
@ -582,7 +582,7 @@ fn gen_update_from_arg_matches(
|
|||
.named
|
||||
.iter()
|
||||
.map(|field| {
|
||||
let attrs = Attrs::from_field(
|
||||
let attrs = Attrs::from_args_field(
|
||||
field,
|
||||
parent_attribute.casing(),
|
||||
parent_attribute.env_casing(),
|
||||
|
|
|
@ -35,7 +35,7 @@ pub fn derive_value_enum(input: &DeriveInput) -> TokenStream {
|
|||
}
|
||||
|
||||
pub fn gen_for_enum(name: &Ident, attrs: &[Attribute], e: &DataEnum) -> TokenStream {
|
||||
let attrs = Attrs::from_struct(
|
||||
let attrs = Attrs::from_value_enum(
|
||||
Span::call_site(),
|
||||
attrs,
|
||||
Name::Derived(name.clone()),
|
||||
|
|
Loading…
Reference in a new issue