codegraph/graph/
types.rs

1//! Core graph types: nodes, edges, IDs, and enums.
2
3use super::property::{PropertyMap, PropertyValue};
4use serde::{Deserialize, Serialize};
5
6/// Unique identifier for a node (monotonic counter).
7pub type NodeId = u64;
8
9/// Unique identifier for an edge (monotonic counter).
10pub type EdgeId = u64;
11
12/// Type of a node in the code graph.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
14pub enum NodeType {
15    /// Source code file
16    CodeFile,
17    /// Function, method, or procedure
18    Function,
19    /// Class, struct, or type definition
20    Class,
21    /// Module, namespace, or package
22    Module,
23    /// Variable, constant, or field
24    Variable,
25    /// Type alias or primitive type
26    Type,
27    /// Interface, trait, or protocol
28    Interface,
29    /// Catch-all for custom entity types
30    Generic,
31}
32
33impl std::fmt::Display for NodeType {
34    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
35        match self {
36            NodeType::CodeFile => write!(f, "CodeFile"),
37            NodeType::Function => write!(f, "Function"),
38            NodeType::Class => write!(f, "Class"),
39            NodeType::Module => write!(f, "Module"),
40            NodeType::Variable => write!(f, "Variable"),
41            NodeType::Type => write!(f, "Type"),
42            NodeType::Interface => write!(f, "Interface"),
43            NodeType::Generic => write!(f, "Generic"),
44        }
45    }
46}
47
48/// Type of edge (relationship) between nodes.
49#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
50pub enum EdgeType {
51    /// File A imports File B
52    Imports,
53    /// File A imports symbols from File B
54    ImportsFrom,
55    /// Parent contains child entity (file contains function)
56    Contains,
57    /// Function A calls Function B
58    Calls,
59    /// Function invokes method on object
60    Invokes,
61    /// Function creates instance of class
62    Instantiates,
63    /// Class A extends/inherits from Class B
64    Extends,
65    /// Class implements interface/trait
66    Implements,
67    /// Generic usage relationship
68    Uses,
69    /// Module defines entity
70    Defines,
71    /// Generic reference
72    References,
73}
74
75impl std::fmt::Display for EdgeType {
76    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77        match self {
78            EdgeType::Imports => write!(f, "Imports"),
79            EdgeType::ImportsFrom => write!(f, "ImportsFrom"),
80            EdgeType::Contains => write!(f, "Contains"),
81            EdgeType::Calls => write!(f, "Calls"),
82            EdgeType::Invokes => write!(f, "Invokes"),
83            EdgeType::Instantiates => write!(f, "Instantiates"),
84            EdgeType::Extends => write!(f, "Extends"),
85            EdgeType::Implements => write!(f, "Implements"),
86            EdgeType::Uses => write!(f, "Uses"),
87            EdgeType::Defines => write!(f, "Defines"),
88            EdgeType::References => write!(f, "References"),
89        }
90    }
91}
92
93/// Direction for neighbor queries.
94#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
95pub enum Direction {
96    /// Follow outgoing edges (from this node)
97    Outgoing,
98    /// Follow incoming edges (to this node)
99    Incoming,
100    /// Follow edges in both directions
101    Both,
102}
103
104/// A node in the code graph.
105///
106/// Nodes represent code entities like files, functions, classes, etc.
107#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct Node {
109    /// Unique identifier (assigned by graph)
110    pub id: NodeId,
111    /// Type of code entity
112    pub node_type: NodeType,
113    /// Flexible key-value metadata
114    pub properties: PropertyMap,
115}
116
117impl Node {
118    /// Create a new node (ID will be assigned by graph).
119    pub fn new(id: NodeId, node_type: NodeType, properties: PropertyMap) -> Self {
120        Self {
121            id,
122            node_type,
123            properties,
124        }
125    }
126
127    /// Add or update a property.
128    pub fn set_property(&mut self, key: impl Into<String>, value: impl Into<PropertyValue>) {
129        self.properties.insert(key, value);
130    }
131
132    /// Get a property value.
133    pub fn get_property(&self, key: &str) -> Option<&PropertyValue> {
134        self.properties.get(key)
135    }
136}
137
138/// A directed edge in the code graph.
139///
140/// Edges represent relationships between nodes.
141#[derive(Debug, Clone, Serialize, Deserialize)]
142pub struct Edge {
143    /// Unique identifier (assigned by graph)
144    pub id: EdgeId,
145    /// Source node ID
146    pub source_id: NodeId,
147    /// Target node ID
148    pub target_id: NodeId,
149    /// Type of relationship
150    pub edge_type: EdgeType,
151    /// Optional metadata (e.g., line number for calls)
152    pub properties: PropertyMap,
153}
154
155impl Edge {
156    /// Create a new edge (ID will be assigned by graph).
157    pub fn new(
158        id: EdgeId,
159        source_id: NodeId,
160        target_id: NodeId,
161        edge_type: EdgeType,
162        properties: PropertyMap,
163    ) -> Self {
164        Self {
165            id,
166            source_id,
167            target_id,
168            edge_type,
169            properties,
170        }
171    }
172
173    /// Add or update a property.
174    pub fn set_property(&mut self, key: impl Into<String>, value: impl Into<PropertyValue>) {
175        self.properties.insert(key, value);
176    }
177
178    /// Get a property value.
179    pub fn get_property(&self, key: &str) -> Option<&PropertyValue> {
180        self.properties.get(key)
181    }
182}