Skip to content

Commit

Permalink
Merge pull request #3825 from epage/refactor
Browse files Browse the repository at this point in the history
fix(derive): Tweak spans
  • Loading branch information
epage committed Jun 13, 2022
2 parents 650a29c + 9910b14 commit a96e7cf
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 26 deletions.
13 changes: 10 additions & 3 deletions clap_derive/src/attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ impl Attrs {
res
}

pub fn from_arg_enum_variant(
pub fn from_value_enum_variant(
variant: &Variant,
argument_casing: Sp<CasingStyle>,
env_casing: Sp<CasingStyle>,
Expand Down Expand Up @@ -362,7 +362,7 @@ impl Attrs {
let ty = Ty::from_syn_ty(&field.ty);
res.kind = Sp::new(Kind::FromGlobal(ty), orig_ty.span());
}
Kind::Arg(orig_ty) => {
Kind::Arg(_) => {
let mut ty = Ty::from_syn_ty(&field.ty);
if res.parser.is_some() {
if let Some(value_parser) = res.value_parser.as_ref() {
Expand Down Expand Up @@ -408,7 +408,14 @@ impl Attrs {

_ => (),
}
res.kind = Sp::new(Kind::Arg(ty), orig_ty.span());
res.kind = Sp::new(
Kind::Arg(ty),
field
.ident
.as_ref()
.map(|i| i.span())
.unwrap_or_else(|| field.ty.span()),
);
}
}

Expand Down
4 changes: 2 additions & 2 deletions clap_derive/src/derives/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ pub fn gen_augment(

let value_name = attrs.value_name();
let possible_values = if attrs.is_enum() && !attrs.ignore_parser() {
gen_arg_enum_possible_values(convert_type)
gen_value_enum_possible_values(convert_type)
} else {
quote!()
};
Expand Down Expand Up @@ -424,7 +424,7 @@ pub fn gen_augment(
}}
}

fn gen_arg_enum_possible_values(ty: &Type) -> TokenStream {
fn gen_value_enum_possible_values(ty: &Type) -> TokenStream {
quote_spanned! { ty.span()=>
.possible_values(<#ty as clap::ValueEnum>::value_variants().iter().filter_map(clap::ValueEnum::to_possible_value))
}
Expand Down
4 changes: 2 additions & 2 deletions clap_derive/src/derives/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
// This work was derived from Structopt (https://github.com/TeXitoi/structopt)
// commit#ea76fa1b1b273e65e3b0b1046643715b49bec51f which is licensed under the
// MIT/Apache 2.0 license.
mod arg_enum;
mod args;
mod into_app;
mod parser;
mod subcommand;
mod value_enum;

pub use self::parser::derive_parser;
pub use arg_enum::derive_arg_enum;
pub use args::derive_args;
pub use subcommand::derive_subcommand;
pub use value_enum::derive_value_enum;
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,16 @@ use crate::{
use proc_macro2::{Span, TokenStream};
use proc_macro_error::{abort, abort_call_site};
use quote::quote;
use quote::quote_spanned;
use syn::{
punctuated::Punctuated, spanned::Spanned, token::Comma, Attribute, Data, DataEnum, DeriveInput,
Fields, Ident, Variant,
};

pub fn derive_arg_enum(input: &DeriveInput) -> TokenStream {
pub fn derive_value_enum(input: &DeriveInput) -> TokenStream {
let ident = &input.ident;

dummies::arg_enum(ident);
dummies::value_enum(ident);

match input.data {
Data::Enum(ref e) => gen_for_enum(ident, &input.attrs, e),
Expand Down Expand Up @@ -74,7 +75,7 @@ fn lits(
variants
.iter()
.filter_map(|variant| {
let attrs = Attrs::from_arg_enum_variant(
let attrs = Attrs::from_value_enum_variant(
variant,
parent_attribute.casing(),
parent_attribute.env_casing(),
Expand All @@ -88,7 +89,7 @@ fn lits(
let fields = attrs.field_methods(false);
let name = attrs.cased_name();
Some((
quote! {
quote_spanned! { variant.span()=>
clap::PossibleValue::new(#name)
#fields
},
Expand Down
2 changes: 1 addition & 1 deletion clap_derive/src/dummies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn args(name: &Ident) {
});
}

pub fn arg_enum(name: &Ident) {
pub fn value_enum(name: &Ident) {
append_dummy(quote! {
impl clap::ValueEnum for #name {
fn value_variants<'a>() -> &'a [Self]{
Expand Down
4 changes: 2 additions & 2 deletions clap_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,15 @@ mod utils;
#[proc_macro_error]
pub fn value_enum(input: TokenStream) -> TokenStream {
let input: DeriveInput = parse_macro_input!(input);
derives::derive_arg_enum(&input).into()
derives::derive_value_enum(&input).into()
}

/// Generates the `ValueEnum` impl.
#[proc_macro_derive(ArgEnum, attributes(clap))]
#[proc_macro_error]
pub fn arg_enum(input: TokenStream) -> TokenStream {
let input: DeriveInput = parse_macro_input!(input);
derives::derive_arg_enum(&input).into()
derives::derive_value_enum(&input).into()
}

/// Generates the `Parser` implementation.
Expand Down
12 changes: 6 additions & 6 deletions tests/derive_ui/next/bool_value_enum.stderr
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
warning: use of deprecated variant `clap::ArgAction::IncOccurrence`: Replaced with `ArgAction::SetTrue` or `ArgAction::Count`
--> tests/derive_ui/next/bool_value_enum.rs:6:5
--> tests/derive_ui/next/bool_value_enum.rs:7:5
|
6 | #[clap(short, value_enum)]
| ^
7 | opts: bool,
| ^^^^
|
= note: `#[warn(deprecated)]` on by default

warning: use of deprecated variant `clap::ArgAction::IncOccurrence`: Replaced with `ArgAction::SetTrue` or `ArgAction::Count`
--> tests/derive_ui/next/bool_value_enum.rs:6:5
--> tests/derive_ui/next/bool_value_enum.rs:7:5
|
6 | #[clap(short, value_enum)]
| ^
7 | opts: bool,
| ^^^^

error[E0277]: the trait bound `bool: ArgEnum` is not satisfied
--> tests/derive_ui/next/bool_value_enum.rs:7:11
Expand Down
12 changes: 6 additions & 6 deletions tests/derive_ui/stable/bool_value_enum.stderr
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
warning: use of deprecated variant `clap::ArgAction::IncOccurrence`: Replaced with `ArgAction::SetTrue` or `ArgAction::Count`
--> tests/derive_ui/stable/bool_value_enum.rs:6:5
--> tests/derive_ui/stable/bool_value_enum.rs:7:5
|
6 | #[clap(short, value_enum)]
| ^
7 | opts: bool,
| ^^^^
|
= note: `#[warn(deprecated)]` on by default

warning: use of deprecated variant `clap::ArgAction::IncOccurrence`: Replaced with `ArgAction::SetTrue` or `ArgAction::Count`
--> tests/derive_ui/stable/bool_value_enum.rs:6:5
--> tests/derive_ui/stable/bool_value_enum.rs:7:5
|
6 | #[clap(short, value_enum)]
| ^
7 | opts: bool,
| ^^^^

error[E0277]: the trait bound `bool: ValueEnum` is not satisfied
--> tests/derive_ui/stable/bool_value_enum.rs:7:11
Expand Down

0 comments on commit a96e7cf

Please sign in to comment.