1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
use failure::Error;
use rustc_hash::FxHashMap;
use crate::error::ErrorKind;
use crate::util::{Bits, Bits11};
pub struct WordMap {
inner: FxHashMap<&'static str, Bits11>,
}
pub struct WordList {
inner: Vec<&'static str>,
}
impl WordMap {
pub fn get_bits(&self, word: &str) -> Result<Bits11, Error> {
match self.inner.get(word) {
Some(n) => Ok(*n),
None => Err(ErrorKind::InvalidWord)?,
}
}
}
impl WordList {
pub fn get_word(&self, bits: Bits11) -> &'static str {
self.inner[bits.bits() as usize]
}
}
mod lazy {
use super::{Bits11, WordList, WordMap};
use once_cell::sync::Lazy;
fn gen_wordlist(lang_words: &'static str) -> WordList {
let inner: Vec<_> = lang_words.split_whitespace().collect();
debug_assert!(inner.len() == 2048, "Invalid wordlist length");
WordList { inner }
}
fn gen_wordmap(wordlist: &WordList) -> WordMap {
let inner = wordlist
.inner
.iter()
.enumerate()
.map(|(i, item)| (*item, Bits11::from(i as u16)))
.collect();
WordMap { inner }
}
pub static WORDLIST_ENGLISH: Lazy<WordList> =
Lazy::new(|| gen_wordlist(include_str!("langs/english.txt")));
#[cfg(feature = "chinese-simplified")]
pub static WORDLIST_CHINESE_SIMPLIFIED: Lazy<WordList> =
Lazy::new(|| gen_wordlist(include_str!("langs/chinese_simplified.txt")));
#[cfg(feature = "chinese-traditional")]
pub static WORDLIST_CHINESE_TRADITIONAL: Lazy<WordList> =
Lazy::new(|| gen_wordlist(include_str!("langs/chinese_traditional.txt")));
#[cfg(feature = "french")]
pub static WORDLIST_FRENCH: Lazy<WordList> =
Lazy::new(|| gen_wordlist(include_str!("langs/french.txt")));
#[cfg(feature = "italian")]
pub static WORDLIST_ITALIAN: Lazy<WordList> =
Lazy::new(|| gen_wordlist(include_str!("langs/italian.txt")));
#[cfg(feature = "japanese")]
pub static WORDLIST_JAPANESE: Lazy<WordList> =
Lazy::new(|| gen_wordlist(include_str!("langs/japanese.txt")));
#[cfg(feature = "korean")]
pub static WORDLIST_KOREAN: Lazy<WordList> =
Lazy::new(|| gen_wordlist(include_str!("langs/korean.txt")));
#[cfg(feature = "spanish")]
pub static WORDLIST_SPANISH: Lazy<WordList> =
Lazy::new(|| gen_wordlist(include_str!("langs/spanish.txt")));
pub static WORDMAP_ENGLISH: Lazy<WordMap> = Lazy::new(|| gen_wordmap(&WORDLIST_ENGLISH));
#[cfg(feature = "chinese-simplified")]
pub static WORDMAP_CHINESE_SIMPLIFIED: Lazy<WordMap> =
Lazy::new(|| gen_wordmap(&WORDLIST_CHINESE_SIMPLIFIED));
#[cfg(feature = "chinese-traditional")]
pub static WORDMAP_CHINESE_TRADITIONAL: Lazy<WordMap> =
Lazy::new(|| gen_wordmap(&WORDLIST_CHINESE_TRADITIONAL));
#[cfg(feature = "french")]
pub static WORDMAP_FRENCH: Lazy<WordMap> = Lazy::new(|| gen_wordmap(&WORDLIST_FRENCH));
#[cfg(feature = "italian")]
pub static WORDMAP_ITALIAN: Lazy<WordMap> = Lazy::new(|| gen_wordmap(&WORDLIST_ITALIAN));
#[cfg(feature = "japanese")]
pub static WORDMAP_JAPANESE: Lazy<WordMap> = Lazy::new(|| gen_wordmap(&WORDLIST_JAPANESE));
#[cfg(feature = "korean")]
pub static WORDMAP_KOREAN: Lazy<WordMap> = Lazy::new(|| gen_wordmap(&WORDLIST_KOREAN));
#[cfg(feature = "spanish")]
pub static WORDMAP_SPANISH: Lazy<WordMap> = Lazy::new(|| gen_wordmap(&WORDLIST_SPANISH));
}
#[derive(Debug, Clone, Copy)]
pub enum Language {
English,
#[cfg(feature = "chinese-simplified")]
ChineseSimplified,
#[cfg(feature = "chinese-traditional")]
ChineseTraditional,
#[cfg(feature = "french")]
French,
#[cfg(feature = "italian")]
Italian,
#[cfg(feature = "japanese")]
Japanese,
#[cfg(feature = "korean")]
Korean,
#[cfg(feature = "spanish")]
Spanish,
}
impl Language {
pub fn wordlist(&self) -> &'static WordList {
match *self {
Language::English => &lazy::WORDLIST_ENGLISH,
#[cfg(feature = "chinese-simplified")]
Language::ChineseSimplified => &lazy::WORDLIST_CHINESE_SIMPLIFIED,
#[cfg(feature = "chinese-traditional")]
Language::ChineseTraditional => &lazy::WORDLIST_CHINESE_TRADITIONAL,
#[cfg(feature = "french")]
Language::French => &lazy::WORDLIST_FRENCH,
#[cfg(feature = "italian")]
Language::Italian => &lazy::WORDLIST_ITALIAN,
#[cfg(feature = "japanese")]
Language::Japanese => &lazy::WORDLIST_JAPANESE,
#[cfg(feature = "korean")]
Language::Korean => &lazy::WORDLIST_KOREAN,
#[cfg(feature = "spanish")]
Language::Spanish => &lazy::WORDLIST_SPANISH,
}
}
pub fn wordmap(&self) -> &'static WordMap {
match *self {
Language::English => &lazy::WORDMAP_ENGLISH,
#[cfg(feature = "chinese-simplified")]
Language::ChineseSimplified => &lazy::WORDMAP_CHINESE_SIMPLIFIED,
#[cfg(feature = "chinese-traditional")]
Language::ChineseTraditional => &lazy::WORDMAP_CHINESE_TRADITIONAL,
#[cfg(feature = "french")]
Language::French => &lazy::WORDMAP_FRENCH,
#[cfg(feature = "italian")]
Language::Italian => &lazy::WORDMAP_ITALIAN,
#[cfg(feature = "japanese")]
Language::Japanese => &lazy::WORDMAP_JAPANESE,
#[cfg(feature = "korean")]
Language::Korean => &lazy::WORDMAP_KOREAN,
#[cfg(feature = "spanish")]
Language::Spanish => &lazy::WORDMAP_SPANISH,
}
}
}
impl Default for Language {
fn default() -> Language {
Language::English
}
}
#[cfg(test)]
mod test {
use super::lazy;
use super::WordList;
fn is_wordlist_nfkd(wl: &WordList) -> bool {
for idx in 0..2047 {
let word = wl.get_word(idx.into());
if !unicode_normalization::is_nfkd(word) {
return false;
}
}
return true;
}
#[test]
#[cfg(feature = "chinese-simplified")]
fn chinese_simplified_wordlist_is_nfkd() {
assert!(is_wordlist_nfkd(&lazy::WORDLIST_CHINESE_SIMPLIFIED));
}
#[test]
#[cfg(feature = "chinese-traditional")]
fn chinese_traditional_wordlist_is_nfkd() {
assert!(is_wordlist_nfkd(&lazy::WORDLIST_CHINESE_TRADITIONAL));
}
#[test]
#[cfg(feature = "french")]
fn french_wordlist_is_nfkd() {
assert!(is_wordlist_nfkd(&lazy::WORDLIST_FRENCH));
}
#[test]
#[cfg(feature = "italian")]
fn italian_wordlist_is_nfkd() {
assert!(is_wordlist_nfkd(&lazy::WORDLIST_ITALIAN));
}
#[test]
#[cfg(feature = "japanese")]
fn japanese_wordlist_is_nfkd() {
assert!(is_wordlist_nfkd(&lazy::WORDLIST_JAPANESE));
}
#[test]
#[cfg(feature = "korean")]
fn korean_wordlist_is_nfkd() {
assert!(is_wordlist_nfkd(&lazy::WORDLIST_KOREAN));
}
#[test]
#[cfg(feature = "spanish")]
fn spanish_wordlist_is_nfkd() {
assert!(is_wordlist_nfkd(&lazy::WORDLIST_SPANISH));
}
}