Adjust derive

This commit is contained in:
CreepySkeleton 2020-08-05 15:14:22 +03:00
parent e9759a241b
commit e6cc49ecde
4 changed files with 12 additions and 29 deletions

View file

@ -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<Field, Comma>, 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
}
}

View file

@ -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<Self>
{
match (name, sub) {
fn from_subcommand<'b>(subcommand: Option<(&'b str, &'b ::clap::ArgMatches)>) -> Option<Self> {
match subcommand {
#( #match_arms, )*
other => {
#( #child_subcommands )else*

View file

@ -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<Self> {
fn from_subcommand<'b>(_sub: Option<(&'b str, &'b ::clap::ArgMatches)>) -> Option<Self> {
unimplemented!()
}
fn augment_subcommands(_app: ::clap::App<'_>) -> ::clap::App<'_> {

View file

@ -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<Self>;
fn from_subcommand(subcommand: Option<(&str, &ArgMatches)>) -> Option<Self>;
/// @TODO @release @docs
fn augment_subcommands(app: App<'_>) -> App<'_>;
}
@ -118,8 +118,8 @@ impl<T: FromArgMatches> FromArgMatches for Box<T> {
}
impl<T: Subcommand> Subcommand for Box<T> {
fn from_subcommand(name: &str, matches: Option<&ArgMatches>) -> Option<Self> {
<T as Subcommand>::from_subcommand(name, matches).map(Box::new)
fn from_subcommand(subcommand: Option<(&str, &ArgMatches)>) -> Option<Self> {
<T as Subcommand>::from_subcommand(subcommand).map(Box::new)
}
fn augment_subcommands(app: App<'_>) -> App<'_> {
<T as Subcommand>::augment_subcommands(app)