1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
mod modules_load_strategy;
pub(crate) use modules_load_strategy::ModulesLoadStrategy;
use crate::FaaSError;
use crate::FaaSResult;
use std::collections::HashMap;
use std::path::Path;
pub(crate) fn load_modules_from_fs(
modules_dir: &Path,
modules: ModulesLoadStrategy<'_>,
) -> FaaSResult<HashMap<String, Vec<u8>>> {
use FaaSError::IOError;
let mut dir_entries =
std::fs::read_dir(modules_dir).map_err(|e| IOError(format!("{:?}: {}", modules_dir, e)))?;
let loaded = dir_entries.try_fold(HashMap::new(), |mut hash_map, entry| {
let entry = entry?;
let path = entry.path();
if path.is_dir() {
return Ok(hash_map);
}
let file_name = Path::new(
path.file_name()
.ok_or_else(|| IOError(format!("No file name in path {:?}", path)))?,
);
if modules.should_load(file_name) {
let module_bytes = std::fs::read(&path)?;
let module_name = modules.extract_module_name(&path)?;
if hash_map.insert(module_name, module_bytes).is_some() {
return Err(FaaSError::InvalidConfig(String::from(
"module {} is duplicated in config",
)));
}
}
Ok(hash_map)
})?;
if modules.required_modules_len() > loaded.len() {
let loaded = loaded.iter().map(|(n, _)| n);
let not_found = modules.missing_modules(loaded);
return Err(FaaSError::InvalidConfig(format!(
"the following modules were not found: {:?}",
not_found
)));
}
Ok(loaded)
}