Skip to main content

cimstructs/
base.rs

1use std::collections::HashMap;
2
3pub trait CimElement: Send + Sync {
4    fn mrid(&self) -> &str;
5    fn type_name(&self) -> &'static str;
6    fn as_any(&self) -> &dyn std::any::Any;
7    fn to_json_value(&self) -> serde_json::Value;
8    fn to_block(&self) -> RdfBlock;
9}
10
11#[derive(Debug, Clone)]
12pub enum FieldValue {
13    Text(String),
14    /// Repeated text element within one parsed element (e.g. the multiple
15    /// `md:Model.profile` entries of a combined EQ+SC file header).
16    TextList(Vec<String>),
17    Resource(String),
18    ResourceList(Vec<String>),
19}
20
21#[derive(Debug, Clone)]
22pub struct RdfBlock {
23    pub type_name: String,
24    pub mrid: String,
25    pub fields: HashMap<String, FieldValue>,
26    /// Field names (local XML element name) that were assigned more than once
27    /// within a single parsed element, indicating a MaxCount violation.
28    pub duplicate_fields: std::collections::HashSet<String>,
29}
30
31impl Default for RdfBlock {
32    fn default() -> Self {
33        Self {
34            type_name: String::new(),
35            mrid: String::new(),
36            fields: HashMap::new(),
37            duplicate_fields: std::collections::HashSet::new(),
38        }
39    }
40}
41
42impl RdfBlock {
43    pub fn merge_from(&mut self, other: &RdfBlock) {
44        for (k, v) in &other.fields {
45            match v {
46                FieldValue::ResourceList(new_list) => {
47                    match self.fields.get_mut(k) {
48                        Some(FieldValue::ResourceList(existing)) => {
49                            existing.extend(new_list.iter().cloned())
50                        }
51                        _ => {
52                            self.fields.insert(k.clone(), v.clone());
53                        }
54                    }
55                }
56                _ => {
57                    self.fields.insert(k.clone(), v.clone());
58                }
59            }
60        }
61    }
62}
63
64/// A reference to another CIM object by MRID.
65#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
66#[serde(transparent)]
67pub struct MridRef {
68    pub mrid: String,
69}
70
71/// A reference to a CIM enum value by URI.
72#[derive(Debug, Default, Clone, serde::Serialize, serde::Deserialize)]
73#[serde(transparent)]
74pub struct UriRef {
75    pub uri: String,
76}