ratio_graph/
serialize.rs

1//! # Serialization module
2//!
3//! ## License
4//!
5//! This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
6//! If a copy of the MPL was not distributed with this file,
7//! You can obtain one at <https://mozilla.org/MPL/2.0/>.
8//!
9//! **Code examples both in the docstrings and rendered documentation are free to use.**
10
11use crate::{Edge, EdgeStore, Graph, Meta, Metadata, Node, NodeStore};
12
13/// A graph data storage type that allows for more compact serialization.
14#[derive(Clone, Debug, Default, PartialEq)]
15#[cfg_attr(
16    feature = "serde",
17    derive(serde::Serialize, serde::Deserialize),
18    serde(default)
19)]
20pub struct CompactGraph {
21    pub metadata: Metadata,
22    pub nodes: Vec<Node>,
23    pub edges: Vec<Edge>,
24}
25impl TryInto<Graph> for CompactGraph {
26    type Error = crate::graph::Error;
27    fn try_into(self) -> Result<Graph, Self::Error> {
28        Graph::new(Some(self.metadata), self.nodes, self.edges)
29    }
30}
31impl From<Graph> for CompactGraph {
32    fn from(value: Graph) -> Self {
33        Self {
34            metadata: value.get_meta().to_owned(),
35            nodes: value.all_nodes().map(|x| x.to_owned()).collect(),
36            edges: value.all_edges().map(|x| x.to_owned()).collect(),
37        }
38    }
39}