From f6a3608981a8a4d60b7ae24905516c9f250a9122 Mon Sep 17 00:00:00 2001 From: samkim-crypto Date: Tue, 26 Mar 2024 13:25:57 +0900 Subject: [PATCH] [clap-v3-utils] Add `try_get_word_count` (#411) --- clap-v3-utils/src/keygen/mnemonic.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/clap-v3-utils/src/keygen/mnemonic.rs b/clap-v3-utils/src/keygen/mnemonic.rs index 0bcc20a58b..2dc32d6ca5 100644 --- a/clap-v3-utils/src/keygen/mnemonic.rs +++ b/clap-v3-utils/src/keygen/mnemonic.rs @@ -25,16 +25,32 @@ pub const NO_PASSPHRASE_ARG: ArgConstant<'static> = ArgConstant { help: "Do not prompt for a BIP39 passphrase", }; +// The constant `POSSIBLE_WORD_COUNTS` and function `try_get_word_count` must always be updated in +// sync +const POSSIBLE_WORD_COUNTS: &[&str] = &["12", "15", "18", "21", "24"]; pub fn word_count_arg<'a>() -> Arg<'a> { Arg::new(WORD_COUNT_ARG.name) .long(WORD_COUNT_ARG.long) - .value_parser(PossibleValuesParser::new(["12", "15", "18", "21", "24"])) + .value_parser(PossibleValuesParser::new(POSSIBLE_WORD_COUNTS)) .default_value("12") .value_name("NUMBER") .takes_value(true) .help(WORD_COUNT_ARG.help) } +pub fn try_get_word_count(matches: &ArgMatches) -> Result, Box> { + Ok(matches + .try_get_one::(WORD_COUNT_ARG.name)? + .map(|count| match count.as_str() { + "12" => 12, + "15" => 15, + "18" => 18, + "21" => 21, + "24" => 24, + _ => unreachable!(), + })) +} + pub fn language_arg<'a>() -> Arg<'a> { Arg::new(LANGUAGE_ARG.name) .long(LANGUAGE_ARG.long)