use std::collections::HashMap;
use crate::module::Module;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum ItemKey {
Function(String),
Struct(String),
StructMethod(String, String),
StructConstant(String, String),
}
pub struct ModuleGraph<'a> {
pub modules: &'a [Module],
by_name: HashMap<&'a str, &'a Module>,
children: HashMap<&'a str, Vec<&'a str>>,
}
impl<'a> ModuleGraph<'a> {
pub fn new(modules: &'a [Module]) -> Self {
let by_name = modules
.iter()
.map(|module| (module.name.as_str(), module))
.collect::<HashMap<_, _>>();
let mut children = HashMap::<&str, Vec<&str>>::new();
for module in modules {
for child in &module.submodules {
if let Some(child_module) = by_name.get(child.as_str()) {
children
.entry(module.name.as_str())
.or_default()
.push(child_module.name.as_str());
}
}
}
Self {
modules,
by_name,
children,
}
}
pub fn get(&self, name: &str) -> Option<&'a Module> {
self.by_name.get(name).copied()
}
pub fn child_modules(&self, name: &str) -> &[&'a str] {
self.children.get(name).map_or(&[], Vec::as_slice)
}
}