codeprism_storage/
graph.rs

1//! Graph serialization types for storage
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::path::PathBuf;
6use std::time::SystemTime;
7
8/// Serializable representation of a code graph for storage
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct SerializableGraph {
11    pub repo_id: String,
12    pub nodes: Vec<SerializableNode>,
13    pub edges: Vec<SerializableEdge>,
14    pub metadata: GraphMetadata,
15}
16
17/// Serializable representation of a graph node
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub struct SerializableNode {
20    pub id: String,
21    pub name: String,
22    pub kind: String,
23    pub file: PathBuf,
24    pub span: SerializableSpan,
25    pub attributes: HashMap<String, String>,
26}
27
28/// Serializable representation of a graph edge
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct SerializableEdge {
31    pub source: String,
32    pub target: String,
33    pub kind: String,
34    pub attributes: HashMap<String, String>,
35}
36
37/// Serializable representation of a source code span
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct SerializableSpan {
40    pub start_byte: usize,
41    pub end_byte: usize,
42    pub start_line: usize,
43    pub end_line: usize,
44    pub start_column: usize,
45    pub end_column: usize,
46}
47
48/// Graph metadata for version tracking and incremental updates
49#[derive(Debug, Clone, Serialize, Deserialize)]
50pub struct GraphMetadata {
51    pub repo_id: String,
52    pub last_updated: SystemTime,
53    pub version: u64,
54    pub file_hashes: HashMap<PathBuf, String>,
55    pub total_nodes: usize,
56    pub total_edges: usize,
57    pub schema_version: String,
58}
59
60impl SerializableGraph {
61    /// Create a new empty serializable graph
62    pub fn new(repo_id: String) -> Self {
63        Self {
64            repo_id: repo_id.clone(),
65            nodes: Vec::new(),
66            edges: Vec::new(),
67            metadata: GraphMetadata {
68                repo_id,
69                last_updated: SystemTime::now(),
70                version: 1,
71                file_hashes: HashMap::new(),
72                total_nodes: 0,
73                total_edges: 0,
74                schema_version: "1.0".to_string(),
75            },
76        }
77    }
78
79    /// Add a node to the graph
80    pub fn add_node(&mut self, node: SerializableNode) {
81        self.nodes.push(node);
82        self.metadata.total_nodes = self.nodes.len();
83    }
84
85    /// Add an edge to the graph
86    pub fn add_edge(&mut self, edge: SerializableEdge) {
87        self.edges.push(edge);
88        self.metadata.total_edges = self.edges.len();
89    }
90
91    /// Update metadata
92    pub fn update_metadata(&mut self) {
93        self.metadata.last_updated = SystemTime::now();
94        self.metadata.total_nodes = self.nodes.len();
95        self.metadata.total_edges = self.edges.len();
96        self.metadata.version += 1;
97    }
98}
99
100impl SerializableNode {
101    /// Create a new serializable node
102    pub fn new(
103        id: String,
104        name: String,
105        kind: String,
106        file: PathBuf,
107        span: SerializableSpan,
108    ) -> Self {
109        Self {
110            id,
111            name,
112            kind,
113            file,
114            span,
115            attributes: HashMap::new(),
116        }
117    }
118
119    /// Add an attribute to the node
120    pub fn add_attribute(&mut self, key: String, value: String) {
121        self.attributes.insert(key, value);
122    }
123}
124
125impl SerializableEdge {
126    /// Create a new serializable edge
127    pub fn new(source: String, target: String, kind: String) -> Self {
128        Self {
129            source,
130            target,
131            kind,
132            attributes: HashMap::new(),
133        }
134    }
135
136    /// Add an attribute to the edge
137    pub fn add_attribute(&mut self, key: String, value: String) {
138        self.attributes.insert(key, value);
139    }
140}