From 01314a4fc339e94f97468c0682c53ccc613255a6 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Wed, 27 Mar 2024 02:51:54 +0900 Subject: [PATCH] [clap-v3-utils] Add `try_get_language` and deprecate `acquire_language` (#430) --- clap-v3-utils/src/keygen/mnemonic.rs | 39 +++++++++++++++++++++------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/clap-v3-utils/src/keygen/mnemonic.rs b/clap-v3-utils/src/keygen/mnemonic.rs index 2dc32d6ca514d7..f5eb032dd1cd64 100644 --- a/clap-v3-utils/src/keygen/mnemonic.rs +++ b/clap-v3-utils/src/keygen/mnemonic.rs @@ -51,19 +51,21 @@ pub fn try_get_word_count(matches: &ArgMatches) -> Result, Box() -> Arg<'a> { Arg::new(LANGUAGE_ARG.name) .long(LANGUAGE_ARG.long) - .value_parser(PossibleValuesParser::new([ - "english", - "chinese-simplified", - "chinese-traditional", - "japanese", - "spanish", - "korean", - "french", - "italian", - ])) + .value_parser(PossibleValuesParser::new(POSSIBLE_LANGUAGES)) .default_value("english") .value_name("LANGUAGE") .takes_value(true) @@ -77,6 +79,7 @@ pub fn no_passphrase_arg<'a>() -> Arg<'a> { .help(NO_PASSPHRASE_ARG.help) } +#[deprecated(since = "2.0.0", note = "Please use `try_get_language` instead")] pub fn acquire_language(matches: &ArgMatches) -> Language { match matches .get_one::(LANGUAGE_ARG.name) @@ -95,6 +98,22 @@ pub fn acquire_language(matches: &ArgMatches) -> Language { } } +pub fn try_get_language(matches: &ArgMatches) -> Result, Box> { + Ok(matches + .try_get_one::(LANGUAGE_ARG.name)? + .map(|language| match language.as_str() { + "english" => Language::English, + "chinese-simplified" => Language::ChineseSimplified, + "chinese-traditional" => Language::ChineseTraditional, + "japanese" => Language::Japanese, + "spanish" => Language::Spanish, + "korean" => Language::Korean, + "french" => Language::French, + "italian" => Language::Italian, + _ => unreachable!(), + })) +} + pub fn no_passphrase_and_message() -> (String, String) { (NO_PASSPHRASE.to_string(), "".to_string()) }