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
35impl std::fmt::Display for EdgeKind {
36    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
37        let s = match self {
38            Self::Calls => "calls",
39            Self::Imports => "imports",
40            Self::Extends => "extends",
41            Self::Implements => "implements",
42            Self::UsesType => "uses_type",
43            Self::References => "references",
44            Self::Contains => "contains",
45        };
46        write!(f, "{}", s)
47    }
48}
49
50/// An edge in the code graph with location info.
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct Edge {
53    /// The kind of relationship.
54    pub kind: EdgeKind,
55
56    /// File where this edge originates.
57    pub file: Option<String>,
58
59    /// Line number where this edge originates.
60    pub line: Option<u32>,
61}
62
63impl Edge {
64    /// Creates a new edge.
65    pub fn new(kind: EdgeKind) -> Self {
66        Self {
67            kind,
68            file: None,
69            line: None,
70        }
71    }
72
73    /// Creates an edge with location info.
74    pub fn with_location(kind: EdgeKind, file: impl Into<String>, line: u32) -> Self {
75        Self {
76            kind,
77            file: Some(file.into()),
78            line: Some(line),
79        }
80    }
81}
82
83/// A simplified edge for graph export/visualization.
84#[derive(Debug, Clone, Serialize, Deserialize)]
85pub struct GraphEdge {
86    pub source: String,
87    pub target: String,
88    pub kind: EdgeKind,
89}