oca_bundle_semantics/state/oca/overlay/
entry.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
use crate::state::{attribute::Attribute, entries::EntriesElement, oca::Overlay};
use isolang::Language;
use oca_ast_semantics::ast::OverlayType;
use said::derivation::HashFunctionCode;
use said::{sad::SerializationFormats, sad::SAD};
use serde::{ser::SerializeMap, Deserialize, Serialize, Serializer};
use std::any::Any;
use std::collections::HashMap;

pub trait Entries {
    fn set_entry(&mut self, l: Language, entry: EntriesElement);
}

impl Entries for Attribute {
    fn set_entry(&mut self, l: Language, entry: EntriesElement) {
        if let Some(entries) = &mut self.entries {
            entries.insert(l, entry);
        } else {
            let mut entries = HashMap::new();
            entries.insert(l, entry);
            self.entries = Some(entries);
        }
    }
}

pub fn serialize_attributes<S>(
    attributes: &HashMap<String, EntriesElement>,
    s: S,
) -> Result<S::Ok, S::Error>
where
    S: Serializer,
{
    use std::collections::BTreeMap;

    let mut ser = s.serialize_map(Some(attributes.len()))?;
    let sorted_attributes: BTreeMap<_, _> = attributes.iter().collect();
    for (k, v) in sorted_attributes {
        ser.serialize_entry(k, v)?;
    }
    ser.end()
}

#[derive(SAD, Serialize, Deserialize, Debug, Clone)]
pub struct EntryOverlay {
    #[said]
    #[serde(rename = "d")]
    said: Option<said::SelfAddressingIdentifier>,
    capture_base: Option<said::SelfAddressingIdentifier>,
    #[serde(rename = "type")]
    overlay_type: OverlayType,
    language: Language,
    #[serde(serialize_with = "serialize_attributes")]
    pub attribute_entries: HashMap<String, EntriesElement>,
}

impl Overlay for EntryOverlay {
    fn as_any(&self) -> &dyn Any {
        self
    }
    fn capture_base(&self) -> &Option<said::SelfAddressingIdentifier> {
        &self.capture_base
    }
    fn set_capture_base(&mut self, said: &said::SelfAddressingIdentifier) {
        self.capture_base = Some(said.clone());
    }
    fn said(&self) -> &Option<said::SelfAddressingIdentifier> {
        &self.said
    }
    fn overlay_type(&self) -> &OverlayType {
        &self.overlay_type
    }
    fn language(&self) -> Option<&Language> {
        Some(&self.language)
    }
    fn attributes(&self) -> Vec<&String> {
        self.attribute_entries.keys().collect::<Vec<&String>>()
    }

    fn add(&mut self, attribute: &Attribute) {
        if let Some(entries) = &attribute.entries {
            if let Some(tr) = entries.get(&self.language) {
                self.attribute_entries
                    .insert(attribute.name.clone(), tr.clone());
            }
        }
    }
}
impl EntryOverlay {
    pub fn new(lang: Language) -> EntryOverlay {
        EntryOverlay {
            capture_base: None,
            said: None,
            overlay_type: OverlayType::Entry,
            language: lang,
            attribute_entries: HashMap::new(),
        }
    }
}