use std::collections::HashMap;
use crate::method::method_callable::MethodCallable;
use crate::method::method_structure::Method;
pub struct MethodManagerImpl {
methods: Vec<Method>,
callables: HashMap<String, Box<dyn MethodCallable>>,
}
pub trait MethodManager {
fn add_method(&mut self, method: Method, callable: Box<dyn MethodCallable>) -> ();
fn get_method(&self, name: &String) -> Option<Method>;
fn get_method_callback(&self, name: &String) -> Option<&Box<dyn MethodCallable>>;
fn get_methods(&self) -> HashMap<String, Method>;
}
impl MethodManagerImpl {
pub fn new() -> Self {
Self {
methods: Vec::new(),
callables: HashMap::new(),
}
}
}
impl MethodManager for MethodManagerImpl {
fn add_method(&mut self, method: Method, callable: Box<dyn MethodCallable>) -> () {
self.methods.push(method.clone());
self.callables.insert(method.name, callable);
}
fn get_method(&self, name: &String) -> Option<Method> {
for method in self.methods.iter() {
if &method.name == name {
return Some(method.clone());
}
}
None
}
fn get_method_callback(&self, name: &String) -> Option<&Box<dyn MethodCallable>> {
let callable = self.callables.get(name);
match callable {
Some(callable) => Some(callable),
_ => None
}
}
fn get_methods(&self) -> HashMap<String, Method> {
let mut map = HashMap::new();
for method in &self.methods {
map.insert(method.name.clone(), method.clone());
}
map
}
}