use helper::*;
use rtrees::bktree::SpellTree;
use std::collections::BTreeMap;
#[derive(Default)]
pub struct Commands {
suggestions: SpellTree<()>,
search: BTreeMap<&'static str, MRc<dyn Cmd>>,
}
impl Commands {
pub fn add_command(&mut self, command_name: &'static str, functionality: MRc<dyn Cmd>) -> bool {
if self.search.contains_key(command_name) {
return false;
} else {
self.suggestions.insert(command_name.to_string(), ());
self.search.insert(command_name, functionality);
return true;
}
}
pub fn find(&self, command: &str) -> Option<MRc<dyn Cmd>> {
return self.search.get(command).cloned();
}
pub fn suggest(&self, command: &str, tolerance: u64) -> Vec<&String> {
return self.suggestions.find(&command.to_string(), tolerance).1;
}
pub fn prefix<'a>(&'a self, command: &'a str) -> Vec<&&str> {
return self.search.range(command..).take_while(|(k, _)| k.starts_with(command)).map(|(k, _)| k).collect();
}
}