1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
use crate::{Edge, EdgeStore, Graph, Meta, Metadata, Node, NodeStore};
use anyhow::{Error, Result};

/// A graph data storage type that allows for more compact serialization.
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct CompactGraph {
    pub metadata: Metadata,
    pub nodes: Vec<Node>,
    pub edges: Vec<Edge>,
}
impl TryInto<Graph> for CompactGraph {
    type Error = Error;
    fn try_into(self) -> Result<Graph> {
        Graph::new(Some(self.metadata), self.nodes, self.edges)
    }
}
impl From<Graph> for CompactGraph {
    fn from(value: Graph) -> Self {
        Self {
            metadata: value.get_meta().to_owned(),
            nodes: value
                .all_nodes()
                .into_iter()
                .map(|x| x.to_owned())
                .collect(),
            edges: value
                .all_edges()
                .into_iter()
                .map(|x| x.to_owned())
                .collect(),
        }
    }
}