Files
ReactionSystems/src/rsprocess/serialize.rs

46 lines
1.1 KiB
Rust
Raw Normal View History

2025-07-10 15:02:14 +02:00
//! Definitions for serializing and deserializing graph and translator.
//!
//! N.B. after serialization the size of the graph may be much larger than
//! before since a lot of ```Rc``` are used in ```RSsystem```.
2025-07-09 21:44:04 +02:00
use std::io;
use petgraph::Graph;
use serde::{Deserialize, Serialize};
2025-07-10 15:02:14 +02:00
use super::{structure::{RSlabel, RSsystem},
translator::Translator};
2025-07-09 21:44:04 +02:00
#[derive(Serialize, Deserialize)]
struct GraphAndTranslator {
graph: Graph<RSsystem, RSlabel>,
translator: Translator
}
2025-07-10 15:02:14 +02:00
/// Serializer for graph and translator.
pub fn sr<W>(
writer: W,
graph: &Graph<RSsystem, RSlabel>,
translator: &Translator
) -> Result<(), serde_cbor_2::Error>
2025-07-09 21:44:04 +02:00
where
W: io::Write,
{
serde_cbor_2::to_writer(writer,
&GraphAndTranslator {
graph: graph.clone(),
translator: translator.clone()
})
}
2025-07-10 15:02:14 +02:00
/// Deserializer for file that contains graph and translator.
2025-07-09 21:44:04 +02:00
pub fn dsr<R>(
reader: R
) -> Result<(Graph<RSsystem, RSlabel>, Translator), serde_cbor_2::Error>
where
R: io::Read,
{
let gat: GraphAndTranslator = serde_cbor_2::from_reader(reader)?;
Ok((gat.graph, gat.translator))
}