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

Move OptifRef from optical to its own module.

Update dependent code accordingly.
parent fd9fac12
No related branches found
No related tags found
1 merge request!34Add Uuid to OpticRef
......@@ -31,6 +31,7 @@ pub use optic_scenery::OpticScenery;
/// Module for handling the OPOSSUM CLI
pub mod console;
mod optic_graph;
mod optic_ref;
/// Return the version information of the currently built OPOSSUM executable.
///
......
......@@ -32,7 +32,7 @@ pub use spectrometer::SpectrometerType;
use crate::error::OpmResult;
use crate::error::OpossumError;
use crate::optical::OpticRef;
use crate::optic_ref::OpticRef;
pub fn create_node_ref(node_type: &str) -> OpmResult<OpticRef> {
match node_type {
......
......@@ -5,7 +5,8 @@ use crate::analyzer::AnalyzerType;
use crate::dottable::Dottable;
use crate::error::{OpmResult, OpossumError};
use crate::optic_ports::OpticPorts;
use crate::optical::{LightResult, OpticRef, Optical};
use crate::optic_ref::OpticRef;
use crate::optical::{LightResult, Optical};
use crate::properties::{Properties, Property, Proptype};
#[derive(Debug)]
......
......@@ -6,10 +6,11 @@ use serde::{
};
use std::{cell::RefCell, rc::Rc};
use crate::optic_ref::OpticRef;
use crate::{
error::{OpmResult, OpossumError},
light::Light,
optical::{OpticRef, Optical},
optical::Optical,
};
#[derive(Debug, Default, Clone)]
......
use std::{cell::RefCell, rc::Rc};
use serde::{
de::{self, MapAccess, SeqAccess, Visitor},
ser::SerializeStruct,
Deserialize, Serialize,
};
use crate::{nodes::create_node_ref, optical::Optical, properties::Properties};
#[derive(Debug, Clone)]
pub struct OpticRef(pub Rc<RefCell<dyn Optical>>);
impl Serialize for OpticRef {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut node = serializer.serialize_struct("node", 1)?;
node.serialize_field("type", self.0.borrow().node_type())?;
node.serialize_field("properties", &self.0.borrow().properties())?;
node.end()
}
}
impl<'de> Deserialize<'de> for OpticRef {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
enum Field {
NodeType,
Properties,
}
const FIELDS: &[&str] = &["type", "properties"];
impl<'de> Deserialize<'de> for Field {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct FieldVisitor;
impl<'de> Visitor<'de> for FieldVisitor {
type Value = Field;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("`type` or `properties`")
}
fn visit_str<E>(self, value: &str) -> std::result::Result<Field, E>
where
E: de::Error,
{
match value {
"type" => Ok(Field::NodeType),
"properties" => Ok(Field::Properties),
_ => Err(de::Error::unknown_field(value, FIELDS)),
}
}
}
deserializer.deserialize_identifier(FieldVisitor)
}
}
struct OpticRefVisitor;
impl<'de> Visitor<'de> for OpticRefVisitor {
type Value = OpticRef;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a struct OpticRef")
}
fn visit_seq<A>(self, mut seq: A) -> std::result::Result<OpticRef, A::Error>
where
A: SeqAccess<'de>,
{
let node_type = seq
.next_element()?
.ok_or_else(|| de::Error::invalid_length(0, &self))?;
let properties = seq
.next_element()?
.ok_or_else(|| de::Error::invalid_length(1, &self))?;
let node =
create_node_ref(node_type).map_err(|e| de::Error::custom(e.to_string()))?;
node.0
.borrow_mut()
.set_properties(&properties)
.map_err(|e| de::Error::custom(e.to_string()))?;
Ok(node)
}
fn visit_map<A>(self, mut map: A) -> std::result::Result<OpticRef, A::Error>
where
A: MapAccess<'de>,
{
let mut node_type = None;
let mut properties = None;
while let Some(key) = map.next_key()? {
match key {
Field::NodeType => {
if node_type.is_some() {
return Err(de::Error::duplicate_field("type"));
}
node_type = Some(map.next_value()?);
}
Field::Properties => {
if properties.is_some() {
return Err(de::Error::duplicate_field("properties"));
}
properties = Some(map.next_value::<Properties>()?);
}
}
}
let node_type = node_type.ok_or_else(|| de::Error::missing_field("type"))?;
let properties =
properties.ok_or_else(|| de::Error::missing_field("properties"))?;
let node =
create_node_ref(node_type).map_err(|e| de::Error::custom(e.to_string()))?;
node.0
.borrow_mut()
.set_properties(&properties)
.map_err(|e| de::Error::custom(e.to_string()))?;
Ok(node)
}
}
deserializer.deserialize_struct("OpticRef", FIELDS, OpticRefVisitor)
}
}
......@@ -10,7 +10,8 @@ use crate::light::Light;
use crate::lightdata::LightData;
use crate::nodes::NodeGroup;
use crate::optic_graph::OpticGraph;
use crate::optical::{LightResult, OpticRef, Optical};
use crate::optic_ref::OpticRef;
use crate::optical::{LightResult, Optical};
use crate::properties::{Properties, Proptype};
use chrono::Local;
use petgraph::algo::*;
......
use serde_json::json;
use crate::analyzer::AnalyzerType;
use crate::dottable::Dottable;
use crate::error::{OpmResult, OpossumError};
use crate::lightdata::LightData;
use crate::nodes::{create_node_ref, NodeGroup};
use crate::nodes::NodeGroup;
use crate::optic_ports::OpticPorts;
use crate::properties::{Properties, Property};
use core::fmt::Debug;
use serde::de::{self, Deserialize, MapAccess, SeqAccess, Visitor};
use serde::ser::SerializeStruct;
use serde::Serialize;
use serde_json::json;
use std::cell::RefCell;
use std::collections::HashMap;
use std::path::Path;
use std::rc::Rc;
pub type LightResult = HashMap<String, Option<LightData>>;
/// This is the basic trait that must be implemented by all concrete optical components.
......@@ -95,123 +92,3 @@ impl Debug for dyn Optical {
write!(f, "{} ({})", self.name(), self.node_type())
}
}
#[derive(Debug, Clone)]
pub struct OpticRef(pub Rc<RefCell<dyn Optical>>);
impl Serialize for OpticRef {
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
let mut node = serializer.serialize_struct("node", 1)?;
node.serialize_field("type", self.0.borrow().node_type())?;
node.serialize_field("properties", &self.0.borrow().properties())?;
node.end()
}
}
impl<'de> Deserialize<'de> for OpticRef {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
enum Field {
NodeType,
Properties,
}
const FIELDS: &[&str] = &["type", "properties"];
impl<'de> Deserialize<'de> for Field {
fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct FieldVisitor;
impl<'de> Visitor<'de> for FieldVisitor {
type Value = Field;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("`type` or `properties`")
}
fn visit_str<E>(self, value: &str) -> std::result::Result<Field, E>
where
E: de::Error,
{
match value {
"type" => Ok(Field::NodeType),
"properties" => Ok(Field::Properties),
_ => Err(de::Error::unknown_field(value, FIELDS)),
}
}
}
deserializer.deserialize_identifier(FieldVisitor)
}
}
struct OpticRefVisitor;
impl<'de> Visitor<'de> for OpticRefVisitor {
type Value = OpticRef;
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
formatter.write_str("a struct OpticRef")
}
fn visit_seq<A>(self, mut seq: A) -> std::result::Result<OpticRef, A::Error>
where
A: SeqAccess<'de>,
{
let node_type = seq
.next_element()?
.ok_or_else(|| de::Error::invalid_length(0, &self))?;
let properties = seq
.next_element()?
.ok_or_else(|| de::Error::invalid_length(1, &self))?;
let node =
create_node_ref(node_type).map_err(|e| de::Error::custom(e.to_string()))?;
node.0
.borrow_mut()
.set_properties(&properties)
.map_err(|e| de::Error::custom(e.to_string()))?;
Ok(node)
}
fn visit_map<A>(self, mut map: A) -> std::result::Result<OpticRef, A::Error>
where
A: MapAccess<'de>,
{
let mut node_type = None;
let mut properties = None;
while let Some(key) = map.next_key()? {
match key {
Field::NodeType => {
if node_type.is_some() {
return Err(de::Error::duplicate_field("type"));
}
node_type = Some(map.next_value()?);
}
Field::Properties => {
if properties.is_some() {
return Err(de::Error::duplicate_field("properties"));
}
properties = Some(map.next_value::<Properties>()?);
}
}
}
let node_type = node_type.ok_or_else(|| de::Error::missing_field("type"))?;
let properties =
properties.ok_or_else(|| de::Error::missing_field("properties"))?;
let node =
create_node_ref(node_type).map_err(|e| de::Error::custom(e.to_string()))?;
node.0
.borrow_mut()
.set_properties(&properties)
.map_err(|e| de::Error::custom(e.to_string()))?;
Ok(node)
}
}
deserializer.deserialize_struct("OpticRef", FIELDS, OpticRefVisitor)
}
}
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