-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmod.rs
More file actions
58 lines (50 loc) · 1.35 KB
/
mod.rs
File metadata and controls
58 lines (50 loc) · 1.35 KB
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
pub mod module;
pub use module::Module;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct ModuleGraphPartial {
pub modules: HashMap<String, Module>,
}
#[derive(Debug, Default)]
pub struct ModuleGraph {
// 已构建好的内容,只读
pub(crate) partials: Vec<ModuleGraphPartial>,
// 增量构建可编辑的部分,可写
// active: Option<ModuleGraphPartial>,
}
impl ModuleGraph {
pub fn new() -> Self {
Self::default()
}
pub fn add_module(&mut self, partial: ModuleGraphPartial) {
self.partials.push(partial);
}
pub fn add_single_module(&mut self, module_id: String, module: Module) {
// 查找最后一个 partial,如果为空则创建新的
if self.partials.is_empty() {
self.partials.push(ModuleGraphPartial {
modules: std::collections::HashMap::new(),
});
}
// 添加到最后一个 partial
if let Some(last) = self.partials.last_mut() {
last.modules.insert(module_id, module);
}
}
pub fn has_module(&self, id: &str) -> bool {
for partial in &self.partials {
if partial.modules.contains_key(id) {
return true;
}
}
false
}
pub fn get_module(&self, id: &str) -> Option<Module> {
for partial in &self.partials {
if let Some(module) = partial.modules.get(id) {
return Some(module.clone());
}
}
None
}
}