extern crate levtree;

use levtree::CaseSensitiveLevTrie;
use levtree::DamerauLevenshteinDistanceCalculator;
use levtree::LevTrie;

use std::io::BufRead;
use std::io::BufReader;

fn main() {
    let bytes = include_bytes!("cracklib-small");
    let reader = BufReader::new(&bytes[..]);
    let mut trie: CaseSensitiveLevTrie = LevTrie::new();
    reader
        .lines()
        .map(|line| line.unwrap())
        .for_each(|word: String| {
            trie.add(word.chars());
        });

    let keys: Vec<Vec<char>> = [
        "camel",
        "coriolis",
        "mattel",
        "cruzer",
        "cpoper",
        "roublesoot",
    ]
    .into_iter()
    .map(|it| it.chars().collect())
    .collect();

    for _ in 0..50 {
        for key in &keys {
            let word = &key;
            trie.fuzzy_search::<DamerauLevenshteinDistanceCalculator>(word, 6);
        }
    }

    for key in keys {
        let word = &key;
        let results = trie.fuzzy_search::<DamerauLevenshteinDistanceCalculator>(word, 6);
        println!("needle: {}", key.iter().collect::<String>());
        for result in results {
            let word: String = trie.lineal_descendant(result.word).into_iter().collect();
            println!("distance: {}, wordkey: {}", result.distance, word);
        }
        println!("")
    }
}
