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
use crate::state::{attribute::Attribute, oca::OCABox, 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 Metas {
    fn add_meta(&mut self, language: Language, key: String, value: String);
}

impl Metas for OCABox {
    fn add_meta(&mut self, l: Language, key: String, value: String) {
        match &mut self.meta {
            Some(ref mut meta) => match meta.get_mut(&l) {
                Some(attr_pairs) => {
                    attr_pairs.insert(key, value);
                }
                None => {
                    let mut attr_pairs = HashMap::new();
                    attr_pairs.insert(key, value);
                    meta.insert(l, attr_pairs);
                }
            },
            None => {
                let meta = HashMap::new();
                self.meta = Some(meta);
                self.add_meta(l, key, value);
            }
        };
    }
}

pub fn serialize_attributes<S>(
    attributes: &HashMap<String, String>,
    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 MetaOverlay {
    #[said]
    #[serde(rename = "d")]
    said: Option<said::SelfAddressingIdentifier>,
    pub language: Language,
    #[serde(rename = "type")]
    overlay_type: OverlayType,
    capture_base: Option<said::SelfAddressingIdentifier>,
    #[serde(flatten, serialize_with = "serialize_attributes")]
    pub attr_pairs: HashMap<String, String>,
}

// TODO: why Overlay implements fn which are not relevant for MetaOverlay?
impl Overlay for MetaOverlay {
    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> {
        vec![]
    }

    fn add(&mut self, _attribute: &Attribute) {}
}

impl MetaOverlay {
    pub fn new(lang: Language, attr_pairs: HashMap<String, String>) -> Self {
        Self {
            capture_base: None,
            said: None,
            overlay_type: OverlayType::Meta,
            language: lang,
            attr_pairs,
        }
    }
}