Skip to content

Commit

Permalink
Merge pull request #3591 from Shir0kamii/fix-ArgEnum-non-unit
Browse files Browse the repository at this point in the history
Fix ArgEnum non-unit variant
  • Loading branch information
epage authored Mar 31, 2022
2 parents 06f855f + fb4755d commit 71ef887
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 7 deletions.
13 changes: 6 additions & 7 deletions clap_derive/src/derives/arg_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use crate::{
};

use proc_macro2::{Span, TokenStream};
use proc_macro_error::abort_call_site;
use proc_macro_error::{abort, abort_call_site};
use quote::quote;
use syn::{
punctuated::Punctuated, token::Comma, Attribute, Data, DataEnum, DeriveInput, Fields, Ident,
Variant,
punctuated::Punctuated, spanned::Spanned, token::Comma, Attribute, Data, DataEnum, DeriveInput,
Fields, Ident, Variant,
};

pub fn derive_arg_enum(input: &DeriveInput) -> TokenStream {
Expand All @@ -34,10 +34,6 @@ pub fn derive_arg_enum(input: &DeriveInput) -> TokenStream {
}

pub fn gen_for_enum(name: &Ident, attrs: &[Attribute], e: &DataEnum) -> TokenStream {
if !e.variants.iter().all(|v| matches!(v.fields, Fields::Unit)) {
return quote!();
};

let attrs = Attrs::from_struct(
Span::call_site(),
attrs,
Expand Down Expand Up @@ -86,6 +82,9 @@ fn lits(
if let Kind::Skip(_) = &*attrs.kind() {
None
} else {
if !matches!(variant.fields, Fields::Unit) {
abort!(variant.span(), "`#[derive(ArgEnum)]` only supports non-unit variants, unless they are skipped");
}
let fields = attrs.field_methods(false);
let name = attrs.cased_name();
Some((
Expand Down
1 change: 1 addition & 0 deletions examples/derive_ref/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ fn main() {
- `Subcommand` defines available subcommands.
- Subcommand arguments can be defined in a struct-variant or automatically flattened with a tuple-variant.
- `ArgEnum` allows parsing a value directly into an `enum`, erroring on unsupported values.
- The derive doesn't work on enums that contain non-unit variants, unless they are skipped

See also the [tutorial](../tutorial_derive/README.md) and [examples](../README.md).

Expand Down
31 changes: 31 additions & 0 deletions tests/derive/arg_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,37 @@ fn skip_variant() {
}
}

#[test]
fn skip_non_unit_variant() {
#[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
#[allow(dead_code)] // silence warning about `Baz` being unused
enum ArgChoice {
Foo,
Bar,
#[clap(skip)]
Baz(usize),
}

assert_eq!(
<ArgChoice as clap::ArgEnum>::value_variants()
.iter()
.map(clap::ArgEnum::to_possible_value)
.map(Option::unwrap)
.collect::<Vec<_>>(),
vec![
clap::PossibleValue::new("foo"),
clap::PossibleValue::new("bar")
]
);

{
use clap::ArgEnum;
assert!(ArgChoice::from_str("foo", true).is_ok());
assert!(ArgChoice::from_str("bar", true).is_ok());
assert!(ArgChoice::from_str("baz", true).is_err());
}
}

#[test]
fn from_str_invalid() {
#[derive(clap::ArgEnum, PartialEq, Debug, Clone)]
Expand Down
10 changes: 10 additions & 0 deletions tests/derive_ui/arg_enum_non_unit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use clap::ArgEnum;

#[derive(ArgEnum, Clone, Debug)]
enum Opt {
Foo(usize),
}

fn main() {
println!("{:?}", Opt::Foo(42));
}
5 changes: 5 additions & 0 deletions tests/derive_ui/arg_enum_non_unit.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
error: `#[derive(ArgEnum)]` only supports non-unit variants, unless they are skipped
--> tests/derive_ui/arg_enum_non_unit.rs:5:5
|
5 | Foo(usize),
| ^^^

0 comments on commit 71ef887

Please sign in to comment.