Skip to main content

synapse_core/
persistence.rs

1use serde::{Deserialize, Serialize};
2use std::fs;
3use std::path::Path;
4
5#[derive(Serialize, Deserialize)]
6pub struct GraphSnapshot {
7    pub nodes: Vec<(u32, String)>,        // (id, name)
8    pub edges: Vec<(u32, u32, u16, u32)>, // (from, to, predicate_id, edge_id)
9    pub predicates: Vec<(u16, String)>,   // (id, name)
10    pub next_edge_id: u32,
11}
12
13impl GraphSnapshot {
14    pub fn save_to_file(&self, path: &str) -> std::io::Result<()> {
15        let data = bincode::serialize(self).map_err(std::io::Error::other)?;
16        fs::write(path, data)?;
17        println!("💾 Graph saved to {}", path);
18        Ok(())
19    }
20
21    pub fn load_from_file(path: &str) -> std::io::Result<Self> {
22        if !Path::new(path).exists() {
23            return Ok(GraphSnapshot {
24                nodes: Vec::new(),
25                edges: Vec::new(),
26                predicates: Vec::new(),
27                next_edge_id: 0,
28            });
29        }
30
31        let data = fs::read(path)?;
32        let snapshot: GraphSnapshot = bincode::deserialize(&data).map_err(std::io::Error::other)?;
33        println!("📂 Graph loaded from {}", path);
34        Ok(snapshot)
35    }
36}