Skip to main content

cimstructs/
SvSwitch.rs

1/// State variable for switch.
2#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
3pub struct SvSwitch {
4    pub id: String,
5    /// The switch associated with the switch state.
6    #[serde(skip_serializing_if = "Option::is_none")]
7    pub switch: Option<super::base::MridRef>,
8    /// The attribute tells if the computed state of the switch is considered open.
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub open: Option<bool>,
11}
12impl crate::base::CimElement for SvSwitch {
13    fn mrid(&self) -> &str { &self.id }
14    fn type_name(&self) -> &'static str { "SvSwitch" }
15    fn as_any(&self) -> &dyn std::any::Any { self }
16    fn to_json_value(&self) -> serde_json::Value {
17        serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
18    }
19    fn to_block(&self) -> crate::base::RdfBlock {
20        let mut block = crate::base::RdfBlock {
21            type_name: "SvSwitch".to_string(),
22            mrid: self.id.clone(),
23            fields: std::collections::HashMap::new(),
24            duplicate_fields: std::collections::HashSet::new(),
25        };
26        if let Some(ref v) = self.switch {
27            block.fields.insert("SvSwitch.Switch".into(), crate::base::FieldValue::Resource(v.mrid.clone()));
28        }
29        if let Some(v) = self.open {
30            block.fields.insert("SvSwitch.open".into(), crate::base::FieldValue::Text(v.to_string()));
31        }
32        block
33    }
34}
35
36impl SvSwitch {
37    pub fn from_block(b: &crate::base::RdfBlock) -> Self {
38        let mut obj = Self::default();
39        obj.id.clone_from(&b.mrid);
40        for (key, val) in &b.fields {
41            match key.as_str() {
42                "SvSwitch.Switch" => {
43                    if let crate::base::FieldValue::Resource(sv) = val {
44                        obj.switch = Some(crate::base::MridRef { mrid: sv.clone() });
45                    }
46                }
47                "SvSwitch.open" => {
48                    match val {
49                        crate::base::FieldValue::Text(sv) => { obj.open = Some(sv.trim() == "true"); }
50                        crate::base::FieldValue::TextList(svs) => {
51                            if let Some(sv) = svs.last() { obj.open = Some(sv.trim() == "true"); }
52                        }
53                        _ => {}
54                    }
55                }
56                _ => {}
57            }
58        }
59        obj
60    }
61}