#![doc = include_str!("../README.md")]
use libflate::gzip::Decoder;
use std::io::Read;
const COMPRESSED_LIST: &[u8] = include_bytes!(concat!(env!("OUT_DIR"), "/word_list.gz"));
lazy_static::lazy_static! {
pub static ref LIST: Vec<String> = {
let mut dec = Decoder::new(COMPRESSED_LIST).expect("Failed to initialize runtime decoder");
let mut output = Vec::new();
dec.read_to_end(&mut output).expect("Failed to decompress list");
std::str::from_utf8(output.as_slice()).expect("Failed to interpret decompressed data").split("\n").map(|x| x.to_string()).collect()
};
}
#[derive(Clone, Debug, PartialEq)]
pub struct Anagrams {
pub count: usize,
pub words: Vec<String>,
}
pub fn init() {
lazy_static::initialize(&LIST);
}
pub fn get<A: AsRef<str>>(search: A) -> Anagrams {
let search = search.as_ref().to_lowercase();
let search_chars: Vec<char> = search.chars().collect();
let mut words = Vec::new();
'word_loop: for word in LIST.iter() {
if word.chars().count() != search_chars.len() || word == &search {
continue;
}
for char in search_chars.iter() {
if word.matches(*char).count() != search.matches(*char).count() {
continue 'word_loop;
}
}
words.push(word.clone());
}
Anagrams {
count: words.len(),
words,
}
}