Skip to content

Commit

Permalink
[clap-v3-utils] Add try_get_language and deprecate `acquire_languag…
Browse files Browse the repository at this point in the history
  • Loading branch information
samkim-crypto authored Mar 26, 2024
1 parent bcf3d80 commit 01314a4
Showing 1 changed file with 29 additions and 10 deletions.
39 changes: 29 additions & 10 deletions clap-v3-utils/src/keygen/mnemonic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,21 @@ pub fn try_get_word_count(matches: &ArgMatches) -> Result<Option<usize>, Box<dyn
}))
}

// The constant `POSSIBLE_LANGUAGES` and function `try_get_language` must always be updated in sync
const POSSIBLE_LANGUAGES: &[&str] = &[
"english",
"chinese-simplified",
"chinese-traditional",
"japanese",
"spanish",
"korean",
"french",
"italian",
];
pub fn language_arg<'a>() -> 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)
Expand All @@ -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::<String>(LANGUAGE_ARG.name)
Expand All @@ -95,6 +98,22 @@ pub fn acquire_language(matches: &ArgMatches) -> Language {
}
}

pub fn try_get_language(matches: &ArgMatches) -> Result<Option<Language>, Box<dyn error::Error>> {
Ok(matches
.try_get_one::<String>(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())
}
Expand Down

0 comments on commit 01314a4

Please sign in to comment.