use std::collections::HashMap;
pub trait LibraryLoader {
fn load_library(&self, path: &str) -> Result<String, String>;
}
#[derive(Debug, Default, Clone)]
pub struct FileResolver {
files: HashMap<String, String>,
}
impl FileResolver {
pub fn new() -> Self {
Self::default()
}
pub fn add(&mut self, path: &str, source: &str) {
self.files.insert(path.to_string(), source.to_string());
}
pub fn with_file(mut self, path: &str, source: &str) -> Self {
self.add(path, source);
self
}
}
impl LibraryLoader for FileResolver {
fn load_library(&self, path: &str) -> Result<String, String> {
self.files
.get(path)
.cloned()
.ok_or_else(|| format!("no library registered for '{path}'"))
}
}