Serialization maybe

This commit is contained in:
elvis
2025-07-09 21:44:04 +02:00
parent ed49d6fa52
commit 0fb1ca97a0
9 changed files with 119 additions and 19 deletions

View File

@ -9,3 +9,4 @@ pub mod transitions;
pub mod translator;
pub mod graph;
pub mod rsdot;
pub mod serialize;

View File

@ -0,0 +1,33 @@
use std::io;
use petgraph::Graph;
use serde::{Deserialize, Serialize};
use super::{structure::{RSlabel, RSsystem}, translator::Translator};
#[derive(Serialize, Deserialize)]
struct GraphAndTranslator {
graph: Graph<RSsystem, RSlabel>,
translator: Translator
}
pub fn sr<W>(writer: W, graph: &Graph<RSsystem, RSlabel>, translator: &Translator) -> Result<(), serde_cbor_2::Error>
where
W: io::Write,
{
serde_cbor_2::to_writer(writer,
&GraphAndTranslator {
graph: graph.clone(),
translator: translator.clone()
})
}
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))
}

View File

@ -2,12 +2,13 @@ use super::translator::IdType;
use std::collections::{BTreeSet, HashMap, VecDeque};
use std::hash::Hash;
use std::rc::Rc;
use serde::{Deserialize, Serialize};
// -----------------------------------------------------------------------------
// RSset
// -----------------------------------------------------------------------------
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct RSset {
pub identifiers: BTreeSet<IdType>,
}
@ -125,7 +126,7 @@ impl IntoIterator for RSset {
// -----------------------------------------------------------------------------
// RSreaction
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RSreaction {
pub reactants: RSset,
pub inihibitors: RSset,
@ -166,7 +167,7 @@ impl Default for RSreaction {
// -----------------------------------------------------------------------------
// RSprocess
// -----------------------------------------------------------------------------
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum RSprocess {
Nill,
RecursiveIdentifier {
@ -355,7 +356,7 @@ impl From<Vec<(Rc<RSset>, Rc<RSprocess>)>> for RSchoices {
// -----------------------------------------------------------------------------
// RSenvironment
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RSenvironment {
definitions: HashMap<IdType, RSprocess>,
}
@ -417,7 +418,7 @@ impl From<Vec<(IdType, RSprocess)>> for RSenvironment {
// -----------------------------------------------------------------------------
// RSsystem
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct RSsystem {
pub delta: Rc<RSenvironment>,
pub available_entities: RSset,
@ -483,7 +484,7 @@ impl Default for RSsystem {
// -----------------------------------------------------------------------------
// RSlabel
// -----------------------------------------------------------------------------
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct RSlabel {
pub available_entities: RSset,
pub context: RSset,

View File

@ -1,12 +1,13 @@
//! Module for translation and keeping track of strings.
use std::{cmp::max, collections::HashMap};
use serde::{Serialize, Deserialize};
/// precision for printing frequencies
static PRECISION: &usize = &2;
pub type IdType = u32;
#[derive(Clone, Debug)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Translator {
strings: HashMap<String, IdType>,
reverse: HashMap<IdType, String>,