use crate::MarineError;
use crate::MarineResult;
use std::collections::HashMap;
use std::path::PathBuf;
pub(crate) fn load_modules_from_fs(
modules: &HashMap<String, PathBuf>,
) -> MarineResult<HashMap<String, Vec<u8>>> {
let loaded = modules
.iter()
.try_fold(HashMap::new(), |mut hash_map, (import_name, path)| {
let module_bytes = std::fs::read(path).map_err(|e| {
MarineError::IOError(format!("failed to load {}: {}", path.display(), e))
})?;
if hash_map.insert(import_name.clone(), module_bytes).is_some() {
return Err(MarineError::InvalidConfig(String::from(
"module {} is duplicated in config",
)));
}
Ok(hash_map)
})?;
Ok(loaded)
}