use std::path::Path;
use anyhow::Result;
use super::CodeGraph;
pub fn serialize(graph: &CodeGraph) -> Result<Vec<u8>> {
let bytes = bincode::serialize(graph)?;
Ok(bytes)
}
pub fn deserialize(bytes: &[u8]) -> Result<CodeGraph> {
let graph = bincode::deserialize(bytes)?;
Ok(graph)
}
pub fn load(path: &Path) -> CodeGraph {
match std::fs::read(path) {
Ok(bytes) => deserialize(&bytes).unwrap_or_else(|_| CodeGraph::new()),
Err(_) => CodeGraph::new(),
}
}
pub fn save(graph: &CodeGraph, path: &Path) -> Result<()> {
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent)?;
}
let bytes = serialize(graph)?;
std::fs::write(path, bytes)?;
Ok(())
}