arbor_graph/
edge.rs

1//! Edge types for the code graph.
2//!
3//! Edges represent relationships between code entities. We keep
4//! the set of edge kinds focused on what's useful for understanding
5//! code architecture.
6
7use serde::{Deserialize, Serialize};
8
9/// The type of relationship between two code entities.
10#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
11#[serde(rename_all = "snake_case")]
12pub enum EdgeKind {
13    /// Function A calls function B.
14    Calls,
15
16    /// Module A imports from module B.
17    Imports,
18
19    /// Class A extends class B.
20    Extends,
21
22    /// Class implements interface/trait.
23    Implements,
24
25    /// Type references another type.
26    UsesType,
27
28    /// General reference to a symbol.
29    References,
30
31    /// Container relationship (class contains method).
32    Contains,
33
34    /// Control flow: statement A flows to statement B.
35    /// Used for CFG (Control Flow Graph) edges.
36    FlowsTo,
37
38    /// Data dependency: variable use depends on definition.
39    /// Used for DFA (Data Flow Analysis) edges.
40    DataDependency,
41}
42
43impl std::fmt::Display for EdgeKind {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        let s = match self {
46            Self::Calls => "calls",
47            Self::Imports => "imports",
48            Self::Extends => "extends",
49            Self::Implements => "implements",
50            Self::UsesType => "uses_type",
51            Self::References => "references",
52            Self::Contains => "contains",
53            Self::FlowsTo => "flows_to",
54            Self::DataDependency => "data_dependency",
55        };
56        write!(f, "{}", s)
57    }
58}
59
60/// An edge in the code graph with location info.
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct Edge {
63    /// The kind of relationship.
64    pub kind: EdgeKind,
65
66    /// File where this edge originates.
67    pub file: Option<String>,
68
69    /// Line number where this edge originates.
70    pub line: Option<u32>,
71}
72
73impl Edge {
74    /// Creates a new edge.
75    pub fn new(kind: EdgeKind) -> Self {
76        Self {
77            kind,
78            file: None,
79            line: None,
80        }
81    }
82
83    /// Creates an edge with location info.
84    pub fn with_location(kind: EdgeKind, file: impl Into<String>, line: u32) -> Self {
85        Self {
86            kind,
87            file: Some(file.into()),
88            line: Some(line),
89        }
90    }
91}
92
93/// A simplified edge for graph export/visualization.
94#[derive(Debug, Clone, Serialize, Deserialize)]
95pub struct GraphEdge {
96    pub source: String,
97    pub target: String,
98    pub kind: EdgeKind,
99}