Newer
Older
use crate::lightdata::LightData;
use crate::properties::{Properties, Property, Proptype};
/// This node represents an universal detector (so far for test / debugging purposes).
/// Any [`LightData`] coming in will be stored internally for later display / export.
///
/// ## Optical Ports
/// - Inputs
/// - `in1`
/// - Outputs
/// ## Properties
/// - `name`
/// - `inverted`
///
/// During analysis, the output port contains a replica of the input port similar to a [`Dummy`](crate::nodes::Dummy) node. This way,
/// different dectector nodes can be "stacked" or used somewhere in between arbitrary optic nodes.
}
fn create_default_props() -> Properties {
let mut props = Properties::default();
props.set("inverted", false.into());
props
}
impl Default for Detector {
fn default() -> Self {
Self {
light_data: Default::default(),
props: create_default_props(),
}
impl Detector {
/// Creates a new [`Detector`].
pub fn new(name: &str) -> Self {
let mut props = create_default_props();
props.set("name", name.into());
Self {
props,
..Default::default()
}
}
}
fn name(&self) -> &str {
if let Some(value) = self.props.get("name") {
if let Proptype::String(name) = &value.prop {
return name;
}
}
panic!("wrong format");
}
fn inverted(&self) -> bool {
self.properties().get_bool("inverted").unwrap().unwrap()
}
}
fn ports(&self) -> OpticPorts {
let mut ports = OpticPorts::new();
ports.add_input("in1").unwrap();
ports
}
fn analyze(
&mut self,
incoming_data: LightResult,
_analyzer_type: &crate::analyzer::AnalyzerType,
if let Some(data) = incoming_data.get("in1") {
self.light_data = data.clone();
Ok(HashMap::from([("out1".into(), data.clone())]))
} else {
Ok(HashMap::from([("out2".into(), None)]))
if let Some(data) = &self.light_data {
let mut file_path = PathBuf::from(report_dir);
file_path.push(format!("spectrum_{}.svg", self.name()));
data.export(&file_path)
fn is_detector(&self) -> bool {
true
}
fn properties(&self) -> &Properties {
&self.props
}
fn set_property(&mut self, name: &str, prop: Property) -> OpmResult<()> {
if self.props.set(name, prop).is_none() {
Err(OpossumError::Other("property not defined".into()))
} else {
Ok(())
}
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.light_data {
Some(data) => write!(f, "{}", data),
None => write!(f, "no data"),
}
}
}
fn node_color(&self) -> &str {
"lemonchiffon"
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#[cfg(test)]
mod test {
use super::*;
#[test]
fn default() {
let node = Detector::default();
assert_eq!(node.name(), "detector");
assert_eq!(node.node_type(), "detector");
assert_eq!(node.is_detector(), true);
assert_eq!(node.inverted(), false);
assert_eq!(node.node_color(), "lemonchiffon");
assert!(node.as_group().is_err());
}
#[test]
fn new() {
let node = Detector::new("test");
assert_eq!(node.name(), "test");
}
#[test]
fn inverted() {
let mut node = Detector::default();
node.set_property("inverted", true.into()).unwrap();
assert_eq!(node.inverted(), true)
}
#[test]
fn ports() {
let node = Detector::default();
assert_eq!(node.ports().inputs(), vec!["in1"]);
assert_eq!(node.ports().outputs(), vec!["out1"]);
}
}