From ac1a9d6d1333ab06ce19da0aa1680d768ad8c214 Mon Sep 17 00:00:00 2001 From: ModProg Date: Mon, 27 Sep 2021 22:06:17 +0200 Subject: [PATCH] address epage's remarks --- clap_derive/src/derives/arg_enum.rs | 16 ++++++++-------- clap_derive/src/derives/args.rs | 2 +- clap_derive/src/dummies.rs | 4 ++-- clap_derive/tests/arg_enum.rs | 4 ++-- clap_derive/tests/ui/arg_enum_on_struct.rs | 2 +- src/build/arg/arg_value.rs | 13 ++++++------- src/derive.rs | 6 +++--- 7 files changed, 23 insertions(+), 24 deletions(-) diff --git a/clap_derive/src/derives/arg_enum.rs b/clap_derive/src/derives/arg_enum.rs index 373b432e..1211e829 100644 --- a/clap_derive/src/derives/arg_enum.rs +++ b/clap_derive/src/derives/arg_enum.rs @@ -47,9 +47,9 @@ pub fn gen_for_enum(name: &Ident, attrs: &[Attribute], e: &DataEnum) -> TokenStr ); let lits = lits(&e.variants, &attrs); - let variants = gen_variants(&lits); + let arg_values = gen_arg_values(&lits); let from_str = gen_from_str(&lits); - let as_arg = gen_as_arg(&lits); + let arg_value = gen_arg_value(&lits); quote! { #[allow(dead_code, unreachable_code, unused_variables)] @@ -65,9 +65,9 @@ pub fn gen_for_enum(name: &Ident, attrs: &[Attribute], e: &DataEnum) -> TokenStr )] #[deny(clippy::correctness)] impl clap::ArgEnum for #name { - #variants + #arg_values #from_str - #as_arg + #arg_value } } } @@ -101,11 +101,11 @@ fn lits( .collect::>() } -fn gen_variants(lits: &[(TokenStream, Ident)]) -> TokenStream { +fn gen_arg_values(lits: &[(TokenStream, Ident)]) -> TokenStream { let lit = lits.iter().map(|l| &l.0).collect::>(); quote! { - fn variants() -> Vec> { + fn arg_values() -> Vec> { vec![#(#lit),*] } } @@ -124,11 +124,11 @@ fn gen_from_str(lits: &[(TokenStream, Ident)]) -> TokenStream { } } -fn gen_as_arg(lits: &[(TokenStream, Ident)]) -> TokenStream { +fn gen_arg_value(lits: &[(TokenStream, Ident)]) -> TokenStream { let (lit, variant): (Vec, Vec) = lits.iter().cloned().unzip(); quote! { - fn as_arg(&self) -> Option> { + fn arg_value(&self) -> Option> { match self { #(Self::#variant => Some(#lit),)* _ => None diff --git a/clap_derive/src/derives/args.rs b/clap_derive/src/derives/args.rs index 3b987cff..0cceb291 100644 --- a/clap_derive/src/derives/args.rs +++ b/clap_derive/src/derives/args.rs @@ -361,7 +361,7 @@ pub fn gen_augment( fn gen_arg_enum_possible_values(ty: &Type) -> TokenStream { quote_spanned! { ty.span()=> - .possible_values(<#ty as clap::ArgEnum>::variants()) + .possible_values(<#ty as clap::ArgEnum>::arg_values()) } } diff --git a/clap_derive/src/dummies.rs b/clap_derive/src/dummies.rs index 3d5905cd..ecc63277 100644 --- a/clap_derive/src/dummies.rs +++ b/clap_derive/src/dummies.rs @@ -76,13 +76,13 @@ pub fn args(name: &Ident) { pub fn arg_enum(name: &Ident) { append_dummy(quote! { impl clap::ArgEnum for #name { - fn variants() -> Vec> { + fn arg_values() -> Vec> { unimplemented!() } fn from_str(_input: &str, _case_insensitive: bool) -> Result { unimplemented!() } - fn as_arg(&self) -> Option> { + fn arg_value(&self) -> Option> { unimplemented!() } } diff --git a/clap_derive/tests/arg_enum.rs b/clap_derive/tests/arg_enum.rs index 8642b545..84c816cd 100644 --- a/clap_derive/tests/arg_enum.rs +++ b/clap_derive/tests/arg_enum.rs @@ -54,7 +54,7 @@ fn default_value() { impl std::fmt::Display for ArgChoice { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> Result<(), std::fmt::Error> { - std::fmt::Display::fmt(self.as_arg().unwrap().get_name(), f) + std::fmt::Display::fmt(self.arg_value().unwrap().get_name(), f) } } @@ -368,7 +368,7 @@ fn skip_variant() { } assert_eq!( - ArgChoice::variants(), + ArgChoice::arg_values(), vec![ArgValue::new("foo"), ArgValue::new("bar")] ); assert!(ArgChoice::from_str("foo", true).is_ok()); diff --git a/clap_derive/tests/ui/arg_enum_on_struct.rs b/clap_derive/tests/ui/arg_enum_on_struct.rs index cb41f72b..27f9a950 100644 --- a/clap_derive/tests/ui/arg_enum_on_struct.rs +++ b/clap_derive/tests/ui/arg_enum_on_struct.rs @@ -4,5 +4,5 @@ use clap::ArgEnum; struct Opt {} fn main() { - println!("{:?}", Opt::VARIANTS); + println!("{:?}", Opt::arg_values()); } diff --git a/src/build/arg/arg_value.rs b/src/build/arg/arg_value.rs index 35579213..6b7d45e3 100644 --- a/src/build/arg/arg_value.rs +++ b/src/build/arg/arg_value.rs @@ -1,3 +1,5 @@ +use std::iter; + /// The representation of a possible value of an argument. /// /// This is used for specifying [possible values] of [Args]. @@ -20,7 +22,7 @@ /// [possible values]: crate::Arg::possible_value() /// [hide]: ArgValue::hidden() /// [about]: ArgValue::about() -#[derive(Debug, Default, Clone, PartialEq)] +#[derive(Debug, Default, Clone, PartialEq, Eq)] pub struct ArgValue<'help> { pub(crate) name: &'help str, pub(crate) about: Option<&'help str>, @@ -64,20 +66,17 @@ impl<'help> ArgValue<'help> { } /// TODO - pub fn get_name_and_aliases(&self) -> Vec<&str> { - [self.name].iter().chain(&self.aliases).copied().collect() + pub fn get_name_and_aliases(&self) -> impl Iterator { + iter::once(&self.name).chain(&self.aliases).copied() } /// TODO pub fn matches(&self, value: &str, ignore_case: bool) -> bool { if ignore_case { self.get_name_and_aliases() - .iter() .any(|name| name.eq_ignore_ascii_case(value)) } else { - self.get_name_and_aliases() - .iter() - .any(|name| name == &value) + self.get_name_and_aliases().any(|name| name == value) } } } diff --git a/src/derive.rs b/src/derive.rs index e1f3e3dc..3edb39c3 100644 --- a/src/derive.rs +++ b/src/derive.rs @@ -282,8 +282,8 @@ pub trait Subcommand: FromArgMatches + Sized { /// } /// ``` pub trait ArgEnum: Sized { - /// All possible argument choices, in display order. - fn variants() -> Vec>; + /// All possible argument values, in display order. + fn arg_values() -> Vec>; /// Parse an argument into `Self`. fn from_str(input: &str, case_insensitive: bool) -> Result; @@ -291,7 +291,7 @@ pub trait ArgEnum: Sized { /// The canonical argument value. /// /// The value is `None` for skipped variants. - fn as_arg(&self) -> Option>; + fn arg_value(&self) -> Option>; } impl Clap for Box {