1use super::property::{PropertyMap, PropertyValue};
4use serde::{Deserialize, Serialize};
5
6pub type NodeId = u64;
8
9pub type EdgeId = u64;
11
12#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
14pub enum NodeType {
15 CodeFile,
17 Function,
19 Class,
21 Module,
23 Variable,
25 Type,
27 Interface,
29 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
50pub enum EdgeType {
51 Imports,
53 ImportsFrom,
55 Contains,
57 Calls,
59 Invokes,
61 Instantiates,
63 Extends,
65 Implements,
67 Uses,
69 Defines,
71 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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
95pub enum Direction {
96 Outgoing,
98 Incoming,
100 Both,
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize)]
108pub struct Node {
109 pub id: NodeId,
111 pub node_type: NodeType,
113 pub properties: PropertyMap,
115}
116
117impl Node {
118 pub fn new(id: NodeId, node_type: NodeType, properties: PropertyMap) -> Self {
120 Self {
121 id,
122 node_type,
123 properties,
124 }
125 }
126
127 pub fn set_property(&mut self, key: impl Into<String>, value: impl Into<PropertyValue>) {
129 self.properties.insert(key, value);
130 }
131
132 pub fn get_property(&self, key: &str) -> Option<&PropertyValue> {
134 self.properties.get(key)
135 }
136}
137
138#[derive(Debug, Clone, Serialize, Deserialize)]
142pub struct Edge {
143 pub id: EdgeId,
145 pub source_id: NodeId,
147 pub target_id: NodeId,
149 pub edge_type: EdgeType,
151 pub properties: PropertyMap,
153}
154
155impl Edge {
156 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 pub fn set_property(&mut self, key: impl Into<String>, value: impl Into<PropertyValue>) {
175 self.properties.insert(key, value);
176 }
177
178 pub fn get_property(&self, key: &str) -> Option<&PropertyValue> {
180 self.properties.get(key)
181 }
182}