Skip to content
Snippets Groups Projects
Commit 125e9a7c authored by Udo Eisenbarth's avatar Udo Eisenbarth :speech_balloon:
Browse files

Improve serialization of edges

parent ff0b259b
No related branches found
No related tags found
No related merge requests found
Pipeline #7510 passed
......@@ -8,7 +8,7 @@ use crate::light::Light;
use crate::lightdata::LightData;
use crate::nodes::NodeGroup;
use crate::optical::{LightResult, OpticRef, Optical, OpticGraph};
use crate::properties::Properties;
use crate::properties::{Properties, Property, Proptype};
use petgraph::algo::*;
use petgraph::prelude::{DiGraph, NodeIndex};
use petgraph::visit::EdgeRef;
......@@ -39,13 +39,29 @@ type Result<T> = std::result::Result<T, OpossumError>;
/// }
///
/// ```
#[derive(Default, Debug, Clone)]
#[derive(Debug, Clone)]
pub struct OpticScenery {
g: DiGraph<OpticRef, Light>,
description: String,
props: Properties,
}
fn create_default_props() -> Properties {
let mut props = Properties::default();
props.set(
"description",
Property {
prop: Proptype::String("".into()),
},
);
props
}
impl Default for OpticScenery {
fn default() -> Self {
Self { g: Default::default(), description: Default::default(), props: create_default_props() }
}
}
impl OpticScenery {
/// Creates a new (empty) [`OpticScenery`].
pub fn new() -> Self {
......
use petgraph::prelude::DiGraph;
use petgraph::stable_graph::NodeIndex;
use serde::ser::SerializeStruct;
use serde::Serialize;
......@@ -87,13 +88,31 @@ pub struct OpticGraph(pub DiGraph<OpticRef, Light>);
impl Serialize for OpticGraph {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer {
let g=self.0.clone();
let mut graph=serializer.serialize_struct("graph", 2)?;
let nodes=g.node_weights().map(|n| n.to_owned()).collect::<Vec<OpticRef>>();
S: serde::Serializer,
{
let g = self.0.clone();
let mut graph = serializer.serialize_struct("graph", 2)?;
let nodes = g
.node_weights()
.map(|n| n.to_owned())
.collect::<Vec<OpticRef>>();
graph.serialize_field("nodes", &nodes)?;
let edges=g.edge_weights().map(|n| (n.src_port(),n.target_port()).to_owned()).collect::<Vec<(&str,&str)>>();
graph.serialize_field("edges", &edges)?;
let edgeidx = g
.edge_indices()
.map(|e| {
(
g.edge_endpoints(e).unwrap().0,
g.edge_endpoints(e).unwrap().1,
g.edge_weight(e).unwrap().src_port(),
g.edge_weight(e).unwrap().target_port(),
)
})
.collect::<Vec<(NodeIndex, NodeIndex, &str, &str)>>();
// let edges = g
// .edge_weights()
// .map(|n| (n.src_port(), n.target_port()).to_owned())
// .collect::<Vec<(&str, &str)>>();
graph.serialize_field("edges", &edgeidx)?;
graph.end()
}
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment