use crate::graph::GraphReadWriter;
use petgraph::stable_graph::DefaultIx;
use serde::{de::DeserializeOwned, Serialize};
use std::{fs::File, io::Error as IOError};
impl<NodeWeight, EdgeWeight, EdgeType> GraphReadWriter<NodeWeight, EdgeWeight>
for petgraph::Graph<NodeWeight, EdgeWeight, EdgeType, DefaultIx>
where
NodeWeight: Serialize + DeserializeOwned,
EdgeWeight: Serialize + DeserializeOwned,
EdgeType: petgraph::EdgeType,
{
fn serialize_graph_to_file(&self, path: &str) -> Result<(), IOError> {
let file = File::create(path)?;
serde_json::ser::to_writer(file, &self)
.map_err(|e| IOError::new(std::io::ErrorKind::Other, e))
}
fn deserialize_graph_from_file(path: &str) -> Result<Box<Self>, IOError> {
let file = File::open(path)?;
serde_json::de::from_reader(file)
.map(Box::new)
.map_err(|e| IOError::new(std::io::ErrorKind::Other, e))
}
}