Skip to main content

cimstructs/
SvTapStep.rs

1/// State variable for transformer tap step.
2#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
3pub struct SvTapStep {
4    pub id: String,
5    /// The tap changer associated with the tap step state.
6    #[serde(skip_serializing_if = "Option::is_none")]
7    pub tap_changer: Option<super::base::MridRef>,
8    /// The floating point tap position. This is not the tap ratio, but rather the tap step position as defined by the related tap changer model and normally is constrained to be within the range of minimum and maximum tap positions.
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub position: Option<f64>,
11}
12impl crate::base::CimElement for SvTapStep {
13    fn mrid(&self) -> &str { &self.id }
14    fn type_name(&self) -> &'static str { "SvTapStep" }
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: "SvTapStep".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.tap_changer {
27            block.fields.insert("SvTapStep.TapChanger".into(), crate::base::FieldValue::Resource(v.mrid.clone()));
28        }
29        if let Some(v) = self.position {
30            block.fields.insert("SvTapStep.position".into(), crate::base::FieldValue::Text(v.to_string()));
31        }
32        block
33    }
34}
35
36impl SvTapStep {
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                "SvTapStep.TapChanger" => {
43                    if let crate::base::FieldValue::Resource(sv) = val {
44                        obj.tap_changer = Some(crate::base::MridRef { mrid: sv.clone() });
45                    }
46                }
47                "SvTapStep.position" => {
48                    match val {
49                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.position = Some(v); } }
50                        crate::base::FieldValue::TextList(svs) => {
51                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.position = Some(v); } }
52                        }
53                        _ => {}
54                    }
55                }
56                _ => {}
57            }
58        }
59        obj
60    }
61}