#![deny(missing_docs)]
use bitvec::prelude::*;
use thiserror::Error;
mod bits;
mod types;
pub use types::*;
mod gen;
use crate::bits::index_width;
pub use gen::*;
#[derive(Error, Debug)]
pub enum WordlistError {
#[error("Index `{0}` is out of bounds (`{1}`).")]
IndexOutOfBounds(usize, usize),
#[error("Wordlist is potentially incomplete as unable to index into it.")]
WordlistIsIncomplete,
#[error("The given word ('{0}') is not known to the given wordlist.")]
InvalidWord(String),
#[error("The wordlist has an invalid size ('{0}').")]
InvalidWordlistSize(usize),
}
pub type Result<T> = std::result::Result<T, WordlistError>;
pub type WordVec = std::vec::Vec<&'static str>;
pub trait WordlistByteConverter {
fn to_words(&self, bytes: &BitVec) -> Result<WordVec>;
fn to_bytes(&self, words: &WordVec) -> Result<BitVec>;
}
pub trait Wordlist {
fn name(&self) -> &String;
fn size(&self) -> usize;
fn contains(&self, word: &str) -> bool;
fn index_bit_width(&self) -> Result<usize> {
index_width(self.size())
}
fn validate_word_list(&self, possible_words: &Vec<String>) -> bool {
possible_words.iter().all(|v| self.contains(v))
}
}