use std::io::Read;
use lazy_static::{initialize, lazy_static};
use libflate::gzip::Decoder;
const DELIMITER: char = 0x0C as char;
static COMPRESSED_DICTIONARY: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/dictionary.gz"));
lazy_static! {
static ref DICTIONARY: Vec<[String; 2]> = {
let string = {
let mut dec = Decoder::new(COMPRESSED_DICTIONARY).expect("Failed to initialize runtime dictionary decoder");
let mut output = vec![];
dec.read_to_end(&mut output).expect("Failed to decompress dictionary");
String::from_utf8(output).expect("Failed to interpret decompressed data as string")
};
string
.split(DELIMITER as char)
.collect::<Vec<&str>>()
.chunks_exact(2)
.map(|s| [s[0].into(), s[1].into()])
.collect::<Vec<[String; 2]>>()
};
}
pub fn preload() {
initialize(&DICTIONARY);
}
pub fn dictionary<T: AsRef<str>>(word: T) -> Option<&'static str> {
let key = word.as_ref().to_uppercase();
DICTIONARY.binary_search_by(|[w, _]| w.cmp(&key))
.ok()
.and_then(|idx| Some(DICTIONARY[idx][1].as_str()))
}
mod test {
#[test]
fn test_define_rust() {
assert_eq!(
super::dictionary("rust"),
Some("The reddish yellow coating formed on iron when exposed to moistair, consisting of ferric oxide or hydroxide; hence, by extension,any metallic film of corrosion.")
)
}
}