oca_bundle/state/attribute.rs
1use isolang::Language;
2pub use oca_ast::ast::AttributeType;
3use oca_ast::ast::{NestedAttrType, NestedValue};
4use serde::{Deserialize, Serialize};
5use std::collections::HashMap;
6
7pub type OverlayName = String;
8// use crate::state::{encoding::Encoding, entries::EntriesElement, entry_codes::EntryCodes};
9#[derive(Serialize, Deserialize, Debug, Clone)]
10pub struct Attribute {
11 pub name: String,
12 #[serde(rename = "type")]
13 pub attribute_type: Option<NestedAttrType>,
14 pub properties: Option<HashMap<OverlayName, NestedValue>>,
15}
16
17impl Default for Attribute {
18 fn default() -> Self {
19 Self::new("".to_string())
20 }
21}
22
23impl Attribute {
24 pub fn new(name: String) -> Attribute {
25 Attribute {
26 name,
27 attribute_type: None,
28 properties: None,
29 }
30 }
31
32 pub fn set_attribute_type(&mut self, attribute_type: NestedAttrType) {
33 self.attribute_type = Some(attribute_type);
34 }
35
36 // Merge assumption is that if `other` is not None then it would overwrite `self` or would be concatenated with `self`
37 pub fn merge(&mut self, other: &Attribute) {
38 if self.name != other.name {
39 panic!("Cannot merge attributes with different names");
40 } else if other.attribute_type.is_some() {
41 self.attribute_type.clone_from(&other.attribute_type);
42 }
43 }
44
45 // fn merge_entries(&mut self, other: &Attribute) {
46 // if self.entries.is_none() {
47 // self.entries.clone_from(&other.entries);
48 // } else if let Some(entries) = &other.entries {
49 // for (lang, entry) in entries {
50 // self.entries.as_mut().unwrap().insert(*lang, entry.clone());
51 // }
52 // }
53 // }
54 //
55 // fn merge_labels(&mut self, other: &Attribute) {
56 // if self.labels.is_none() {
57 // self.labels.clone_from(&other.labels)
58 // } else if let Some(labels) = &other.labels {
59 // for (lang, label) in labels {
60 // self.labels.as_mut().unwrap().insert(*lang, label.clone());
61 // }
62 // }
63 // }
64
65 // pub fn add_mapping(mut self, mapping: String) -> AttributeBuilder {
66 // self.attribute.mapping = Some(mapping);
67 // self
68 // }
69
70 // pub fn add_entry_codes_mapping(mut self, mapping: Vec<String>) -> AttributeBuilder {
71 // self.attribute.entry_codes_mapping = Some(mapping);
72 // self
73 // }
74}
75
76#[derive(Serialize, Deserialize, Debug, Clone)]
77pub struct Entry {
78 pub id: String,
79 pub translations: HashMap<Language, String>,
80}
81
82impl Entry {
83 pub fn new(id: String, translations: HashMap<Language, String>) -> Entry {
84 Entry { id, translations }
85 }
86}
87
88/*
89#[derive(Serialize, Deserialize, Debug, Clone)]
90pub enum Entries {
91 Sai(HashMap<Language, String>),
92 Object(Vec<Entry>),
93}
94*/