use std::fs::File;
use std::path::Path;
use zstd::{Decoder, Encoder};
use petgraph::graph::DefaultIx;
use petgraph::graph::NodeIndex;
use petgraph::{Graph, Undirected};
use serde::{Deserialize, Serialize};
use anyhow::Result;
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct BookID {
pub code: i32,
pub label: Option<String>,
#[serde(default)]
pub cluster: i32,
}
pub type IdGraph = Graph<BookID, (), Undirected>;
pub type IdNode = NodeIndex<DefaultIx>;
mod gml;
mod load;
pub mod model;
mod sources;
pub use gml::save_gml;
pub use load::construct_graph;
pub fn save_graph<P: AsRef<Path>>(graph: &IdGraph, path: P) -> Result<()> {
let file = File::create(path)?;
let mut out = Encoder::new(file, 4)?;
out.multithread(4)?;
rmp_serde::encode::write(&mut out, &graph)?;
out.finish()?;
Ok(())
}
pub fn load_graph<P: AsRef<Path>>(path: P) -> Result<IdGraph> {
let file = File::open(path)?;
let rdr = Decoder::new(file)?;
let g = rmp_serde::decode::from_read(rdr)?;
Ok(g)
}