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
pub mod english;
pub use self::english::*;
use thiserror::Error;
#[derive(Debug, Error, PartialEq, Eq)]
pub enum WordlistError {
#[error("the index `{0}` is invalid")]
InvalidIndex(usize),
#[error("the word `{0}` is invalid")]
InvalidWord(String),
}
pub trait Wordlist {
const WORDLIST: &'static str;
fn get(index: usize) -> Result<String, WordlistError> {
if index >= 2048 {
return Err(WordlistError::InvalidIndex(index));
}
Ok(Self::get_all()[index].into())
}
fn get_index(word: &str) -> Result<usize, WordlistError> {
match Self::get_all().iter().position(|element| element == &word) {
Some(index) => Ok(index),
None => Err(WordlistError::InvalidWord(word.into())),
}
}
fn get_all() -> Vec<&'static str> {
Self::WORDLIST.lines().collect::<Vec<&str>>()
}
}