use std::collections::HashMap;
pub trait CommandNamespace{
fn on_command(&mut self, command: String, args: Vec<String>);
}
pub struct CommandRouter{
namespaces: HashMap<Vec<String>, Box<dyn CommandNamespace>>,
}
impl CommandRouter {
#[inline]
pub fn new() -> CommandRouter{
CommandRouter{
namespaces: HashMap::new(),
}
}
#[inline]
pub fn register_namespace(&mut self, namespace_path: Vec<String>,
namespace: Box<dyn CommandNamespace>){
if self.namespaces.contains_key(&namespace_path){
panic!("Namespace is already registered");
}
self.namespaces.insert(namespace_path, namespace);
}
pub fn on_command(&mut self, command: Vec<String>, arguments: Vec<String>) -> bool{
if command.len() == 0{
panic!("Empty command vector");
}
let command_name = command.last().unwrap();
let namespace = command[0..command.len()-1].to_vec();
if !self.namespaces.contains_key(&namespace){
return false;
}
self.namespaces.get_mut(&namespace).unwrap().on_command(command_name.clone(), arguments);
true
}
}