Skip to content

Commit

Permalink
Add Language::ALL and deprecated Language::all()
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenroose committed Mar 28, 2024
1 parent a4c57cb commit ce5e41b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
49 changes: 27 additions & 22 deletions src/language/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,28 +69,33 @@ impl Default for Language {
impl Language {
/// The list of supported languages.
/// Language support is managed by compile features.
pub const ALL: &'static [Language] = &[
Language::English,
#[cfg(feature = "chinese-simplified")]
Language::SimplifiedChinese,
#[cfg(feature = "chinese-traditional")]
Language::TraditionalChinese,
#[cfg(feature = "czech")]
Language::Czech,
#[cfg(feature = "french")]
Language::French,
#[cfg(feature = "italian")]
Language::Italian,
#[cfg(feature = "japanese")]
Language::Japanese,
#[cfg(feature = "korean")]
Language::Korean,
#[cfg(feature = "portuguese")]
Language::Portuguese,
#[cfg(feature = "spanish")]
Language::Spanish,
];

/// The list of supported languages.
/// Language support is managed by compile features.
#[deprecated(since = "2.1.0", note = "use constant Language::ALL instead")]
pub fn all() -> &'static [Language] {
&[
Language::English,
#[cfg(feature = "chinese-simplified")]
Language::SimplifiedChinese,
#[cfg(feature = "chinese-traditional")]
Language::TraditionalChinese,
#[cfg(feature = "czech")]
Language::Czech,
#[cfg(feature = "french")]
Language::French,
#[cfg(feature = "italian")]
Language::Italian,
#[cfg(feature = "japanese")]
Language::Japanese,
#[cfg(feature = "korean")]
Language::Korean,
#[cfg(feature = "portuguese")]
Language::Portuguese,
#[cfg(feature = "spanish")]
Language::Spanish,
]
Language::ALL
}

/// The word list for this language.
Expand Down Expand Up @@ -306,7 +311,7 @@ mod tests {
// Afterwards, we make sure that no word maps to multiple languages
// if either of those is guaranteed to have unique words.
let mut words: HashMap<&str, Vec<Language>> = HashMap::new();
for lang in Language::all().iter() {
for lang in Language::ALL.iter() {
for word in lang.word_list().iter() {
words.entry(word).or_insert(Vec::new()).push(*lang);
}
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,14 @@ pub struct AmbiguousLanguages([bool; language::MAX_NB_LANGUAGES]);

impl AmbiguousLanguages {
/// Presents the possible languages in the form of a slice of booleans
/// that correspond to the occurrences in [Language::all()].
/// that correspond to the occurrences in [Language::ALL].
pub fn as_bools(&self) -> &[bool; language::MAX_NB_LANGUAGES] {
&self.0
}

/// An iterator over the possible languages.
pub fn iter(&self) -> impl Iterator<Item = Language> + '_ {
Language::all().iter().enumerate().filter(move |(i, _)| self.0[*i]).map(|(_, l)| *l)
Language::ALL.iter().enumerate().filter(move |(i, _)| self.0[*i]).map(|(_, l)| *l)
}

/// Returns a vector of the possible languages.
Expand Down Expand Up @@ -365,7 +365,7 @@ impl Mnemonic {
/// See documentation on [Mnemonic::language_of] for more info.
fn language_of_iter<'a, W: Iterator<Item = &'a str>>(words: W) -> Result<Language, Error> {
let mut words = words.peekable();
let langs = Language::all();
let langs = Language::ALL;
{
// Start scope to drop first_word so that words can be reborrowed later.
let first_word = words.peek().ok_or(Error::BadWordCount(0))?;
Expand Down Expand Up @@ -530,8 +530,8 @@ impl Mnemonic {
let mut cow = s.into();
Mnemonic::normalize_utf8_cow(&mut cow);

let language = if Language::all().len() == 1 {
Language::all()[0]
let language = if Language::ALL.len() == 1 {
Language::ALL[0]
} else {
Mnemonic::language_of(cow.as_ref())?
};
Expand Down Expand Up @@ -680,7 +680,7 @@ mod tests {
#[cfg(feature = "rand")]
#[test]
fn test_language_of() {
for lang in Language::all() {
for lang in Language::ALL {
let m = Mnemonic::generate_in(*lang, 24).unwrap();
assert_eq!(*lang, Mnemonic::language_of_iter(m.words()).unwrap());
assert_eq!(
Expand All @@ -698,10 +698,10 @@ mod tests {
let mut present = [false; language::MAX_NB_LANGUAGES];
let mut present_vec = Vec::new();
let mut alternate = true;
for i in 0..Language::all().len() {
for i in 0..Language::ALL.len() {
present[i] = alternate;
if alternate {
present_vec.push(Language::all()[i]);
present_vec.push(Language::ALL[i]);
}
alternate = !alternate;
}
Expand Down

0 comments on commit ce5e41b

Please sign in to comment.