coins_bip39/wordlist/
korean.rs

1use crate::{Wordlist, WordlistError};
2use once_cell::sync::Lazy;
3
4/// The list of words as supported in the Korean language.
5pub const RAW_KOREAN: &str = include_str!("./words/korean.txt");
6
7/// Korean word list, split into words
8pub static PARSED: Lazy<Vec<&'static str>> = Lazy::new(|| RAW_KOREAN.lines().collect());
9
10#[derive(Clone, Debug, PartialEq, Eq, Copy)]
11/// The Korean wordlist that implements the Wordlist trait.
12pub struct Korean;
13
14impl Wordlist for Korean {
15    fn get_all() -> &'static [&'static str] {
16        PARSED.as_slice()
17    }
18    /// Returns the index of a given word from the word list.
19    fn get_index(word: &str) -> Result<usize, WordlistError> {
20        Self::get_all()
21            .binary_search(&word)
22            .map_err(|_| crate::WordlistError::InvalidWord(word.to_string()))
23    }
24}
25
26#[cfg(test)]
27mod tests {
28    use super::*;
29
30    use crate::WordlistError;
31
32    #[test]
33    fn test_get() {
34        assert_eq!(Korean::get(3), Ok("가능"));
35        assert_eq!(Korean::get(2044), Ok("희망"));
36        assert_eq!(Korean::get(2048), Err(WordlistError::InvalidIndex(2048)));
37    }
38
39    #[test]
40    fn test_get_index() {
41        assert_eq!(Korean::get_index("가능"), Ok(3));
42        assert_eq!(Korean::get_index("희망"), Ok(2044));
43        assert_eq!(
44            Korean::get_index("임의의단어"),
45            Err(WordlistError::InvalidWord("임의의단어".to_string()))
46        );
47    }
48
49    #[test]
50    fn test_get_all() {
51        assert_eq!(Korean::get_all().len(), 2048);
52    }
53}