Skip to main content

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/// Confidence assumed for edges that predate confidence tracking.
61///
62/// Graphs cached by an older build deserialize without the field; treating
63/// them as certain preserves their previous behaviour exactly.
64fn default_confidence() -> f32 {
65    1.0
66}
67
68/// An edge in the code graph with location info.
69#[derive(Debug, Clone, Serialize, Deserialize)]
70pub struct Edge {
71    /// The kind of relationship.
72    pub kind: EdgeKind,
73
74    /// File where this edge originates.
75    pub file: Option<String>,
76
77    /// Line number where this edge originates.
78    pub line: Option<u32>,
79
80    /// How sure we are this edge is real, in `[0.0, 1.0]`.
81    ///
82    /// An exact fully-qualified match is `1.0`. A bare name matched only by
83    /// same-directory locality is far weaker, and an ambiguous match weaker
84    /// still. Consumers that need certainty should filter on this rather than
85    /// treating every edge as proven — see [`Edge::is_confident`].
86    #[serde(default = "default_confidence")]
87    pub confidence: f32,
88}
89
90impl Edge {
91    /// Edges at or above this confidence are treated as established fact.
92    pub const CONFIDENT_THRESHOLD: f32 = 0.75;
93
94    /// Creates a new edge with full confidence.
95    pub fn new(kind: EdgeKind) -> Self {
96        Self {
97            kind,
98            file: None,
99            line: None,
100            confidence: 1.0,
101        }
102    }
103
104    /// Creates an edge with location info.
105    pub fn with_location(kind: EdgeKind, file: impl Into<String>, line: u32) -> Self {
106        Self {
107            kind,
108            file: Some(file.into()),
109            line: Some(line),
110            confidence: 1.0,
111        }
112    }
113
114    /// Sets the confidence, clamped to `[0.0, 1.0]`.
115    pub fn with_confidence(mut self, confidence: f32) -> Self {
116        self.confidence = confidence.clamp(0.0, 1.0);
117        self
118    }
119
120    /// Whether this edge is strong enough to treat as certain.
121    pub fn is_confident(&self) -> bool {
122        self.confidence >= Self::CONFIDENT_THRESHOLD
123    }
124}
125
126/// A simplified edge for graph export/visualization.
127#[derive(Debug, Clone, Serialize, Deserialize)]
128pub struct GraphEdge {
129    pub source: String,
130    pub target: String,
131    pub kind: EdgeKind,
132}