refactor: extract module_edges() to CodeGraph domain — removes duplication from Mermaid and D2 renderers
This commit is contained in:
@@ -278,4 +278,72 @@ impl CodeGraph {
|
||||
relationships: filtered_relationships,
|
||||
}
|
||||
}
|
||||
|
||||
/// Compute module-to-module edges with relationship counts.
|
||||
///
|
||||
/// Handles three cases:
|
||||
/// - Direct module-name edges injected by `merge_project_deps_as_module_edges`
|
||||
/// - Type-level composition/inheritance relationships resolved to their modules
|
||||
/// - Import relationships resolved via file-stem → module mapping
|
||||
pub fn module_edges(&self) -> HashMap<(String, String), usize> {
|
||||
let mut name_to_module: HashMap<&str, &str> = HashMap::new();
|
||||
let mut file_to_module: HashMap<String, String> = HashMap::new();
|
||||
let mut modules: HashSet<String> = HashSet::new();
|
||||
|
||||
for el in &self.elements {
|
||||
if let Some(m) = el.module() {
|
||||
modules.insert(m.as_str().to_string());
|
||||
name_to_module.insert(el.qualified_name(), m.as_str());
|
||||
name_to_module.insert(el.name(), m.as_str());
|
||||
|
||||
let file_stem = std::path::Path::new(el.file_path().as_str())
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or_default()
|
||||
.to_string();
|
||||
if !file_stem.is_empty() {
|
||||
file_to_module.insert(file_stem, m.as_str().to_string());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut edges: HashMap<(String, String), usize> = HashMap::new();
|
||||
|
||||
for rel in &self.relationships {
|
||||
match rel.kind() {
|
||||
RelationshipKind::Import => {
|
||||
let source_mod = file_to_module.get(rel.source());
|
||||
let target_top = rel.target().split('.').next().unwrap_or("");
|
||||
let target_mod = ModuleName::capitalize(target_top);
|
||||
if let Some(src) = source_mod
|
||||
&& modules.contains(&target_mod)
|
||||
&& *src != target_mod
|
||||
{
|
||||
*edges.entry((src.clone(), target_mod)).or_insert(0) += 1;
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
// Direct module-to-module edge (injected by merge_project_deps)
|
||||
if modules.contains(rel.source())
|
||||
&& modules.contains(rel.target())
|
||||
&& rel.source() != rel.target()
|
||||
{
|
||||
*edges
|
||||
.entry((rel.source().to_string(), rel.target().to_string()))
|
||||
.or_insert(0) += 1;
|
||||
continue;
|
||||
}
|
||||
let src_mod = name_to_module.get(rel.source()).copied();
|
||||
let tgt_mod = name_to_module.get(rel.target()).copied();
|
||||
if let (Some(s), Some(t)) = (src_mod, tgt_mod)
|
||||
&& s != t
|
||||
{
|
||||
*edges.entry((s.to_string(), t.to_string())).or_insert(0) += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
edges
|
||||
}
|
||||
}
|
||||
|
||||
@@ -299,3 +299,59 @@ fn graph_lists_unique_modules() {
|
||||
assert!(modules.iter().any(|m| m.as_str() == "Orders"));
|
||||
assert!(modules.iter().any(|m| m.as_str() == "Billing"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_edges_aggregates_type_level_relationships_into_module_pairs() {
|
||||
let mut graph = CodeGraph::new();
|
||||
graph.add_element(make_element("ServiceA", Some("App")));
|
||||
graph.add_element(make_element("ServiceB", Some("App")));
|
||||
graph.add_element(make_element("Order", Some("Domain")));
|
||||
graph.add_element(make_element("Product", Some("Domain")));
|
||||
|
||||
graph.add_relationship(
|
||||
Relationship::new("ServiceA", "Order", RelationshipKind::Composition).unwrap(),
|
||||
);
|
||||
graph.add_relationship(
|
||||
Relationship::new("ServiceB", "Product", RelationshipKind::Composition).unwrap(),
|
||||
);
|
||||
|
||||
let graph = graph.qualify();
|
||||
let edges = graph.module_edges();
|
||||
|
||||
assert_eq!(edges.len(), 1, "should have one cross-module edge pair");
|
||||
assert_eq!(edges[&("App".to_string(), "Domain".to_string())], 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_edges_handles_direct_module_to_module_relationships() {
|
||||
let mut graph = CodeGraph::new();
|
||||
graph.add_element(make_element("Order", Some("Domain")));
|
||||
graph.add_element(make_element("Service", Some("App")));
|
||||
// Direct module-name edge (injected by merge_project_deps)
|
||||
graph.add_relationship(
|
||||
Relationship::new("App", "Domain", RelationshipKind::Composition).unwrap(),
|
||||
);
|
||||
|
||||
let graph = graph.qualify();
|
||||
let edges = graph.module_edges();
|
||||
|
||||
assert!(
|
||||
edges.contains_key(&("App".to_string(), "Domain".to_string())),
|
||||
"direct module edge should appear: {edges:?}"
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn module_edges_excludes_intra_module_relationships() {
|
||||
let mut graph = CodeGraph::new();
|
||||
graph.add_element(make_element("Service", Some("App")));
|
||||
graph.add_element(make_element("Helper", Some("App")));
|
||||
graph.add_relationship(
|
||||
Relationship::new("Service", "Helper", RelationshipKind::Composition).unwrap(),
|
||||
);
|
||||
|
||||
let graph = graph.qualify();
|
||||
let edges = graph.module_edges();
|
||||
|
||||
assert!(edges.is_empty(), "intra-module relationships should not produce edges");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user