use std::collections::HashMap;
use crate::error::SourceError;
pub trait Source: Send + Sync {
fn get(&self, name: &str) -> Result<Vec<u8>, SourceError>;
}
pub struct SourceRegistry {
sources: HashMap<String, Box<dyn Source>>,
}
impl SourceRegistry {
pub fn new() -> Self {
Self {
sources: HashMap::new(),
}
}
pub fn register(&mut self, id: impl Into<String>, source: impl Source + 'static) {
self.sources.insert(id.into(), Box::new(source));
}
pub fn get(&self, source_id: &str) -> Option<&dyn Source> {
self.sources.get(source_id).map(|s| s.as_ref())
}
}
impl Default for SourceRegistry {
fn default() -> Self {
Self::new()
}
}