oca_bundle_semantics/state/
attribute.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use super::standard::Standard;
use isolang::Language;
pub use oca_ast_semantics::ast::AttributeType;
use oca_ast_semantics::ast::NestedAttrType;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::state::{encoding::Encoding, entries::EntriesElement, entry_codes::EntryCodes};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Attribute {
    pub name: String,
    #[serde(rename = "type")]
    pub attribute_type: Option<NestedAttrType>,
    pub is_flagged: bool,
    pub labels: Option<HashMap<Language, String>>,
    pub category_labels: Option<HashMap<Language, String>>,
    pub informations: Option<HashMap<Language, String>>,
    pub entry_codes: Option<EntryCodes>,
    pub entries: Option<HashMap<Language, EntriesElement>>,
    pub mapping: Option<String>,
    pub encoding: Option<Encoding>,
    #[cfg(feature = "format_overlay")]
    pub format: Option<String>,
    pub unit: Option<String>,
    pub entry_codes_mapping: Option<Vec<String>>,
    pub condition: Option<String>,
    pub dependencies: Option<Vec<String>>,
    pub cardinality: Option<String>,
    pub conformance: Option<String>,
    pub standards: Option<Vec<Standard>>,
}

impl Default for Attribute {
    fn default() -> Self {
        Self::new("".to_string())
    }
}

impl Attribute {
    pub fn new(name: String) -> Attribute {
        Attribute {
            name,
            labels: None,
            informations: None,
            category_labels: None,
            attribute_type: None,
            is_flagged: false,
            mapping: None,
            encoding: None,
            #[cfg(feature = "format_overlay")]
            format: None,
            unit: None,
            entry_codes: None,
            entries: None,
            entry_codes_mapping: None,
            condition: None,
            dependencies: None,
            cardinality: None,
            conformance: None,
            standards: None,
        }
    }

    pub fn set_flagged(&mut self) {
        self.is_flagged = true;
    }

    pub fn set_attribute_type(&mut self, attribute_type: NestedAttrType) {
        self.attribute_type = Some(attribute_type);
    }

    // Merge assumption is that if `other` is not None then it would overwrite `self` or would be concatenated with `self`
    pub fn merge(&mut self, other: &Attribute) {
        if self.name != other.name {
            panic!("Cannot merge attributes with different names");
        } else {
            if other.attribute_type.is_some() {
                self.attribute_type.clone_from(&other.attribute_type);
            }

            self.merge_labels(other);
            self.merge_information(other);
            self.merge_category_labels(other);

            if other.mapping.is_some() {
                self.mapping.clone_from(&other.mapping);
            }

            if other.encoding.is_some() {
                self.encoding.clone_from(&other.encoding);
            }

            #[cfg(feature = "format_overlay")]
            if other.format.is_some() {
                self.format.clone_from(&other.format);
            }

            if self.unit.is_none() {
                self.unit.clone_from(&other.unit);
            }

            if self.entry_codes.is_none() {
                self.entry_codes.clone_from(&other.entry_codes);
            }

            self.merge_entries(other);

            if self.entry_codes_mapping.is_none() {
                self.entry_codes_mapping.clone_from(&other.entry_codes_mapping);
            }

            if other.condition.is_some() {
                self.condition.clone_from(&other.condition);

                if other.dependencies.is_some() {
                    self.dependencies.clone_from(&other.dependencies);
                }
            }

            if other.cardinality.is_some() {
                self.cardinality.clone_from(&other.cardinality);
            }

            if other.conformance.is_some() {
                self.conformance.clone_from(&other.conformance);
            }

            if other.standards.is_some() {
                self.standards.clone_from(&other.standards);
            }
        }
    }

    fn merge_entries(&mut self, other: &Attribute) {
        if self.entries.is_none() {
            self.entries.clone_from(&other.entries);
        } else if let Some(entries) = &other.entries {
            for (lang, entry) in entries {
                self.entries.as_mut().unwrap().insert(*lang, entry.clone());
            }
        }
    }

    fn merge_category_labels(&mut self, other: &Attribute) {
        if self.category_labels.is_none() {
            self.category_labels.clone_from(&other.category_labels);
        } else if let Some(category_labels) = &other.category_labels {
            for (lang, category_label) in category_labels {
                self.category_labels
                    .as_mut()
                    .unwrap()
                    .insert(*lang, category_label.clone());
            }
        }
    }
    fn merge_information(&mut self, other: &Attribute) {
        if self.informations.is_none() {
            self.informations.clone_from(&other.informations);
        } else if let Some(informations) = &other.informations {
            for (lang, information) in informations {
                self.informations
                    .as_mut()
                    .unwrap()
                    .insert(*lang, information.clone());
            }
        }
    }
    fn merge_labels(&mut self, other: &Attribute) {
        if self.labels.is_none() {
            self.labels.clone_from(&other.labels)
        } else if let Some(labels) = &other.labels {
            for (lang, label) in labels {
                self.labels.as_mut().unwrap().insert(*lang, label.clone());
            }
        }
    }

    // pub fn add_mapping(mut self, mapping: String) -> AttributeBuilder {
    //     self.attribute.mapping = Some(mapping);
    //     self
    // }

    // pub fn add_entry_codes_mapping(mut self, mapping: Vec<String>) -> AttributeBuilder {
    //     self.attribute.entry_codes_mapping = Some(mapping);
    //     self
    // }
}

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Entry {
    pub id: String,
    pub translations: HashMap<Language, String>,
}

impl Entry {
    pub fn new(id: String, translations: HashMap<Language, String>) -> Entry {
        Entry { id, translations }
    }
}

/*
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum Entries {
    Sai(HashMap<Language, String>),
    Object(Vec<Entry>),
}
*/