1use std::fs::File;
2use std::path::Path;
3use zstd::{Decoder, Encoder};
4
5use petgraph::graph::DefaultIx;
6use petgraph::graph::NodeIndex;
7use petgraph::{Graph, Undirected};
8use serde::{Deserialize, Serialize};
9
10use anyhow::Result;
11
12#[derive(Serialize, Deserialize, Debug, Clone)]
14pub struct BookID {
15 pub code: i32,
16 pub label: Option<String>,
17 #[serde(default)]
18 pub cluster: i32,
19}
20
21pub type IdGraph = Graph<BookID, (), Undirected>;
22pub type IdNode = NodeIndex<DefaultIx>;
23
24mod gml;
25mod load;
26pub mod model;
27mod sources;
28
29pub use gml::save_gml;
30pub use load::construct_graph;
31
32pub fn save_graph<P: AsRef<Path>>(graph: &IdGraph, path: P) -> Result<()> {
34 let file = File::create(path)?;
35 let mut out = Encoder::new(file, 4)?;
36 out.multithread(4)?;
37 rmp_serde::encode::write(&mut out, &graph)?;
38 out.finish()?;
39 Ok(())
40}
41
42pub fn load_graph<P: AsRef<Path>>(path: P) -> Result<IdGraph> {
44 let file = File::open(path)?;
45 let rdr = Decoder::new(file)?;
46 let g = rmp_serde::decode::from_read(rdr)?;
47 Ok(g)
48}