refactor(derive): Move from_arg_matches into coupled derives

This commit is contained in:
Ed Page 2021-07-14 15:51:15 -05:00
parent 894be6799c
commit 58dd1d5c5a
4 changed files with 33 additions and 33 deletions

View file

@ -21,7 +21,7 @@ use crate::{
utils::{sub_type, subty_if_name, Sp, Ty},
};
pub fn gen_for_struct(
pub fn gen_from_arg_matches_for_struct(
struct_name: &Ident,
fields: &Punctuated<Field, Comma>,
parent_attribute: &Attrs,
@ -54,31 +54,6 @@ pub fn gen_for_struct(
}
}
pub fn gen_for_enum(name: &Ident) -> TokenStream {
quote! {
#[allow(dead_code, unreachable_code, unused_variables)]
#[allow(
clippy::style,
clippy::complexity,
clippy::pedantic,
clippy::restriction,
clippy::perf,
clippy::deprecated,
clippy::nursery,
clippy::cargo
)]
#[deny(clippy::correctness)]
impl clap::FromArgMatches for #name {
fn from_arg_matches(arg_matches: &clap::ArgMatches) -> Self {
<#name as clap::Subcommand>::from_subcommand(arg_matches.subcommand()).unwrap()
}
fn update_from_arg_matches(&mut self, arg_matches: &clap::ArgMatches) {
<#name as clap::Subcommand>::update_from_subcommand(self, arg_matches.subcommand());
}
}
}
}
fn gen_arg_enum_parse(ty: &Type, attrs: &Attrs) -> TokenStream {
let ci = attrs.case_insensitive();

View file

@ -13,7 +13,7 @@
// MIT/Apache 2.0 license.
use crate::{
derives::{from_arg_matches, into_app, subcommand},
derives::{args, into_app, subcommand},
dummies,
};
@ -57,7 +57,7 @@ fn gen_for_struct(
attrs: &[Attribute],
) -> TokenStream {
let (into_app, attrs) = into_app::gen_for_struct(name, fields, attrs);
let from_arg_matches = from_arg_matches::gen_for_struct(name, fields, &attrs);
let from_arg_matches = args::gen_from_arg_matches_for_struct(name, fields, &attrs);
quote! {
impl clap::Clap for #name {}
@ -69,7 +69,7 @@ fn gen_for_struct(
fn gen_for_enum(name: &Ident, attrs: &[Attribute], e: &DataEnum) -> TokenStream {
let into_app = into_app::gen_for_enum(name, attrs);
let from_arg_matches = from_arg_matches::gen_for_enum(name);
let from_arg_matches = subcommand::gen_from_arg_matches_for_enum(name);
let subcommand = subcommand::gen_for_enum(name, attrs, e);
quote! {

View file

@ -12,8 +12,8 @@
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
// MIT/Apache 2.0 license.
mod arg_enum;
mod args;
mod clap;
mod from_arg_matches;
mod into_app;
mod subcommand;

View file

@ -1,6 +1,6 @@
use crate::{
attrs::{Attrs, Kind, Name, DEFAULT_CASING, DEFAULT_ENV_CASING},
derives::{from_arg_matches, into_app},
derives::{args, into_app},
dummies,
utils::{is_simple_ty, subty_if_name, Sp},
};
@ -61,6 +61,31 @@ pub fn gen_for_enum(name: &Ident, attrs: &[Attribute], e: &DataEnum) -> TokenStr
}
}
pub fn gen_from_arg_matches_for_enum(name: &Ident) -> TokenStream {
quote! {
#[allow(dead_code, unreachable_code, unused_variables)]
#[allow(
clippy::style,
clippy::complexity,
clippy::pedantic,
clippy::restriction,
clippy::perf,
clippy::deprecated,
clippy::nursery,
clippy::cargo
)]
#[deny(clippy::correctness)]
impl clap::FromArgMatches for #name {
fn from_arg_matches(arg_matches: &clap::ArgMatches) -> Self {
<#name as clap::Subcommand>::from_subcommand(arg_matches.subcommand()).unwrap()
}
fn update_from_arg_matches(&mut self, arg_matches: &clap::ArgMatches) {
<#name as clap::Subcommand>::update_from_subcommand(self, arg_matches.subcommand());
}
}
}
}
fn gen_augment(
fn_name: &str,
variants: &Punctuated<Variant, Token![,]>,
@ -236,7 +261,7 @@ fn gen_from_subcommand(
let sub_name = attrs.cased_name();
let variant_name = &variant.ident;
let constructor_block = match variant.fields {
Named(ref fields) => from_arg_matches::gen_constructor(&fields.named, attrs),
Named(ref fields) => args::gen_constructor(&fields.named, attrs),
Unit => quote!(),
Unnamed(ref fields) if fields.unnamed.len() == 1 => {
let ty = &fields.unnamed[0];
@ -349,7 +374,7 @@ fn gen_update_from_subcommand(
let field_name = field.ident.as_ref().unwrap();
(
quote!( ref mut #field_name ),
from_arg_matches::gen_updater(&fields.named, &attrs, false),
args::gen_updater(&fields.named, &attrs, false),
)
})
.unzip();