Newer
Older
use serde_derive::{Deserialize, Serialize};

Udo Eisenbarth
committed
use std::collections::HashMap;
error::{OpmResult, OpossumError},
#[derive(Default, Serialize, Deserialize, Debug, Clone)]
pub fn create(&mut self, name: &str, value: Property) -> OpmResult<()> {
if self.props.insert(name.into(), value).is_some() {
Err(OpossumError::Properties(format!(
"property {} already created",
name
)))
Ok(())
}
}
pub fn set(&mut self, name: &str, value: Property) -> OpmResult<()> {
if self.props.insert(name.into(), value).is_none() {
Err(OpossumError::Properties(format!(
"property {} does not exist",
name
)))
} else {
Ok(())
pub fn iter(&self) -> std::collections::hash_map::Iter<'_, String, Property> {
self.props.iter()
}
pub fn contains(&self, key: &str) -> bool {
self.props.contains_key(key)
}
pub fn get(&self, name: &str) -> Option<&Property> {
self.props.get(name)
}
pub fn get_bool(&self, name: &str) -> OpmResult<Option<bool>> {
if let Some(property) = self.props.get(name) {
if let Proptype::Bool(value) = property.prop {
Ok(Some(value))
} else {
Err(OpossumError::Other("not a bool property".into()))
}
} else {
Ok(None)
}

Udo Eisenbarth
committed
#[derive(Debug, Serialize, Deserialize, Clone)]

Udo Eisenbarth
committed
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
impl From<bool> for Property {
fn from(value: bool) -> Self {
Property {
prop: Proptype::Bool(value),
}
}
}
impl From<f64> for Property {
fn from(value: f64) -> Self {
Property {
prop: Proptype::F64(value),
}
}
}
impl From<String> for Property {
fn from(value: String) -> Self {
Property {
prop: Proptype::String(value),
}
}
}
impl From<&str> for Property {
fn from(value: &str) -> Self {
Property {
prop: Proptype::String(value.to_string()),
}
}
}
impl From<i32> for Property {
fn from(value: i32) -> Self {
Property {
prop: Proptype::I32(value),
}
}
}
impl From<OpticGraph> for Property {
fn from(value: OpticGraph) -> Self {
Property {
prop: Proptype::OpticGraph(value),
}
}
}
impl From<FilterType> for Property {
fn from(value: FilterType) -> Self {
Property {
prop: Proptype::FilterType(value),
}
}
}
impl From<SpectrometerType> for Property {
fn from(value: SpectrometerType) -> Self {
Property {
prop: Proptype::SpectrometerType(value),
}
}
}
impl From<Metertype> for Property {
fn from(value: Metertype) -> Self {
Property {
prop: Proptype::Metertype(value),
}
}
}
impl From<PortMap> for Property {
fn from(value: PortMap) -> Self {
Property {
prop: Proptype::GroupPortMap(value),
}
}
}
impl From<Option<LightData>> for Property {
fn from(value: Option<LightData>) -> Self {
Property {
impl From<Uuid> for Property {
fn from(value: Uuid) -> Self {
Property {
prop: Proptype::Uuid(value),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
String(String),
I32(i32),
F64(f64),
Bool(bool),
LightData(Option<LightData>),
OpticGraph(OpticGraph),
FilterType(FilterType),
SpectrometerType(SpectrometerType),