use anyhow::Result;
use crate::graph::schema::{EdgeType, NodeType};
use crate::graph::Store;
use super::placeholder;
pub fn resolve_owns_edges(store: &Store) -> Result<()> {
placeholder::resolve_placeholder_edges(
store,
&EdgeType::Owns,
&[
NodeType::Struct,
NodeType::Enum,
NodeType::Trait,
NodeType::TypeAlias,
NodeType::Primitive,
],
)
}
#[cfg(test)]
mod tests {
use crate::graph::query::Query;
use crate::graph::schema::{EdgeType, NodeId, NodeType};
use crate::graph::Store;
use crate::pipeline::primitives;
use super::resolve_owns_edges;
#[test]
fn resolve_owns_edges_resolves_same_file_placeholder() {
let store = Store::new_memory().unwrap();
let path = "src/lib.rs";
let struct_id = NodeId::new(format!("{path}#8:1"));
store
.put_node(&struct_id, &NodeType::Struct, Some("Point"))
.unwrap();
let fn_id = NodeId::new(format!("{path}#12:1"));
store
.put_node(&fn_id, &NodeType::Function, Some("take_point"))
.unwrap();
let placeholder = NodeId::new(format!("{path}::Point"));
store
.put_edge(&fn_id, &placeholder, &EdgeType::Owns)
.unwrap();
resolve_owns_edges(&store).unwrap();
let edges = Query::all_edges(&store).unwrap();
assert_eq!(edges.rows.len(), 1);
let to_str = edges.rows[0][1].to_string().trim_matches('"').to_string();
assert!(
to_str.contains('#'),
"edge should point to real type id (path#line:col), got {to_str}"
);
assert_eq!(to_str, format!("{path}#8:1"));
}
#[test]
fn resolve_owns_edges_resolves_primitive_placeholder() {
let store = Store::new_memory().unwrap();
primitives::create_primitive_nodes(&store).unwrap();
let path = "src/lib.rs";
let fn_id = NodeId::new(format!("{path}#5:1"));
store
.put_node(&fn_id, &NodeType::Function, Some("f"))
.unwrap();
let placeholder = NodeId::new(format!("{path}::i32"));
store
.put_edge(&fn_id, &placeholder, &EdgeType::Owns)
.unwrap();
resolve_owns_edges(&store).unwrap();
let edges = Query::all_edges(&store).unwrap();
assert_eq!(
edges.rows.len(),
1,
"owns edge to i32 should resolve to primitive::i32"
);
let to_str = edges.rows[0][1].to_string().trim_matches('"').to_string();
assert_eq!(
to_str, "primitive::i32",
"edge should point to primitive::i32, got {to_str}"
);
}
#[test]
fn resolve_owns_edges_retains_unresolved_as_external_type() {
let store = Store::new_memory().unwrap();
let path = "src/lib.rs";
let fn_id = NodeId::new(format!("{path}#5:1"));
store
.put_node(&fn_id, &NodeType::Function, Some("f"))
.unwrap();
let placeholder = NodeId::new(format!("{path}::Vec"));
store
.put_edge(&fn_id, &placeholder, &EdgeType::Owns)
.unwrap();
resolve_owns_edges(&store).unwrap();
let edges = Query::all_edges(&store).unwrap();
assert_eq!(
edges.rows.len(),
1,
"edge should point to external_type node when type has no node in graph"
);
let to_str = edges.rows[0][1].to_string().trim_matches('"').to_string();
assert_eq!(to_str, "external::Vec");
}
}