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

Add properties structure

parent 9faa7d2a
No related branches found
No related tags found
2 merge requests!11Resolve "Implement property system for nodes",!7Draft: Resolve "Implement rudimental serialization of OpticScenery"
Pipeline #7445 passed
...@@ -21,4 +21,6 @@ pub mod error; ...@@ -21,4 +21,6 @@ pub mod error;
pub mod spectrum; pub mod spectrum;
pub mod properties;
pub use optic_scenery::OpticScenery; pub use optic_scenery::OpticScenery;
...@@ -9,6 +9,7 @@ use crate::{ ...@@ -9,6 +9,7 @@ use crate::{
lightdata::{DataEnergy, LightData}, lightdata::{DataEnergy, LightData},
optic_ports::OpticPorts, optic_ports::OpticPorts,
optical::{LightResult, Optical}, optical::{LightResult, Optical},
properties::{Properties, Property, Proptype},
spectrum::{merge_spectra, Spectrum}, spectrum::{merge_spectra, Spectrum},
}; };
...@@ -26,13 +27,40 @@ type Result<T> = std::result::Result<T, OpossumError>; ...@@ -26,13 +27,40 @@ type Result<T> = std::result::Result<T, OpossumError>;
/// - `out2_trans2_refl1` /// - `out2_trans2_refl1`
pub struct BeamSplitter { pub struct BeamSplitter {
ratio: f64, ratio: f64,
props: Properties,
} }
fn create_default_props() -> Properties {
let mut props = Properties::default();
props.set(
"name",
Property {
prop: Proptype::String("beam splitter".into()),
},
);
props.set(
"ratio",
Property {
prop: Proptype::F64(0.5),
},
);
props
}
impl BeamSplitter { impl BeamSplitter {
/// Creates a new [`BeamSplitter`] with a given splitting ratio. /// Creates a new [`BeamSplitter`] with a given splitting ratio.
pub fn new(ratio: f64) -> Result<Self> { pub fn new(ratio: f64) -> Result<Self> {
if (0.0..=1.0).contains(&ratio) { if (0.0..=1.0).contains(&ratio) {
Ok(Self { ratio }) let mut props = create_default_props();
props.set(
"ratio",
Property {
prop: Proptype::F64(ratio),
},
);
Ok(Self {
ratio,
props: props,
})
} else { } else {
Err(OpossumError::Other( Err(OpossumError::Other(
"splitting ration must be within (0.0..1.0)".into(), "splitting ration must be within (0.0..1.0)".into(),
...@@ -115,7 +143,11 @@ impl BeamSplitter { ...@@ -115,7 +143,11 @@ impl BeamSplitter {
impl Default for BeamSplitter { impl Default for BeamSplitter {
/// Create a 50:50 beamsplitter. /// Create a 50:50 beamsplitter.
fn default() -> Self { fn default() -> Self {
Self { ratio: 0.5 } let props= create_default_props();
Self {
ratio: 0.5,
props: props,
}
} }
} }
impl Optical for BeamSplitter { impl Optical for BeamSplitter {
...@@ -143,6 +175,16 @@ impl Optical for BeamSplitter { ...@@ -143,6 +175,16 @@ impl Optical for BeamSplitter {
)), )),
} }
} }
fn properties(&self) -> Properties {
self.props.clone()
}
fn set_property(&mut self, name: &str, prop: Property) -> Result<()> {
if self.props.set(name, prop).is_none() {
Err(OpossumError::Other("property not defined".into()))
} else {
Ok(())
}
}
} }
impl Dottable for BeamSplitter { impl Dottable for BeamSplitter {
......
...@@ -7,6 +7,7 @@ use crate::dottable::Dottable; ...@@ -7,6 +7,7 @@ use crate::dottable::Dottable;
use crate::error::OpossumError; use crate::error::OpossumError;
use crate::optic_ports::OpticPorts; use crate::optic_ports::OpticPorts;
use crate::optical::{LightResult, Optical}; use crate::optical::{LightResult, Optical};
use crate::properties::{Properties, Proptype, Property};
type Result<T> = std::result::Result<T, OpossumError>; type Result<T> = std::result::Result<T, OpossumError>;
...@@ -24,22 +25,29 @@ type Result<T> = std::result::Result<T, OpossumError>; ...@@ -24,22 +25,29 @@ type Result<T> = std::result::Result<T, OpossumError>;
pub struct Dummy { pub struct Dummy {
is_inverted: bool, is_inverted: bool,
name: String, name: String,
props: Properties
} }
impl Default for Dummy { impl Default for Dummy {
fn default() -> Self { fn default() -> Self {
let mut props= Properties::default();
props.set("name", Property{prop: Proptype::String("udo".into())});
Self { Self {
is_inverted: Default::default(), is_inverted: Default::default(),
name: String::from("dummy"), name: String::from("dummy"),
props: props
} }
} }
} }
impl Dummy { impl Dummy {
/// Creates a new [`Dummy`] with a given name. /// Creates a new [`Dummy`] with a given name.
pub fn new(name: &str) -> Self { pub fn new(name: &str) -> Self {
let mut props= Properties::default();
props.set("name", Property{prop: Proptype::String(name.into())});
Self { Self {
name: name.to_owned(), name: name.to_owned(),
is_inverted: false, is_inverted: false,
props: props
} }
} }
} }
...@@ -84,6 +92,9 @@ impl Optical for Dummy { ...@@ -84,6 +92,9 @@ impl Optical for Dummy {
fn inverted(&self) -> bool { fn inverted(&self) -> bool {
self.is_inverted self.is_inverted
} }
fn properties(&self) -> Properties {
self.props.clone()
}
} }
impl Dottable for Dummy {} impl Dottable for Dummy {}
......
...@@ -7,6 +7,7 @@ use crate::error::OpossumError; ...@@ -7,6 +7,7 @@ use crate::error::OpossumError;
use crate::lightdata::LightData; use crate::lightdata::LightData;
use crate::nodes::NodeGroup; use crate::nodes::NodeGroup;
use crate::optic_ports::OpticPorts; use crate::optic_ports::OpticPorts;
use crate::properties::{Properties, Property};
use core::fmt::Debug; use core::fmt::Debug;
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
...@@ -68,6 +69,12 @@ pub trait Optical: Dottable + erased_serde::Serialize { ...@@ -68,6 +69,12 @@ pub trait Optical: Dottable + erased_serde::Serialize {
fn as_group(&self) -> Result<&NodeGroup> { fn as_group(&self) -> Result<&NodeGroup> {
Err(OpossumError::Other("cannot cast to group".into())) Err(OpossumError::Other("cannot cast to group".into()))
} }
fn properties(&self) -> Properties {
Properties::default()
}
fn set_property(&mut self, _name: &str, _prop: Property) -> Result<()> {
Ok(())
}
} }
impl Debug for dyn Optical { impl Debug for dyn Optical {
...@@ -86,7 +93,7 @@ impl Serialize for OpticRef { ...@@ -86,7 +93,7 @@ impl Serialize for OpticRef {
{ {
let mut node = serializer.serialize_struct("node", 1)?; let mut node = serializer.serialize_struct("node", 1)?;
node.serialize_field("type", self.0.borrow().node_type())?; node.serialize_field("type", self.0.borrow().node_type())?;
node.serialize_field("ports", &self.0.borrow().ports())?; node.serialize_field("properties", &self.0.borrow().properties())?;
node.end() node.end()
} }
} }
......
use std::collections::HashMap;
use serde::Serialize;
use serde_derive::Serialize;
#[derive(Default, Serialize, Debug, Clone)]
pub struct Properties {
props: HashMap<String, Property>
}
impl Properties {
pub fn set(&mut self, name: &str, value: Property) -> Option<()> {
if self.props.insert(name.into(), value).is_some() {
Some(())
} else {
None
}
}
pub fn get(&self, name: &str) -> Option<&Property> {
self.props.get(name.into())
}
}
#[derive(Debug, Clone)]
pub struct Property {
pub prop: Proptype
}
impl Serialize for Property {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer {
serializer.serialize_newtype_struct("hallo", &self.prop)
}
}
#[non_exhaustive]
#[derive(Serialize, Debug, Clone)]
pub enum Proptype {
String(String),
I32(i32),
F64(f64)
}
\ No newline at end of file
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