use super::*;
#[derive(Clone, Debug, Default)]
pub(crate) struct ModuleHistory {
next: u32,
active: HashMap<String, (ModuleId, Option<SourceId>)>,
}
impl ModuleHistory {
pub(crate) fn remember(&mut self, graph: &ModuleGraph) {
self.next = self.next.max(graph.next);
self.active = graph
.modules
.values()
.map(|module| (module.path.clone(), (module.id, module.source)))
.collect();
}
pub(crate) fn reconcile(&mut self, graph: &mut ModuleGraph) -> HashMap<ModuleId, ModuleId> {
let mut mapping = HashMap::new();
for module in graph.modules.values() {
let id = self
.active
.get(&module.path)
.filter(|(_, source)| *source == module.source)
.map(|(id, _)| *id)
.unwrap_or_else(|| {
let id = ModuleId::new(self.next);
self.next = self.next.checked_add(1).expect("module ID overflow");
id
});
mapping.insert(module.id, id);
}
graph.modules = std::mem::take(&mut graph.modules)
.into_values()
.map(|mut module| {
module.id = mapping[&module.id];
module.parent = module.parent.map(|parent| mapping[&parent]);
(module.id, module)
})
.collect();
for id in graph
.by_path
.values_mut()
.chain(graph.by_source.values_mut())
{
*id = mapping[id];
}
graph.next = self.next;
self.remember(graph);
mapping
}
}