use crate::Error; use std::collections::HashMap; use std::fs; use std::path::{Path, PathBuf}; pub struct ModuleResolver { prelude: HashMap, } impl ModuleResolver { pub fn new() -> Self { let mut prelude = HashMap::new(); prelude.insert( "Combinators".to_string(), include_str!("../prelude/Combinators.i++"), ); prelude.insert("Math".to_string(), include_str!("../prelude/Math.i++")); ModuleResolver { prelude } } pub fn resolve(&self, name: &str, base_dir: &Path) -> Result<(String, PathBuf), Error> { let local_path = base_dir.join(format!("{}.i++", name)); if local_path.exists() { let content = fs::read_to_string(&local_path) .map_err(|e| Error::Eval(format!("cannot read module file '{}': {}", name, e)))?; let parent = local_path.parent().unwrap_or(Path::new(".")).to_path_buf(); return Ok((content, parent)); } if let Some(content) = self.prelude.get(name) { return Ok((content.to_string(), PathBuf::from("."))); } Err(Error::Eval(format!("module not found: {}", name))) } } impl Default for ModuleResolver { fn default() -> Self { Self::new() } }