use std::collections::HashMap;
use rill_core::io::IoBackend;
use rill_core::traits::ParamValue;
pub type BackendCtor<T> =
fn(params: &HashMap<String, ParamValue>) -> Result<Box<dyn IoBackend<T>>, String>;
pub struct BackendFactory<T> {
ctors: HashMap<&'static str, BackendCtor<T>>,
}
impl<T> BackendFactory<T> {
pub fn new() -> Self {
Self {
ctors: HashMap::new(),
}
}
pub fn register(&mut self, name: &'static str, ctor: BackendCtor<T>) {
self.ctors.insert(name, ctor);
}
pub fn create(
&self,
name: &str,
params: &HashMap<String, ParamValue>,
) -> Result<Box<dyn IoBackend<T>>, String> {
match self.ctors.get(name) {
Some(ctor) => ctor(params),
None => Err(format!("unknown backend: {name}")),
}
}
pub fn contains(&self, name: &str) -> bool {
self.ctors.contains_key(name)
}
}
impl<T> Default for BackendFactory<T> {
fn default() -> Self {
Self::new()
}
}