Files
ReactionSystems/rsprocess/src/serialize.rs

44 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 serde::{Deserialize, Serialize};
2025-07-09 21:44:04 +02:00
2025-09-11 02:49:14 +02:00
use super::graph;
2025-07-16 00:05:14 +02:00
use super::translator::Translator;
2025-07-09 21:44:04 +02:00
#[derive(Serialize, Deserialize)]
struct GraphAndTranslator {
2025-09-11 02:49:14 +02:00
graph: graph::SystemGraph,
translator: Translator,
2025-07-09 21:44:04 +02:00
}
2025-07-10 15:02:14 +02:00
/// Serializer for graph and translator.
2025-07-11 19:36:20 +02:00
pub fn ser<W>(
2025-07-10 15:02:14 +02:00
writer: W,
graph: &graph::SystemGraph,
translator: &Translator,
2025-07-10 15:02:14 +02:00
) -> Result<(), serde_cbor_2::Error>
2025-07-09 21:44:04 +02:00
where
W: io::Write,
{
2025-09-11 02:49:14 +02:00
serde_cbor_2::to_writer(writer, &GraphAndTranslator {
graph: graph.clone(),
translator: translator.clone(),
})
2025-07-09 21:44:04 +02:00
}
2025-07-10 15:02:14 +02:00
/// Deserializer for file that contains graph and translator.
2025-09-10 22:41:40 +02:00
pub fn de<R>(
reader: R,
) -> Result<(graph::SystemGraph, Translator), serde_cbor_2::Error>
2025-07-09 21:44:04 +02:00
where
R: io::Read,
{
let gat: GraphAndTranslator = serde_cbor_2::from_reader(reader)?;
Ok((gat.graph, gat.translator))
}