Derived ArgEnum::from_str returns Err instead of panicking

ArgEnum is public so it is possible for users to call it with
a non-variant input string.
This commit is contained in:
Eric Langlois 2021-06-08 13:52:09 -04:00
parent e3bfa50e8f
commit c20acaf2d4
2 changed files with 12 additions and 2 deletions

View file

@ -118,7 +118,7 @@ fn gen_from_str(lits: &[(TokenStream, Ident)]) -> TokenStream {
match input {
#(val if func(val, #lit) => Ok(Self::#variant),)*
e => unreachable!("The impossible variant have been spotted: {}", e),
e => Err(format!("Invalid variant: {}", e)),
}
}
}

View file

@ -7,7 +7,7 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use clap::Clap;
use clap::{ArgEnum, Clap};
#[test]
fn basic() {
@ -309,3 +309,13 @@ fn vector() {
);
assert!(Opt::try_parse_from(&["", "-a", "fOo"]).is_err());
}
#[test]
fn from_str_invalid() {
#[derive(Clap, PartialEq, Debug)]
enum ArgChoice {
Foo,
}
assert!(ArgChoice::from_str("bar", true).is_err());
}