Skip to main content

cimstructs/
CurveData.rs

1/// Multi-purpose data points for defining a curve. The use of this generic class is discouraged if a more specific class can be used to specify the X and Y axis values along with their specific data types.
2#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
3pub struct CurveData {
4    pub id: String,
5    /// The curve of this curve data point.
6    #[serde(skip_serializing_if = "Option::is_none")]
7    pub curve: Option<super::base::MridRef>,
8    /// The data value of the X-axis variable, depending on the X-axis units.
9    #[serde(skip_serializing_if = "Option::is_none")]
10    pub xvalue: Option<f64>,
11    /// The data value of the first Y-axis variable, depending on the Y-axis units.
12    #[serde(skip_serializing_if = "Option::is_none")]
13    pub y1value: Option<f64>,
14    /// The data value of the second Y-axis variable (if present), depending on the Y-axis units.
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub y2value: Option<f64>,
17}
18impl crate::base::CimElement for CurveData {
19    fn mrid(&self) -> &str { &self.id }
20    fn type_name(&self) -> &'static str { "CurveData" }
21    fn as_any(&self) -> &dyn std::any::Any { self }
22    fn to_json_value(&self) -> serde_json::Value {
23        serde_json::to_value(self).unwrap_or(serde_json::Value::Null)
24    }
25    fn to_block(&self) -> crate::base::RdfBlock {
26        let mut block = crate::base::RdfBlock {
27            type_name: "CurveData".to_string(),
28            mrid: self.id.clone(),
29            fields: std::collections::HashMap::new(),
30            duplicate_fields: std::collections::HashSet::new(),
31        };
32        if let Some(ref v) = self.curve {
33            block.fields.insert("CurveData.Curve".into(), crate::base::FieldValue::Resource(v.mrid.clone()));
34        }
35        if let Some(v) = self.xvalue {
36            block.fields.insert("CurveData.xvalue".into(), crate::base::FieldValue::Text(v.to_string()));
37        }
38        if let Some(v) = self.y1value {
39            block.fields.insert("CurveData.y1value".into(), crate::base::FieldValue::Text(v.to_string()));
40        }
41        if let Some(v) = self.y2value {
42            block.fields.insert("CurveData.y2value".into(), crate::base::FieldValue::Text(v.to_string()));
43        }
44        block
45    }
46}
47
48impl CurveData {
49    pub fn from_block(b: &crate::base::RdfBlock) -> Self {
50        let mut obj = Self::default();
51        obj.id.clone_from(&b.mrid);
52        for (key, val) in &b.fields {
53            match key.as_str() {
54                "CurveData.Curve" => {
55                    if let crate::base::FieldValue::Resource(sv) = val {
56                        obj.curve = Some(crate::base::MridRef { mrid: sv.clone() });
57                    }
58                }
59                "CurveData.xvalue" => {
60                    match val {
61                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.xvalue = Some(v); } }
62                        crate::base::FieldValue::TextList(svs) => {
63                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.xvalue = Some(v); } }
64                        }
65                        _ => {}
66                    }
67                }
68                "CurveData.y1value" => {
69                    match val {
70                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.y1value = Some(v); } }
71                        crate::base::FieldValue::TextList(svs) => {
72                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.y1value = Some(v); } }
73                        }
74                        _ => {}
75                    }
76                }
77                "CurveData.y2value" => {
78                    match val {
79                        crate::base::FieldValue::Text(sv) => { if let Ok(v) = sv.trim().parse() { obj.y2value = Some(v); } }
80                        crate::base::FieldValue::TextList(svs) => {
81                            if let Some(sv) = svs.last() { if let Ok(v) = sv.trim().parse() { obj.y2value = Some(v); } }
82                        }
83                        _ => {}
84                    }
85                }
86                _ => {}
87            }
88        }
89        obj
90    }
91}