From e6cc49ecdefdfe5d3a12bf68e1ac9b08253630ae Mon Sep 17 00:00:00 2001 From: CreepySkeleton Date: Wed, 5 Aug 2020 15:14:22 +0300 Subject: [PATCH] Adjust derive --- clap_derive/src/derives/from_arg_matches.rs | 10 ++------- clap_derive/src/derives/subcommand.rs | 23 ++++++--------------- clap_derive/src/dummies.rs | 2 +- src/derive.rs | 6 +++--- 4 files changed, 12 insertions(+), 29 deletions(-) diff --git a/clap_derive/src/derives/from_arg_matches.rs b/clap_derive/src/derives/from_arg_matches.rs index 195d2fe4..d6348ca4 100644 --- a/clap_derive/src/derives/from_arg_matches.rs +++ b/clap_derive/src/derives/from_arg_matches.rs @@ -65,9 +65,7 @@ pub fn gen_for_enum(name: &Ident) -> TokenStream { #[deny(clippy::correctness)] impl ::clap::FromArgMatches for #name { fn from_arg_matches(matches: &::clap::ArgMatches) -> Self { - let (name, subcmd) = matches.subcommand(); - <#name as ::clap::Subcommand>::from_subcommand(name, subcmd) - .unwrap() + <#name as ::clap::Subcommand>::from_subcommand(matches.subcommand()).unwrap() } } } @@ -107,11 +105,7 @@ pub fn gen_constructor(fields: &Punctuated, parent_attribute: &Att }; quote_spanned! { kind.span()=> #field_name: { - let (name, subcmd) = matches.subcommand(); - <#subcmd_type as ::clap::Subcommand>::from_subcommand( - name, - subcmd - ) + <#subcmd_type as ::clap::Subcommand>::from_subcommand(matches.subcommand()) #unwrapper } } diff --git a/clap_derive/src/derives/subcommand.rs b/clap_derive/src/derives/subcommand.rs index ba4192b2..0a831b74 100644 --- a/clap_derive/src/derives/subcommand.rs +++ b/clap_derive/src/derives/subcommand.rs @@ -238,7 +238,7 @@ fn gen_from_subcommand( }; quote! { - (#sub_name, Some(matches)) => { + Some((#sub_name, matches)) => { Some(#name :: #variant_name #constructor_block) } } @@ -249,7 +249,7 @@ fn gen_from_subcommand( Unnamed(ref fields) if fields.unnamed.len() == 1 => { let ty = &fields.unnamed[0]; quote! { - if let Some(res) = <#ty as ::clap::Subcommand>::from_subcommand(other.0, other.1) { + if let Some(res) = <#ty as ::clap::Subcommand>::from_subcommand(other) { return Some(#name :: #variant_name (res)); } } @@ -263,9 +263,9 @@ fn gen_from_subcommand( let wildcard = match ext_subcmd { Some((span, var_name, str_ty, values_of)) => quote_spanned! { span=> - ("", ::std::option::Option::None) => ::std::option::Option::None, + None => ::std::option::Option::None, - (external, Some(matches)) => { + Some((external, matches)) => { ::std::option::Option::Some(#name::#var_name( ::std::iter::once(#str_ty::from(external)) .chain( @@ -274,25 +274,14 @@ fn gen_from_subcommand( .collect::<::std::vec::Vec<_>>() )) } - - (external, None) => { - ::std::option::Option::Some(#name::#var_name({ - let mut v = ::std::vec::Vec::with_capacity(1); - v.push(#str_ty::from(external)); - v - })) - } }, None => quote!(_ => None), }; quote! { - fn from_subcommand<'b>( - name: &'b str, - sub: Option<&'b ::clap::ArgMatches>) -> Option - { - match (name, sub) { + fn from_subcommand<'b>(subcommand: Option<(&'b str, &'b ::clap::ArgMatches)>) -> Option { + match subcommand { #( #match_arms, )* other => { #( #child_subcommands )else* diff --git a/clap_derive/src/dummies.rs b/clap_derive/src/dummies.rs index ef00f663..58e1b2fb 100644 --- a/clap_derive/src/dummies.rs +++ b/clap_derive/src/dummies.rs @@ -44,7 +44,7 @@ pub fn from_arg_matches(name: &Ident) { pub fn subcommand(name: &Ident) { append_dummy(quote! { impl ::clap::Subcommand for #name { - fn from_subcommand(_name: &str, _matches: Option<&::clap::ArgMatches>) -> Option { + fn from_subcommand<'b>(_sub: Option<(&'b str, &'b ::clap::ArgMatches)>) -> Option { unimplemented!() } fn augment_subcommands(_app: ::clap::App<'_>) -> ::clap::App<'_> { diff --git a/src/derive.rs b/src/derive.rs index bbe8fdae..f30c7af0 100644 --- a/src/derive.rs +++ b/src/derive.rs @@ -60,7 +60,7 @@ pub trait FromArgMatches: Sized { /// @TODO @release @docs pub trait Subcommand: Sized { /// @TODO @release @docs - fn from_subcommand(name: &str, matches: Option<&ArgMatches>) -> Option; + fn from_subcommand(subcommand: Option<(&str, &ArgMatches)>) -> Option; /// @TODO @release @docs fn augment_subcommands(app: App<'_>) -> App<'_>; } @@ -118,8 +118,8 @@ impl FromArgMatches for Box { } impl Subcommand for Box { - fn from_subcommand(name: &str, matches: Option<&ArgMatches>) -> Option { - ::from_subcommand(name, matches).map(Box::new) + fn from_subcommand(subcommand: Option<(&str, &ArgMatches)>) -> Option { + ::from_subcommand(subcommand).map(Box::new) } fn augment_subcommands(app: App<'_>) -> App<'_> { ::augment_subcommands(app)