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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
use crate::path::Path;
use sede::{deserialize_rc_box_any_map, serialize_rc_box_any_map};
use serde::Deserialize;
use serde::Serialize;
use std::any::Any;
use std::collections::HashMap;
use std::fmt::{Debug, Display, Formatter};
use std::rc::Rc;
use std::str::FromStr;

pub type Result<T> = std::result::Result<T, Box<dyn std::error::Error>>;

/// Abstraction for the result of local computation.
/// It is an AST decorated with the computation value.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Export {
    #[serde(
        serialize_with = "serialize_rc_box_any_map",
        deserialize_with = "deserialize_rc_box_any_map"
    )]
    pub(crate) map: HashMap<Path, Rc<Box<dyn Any>>>,
}

#[macro_export]
macro_rules! export {
        ($($x:expr),*) => {{
            let mut temp_map = HashMap::new();
            $(
                temp_map.insert($x.0, std::rc::Rc::new(Box::new($x.1) as Box<dyn Any>));
            )*
            Export::from(temp_map)
        }};
    }

impl Export {
    /// Create new Export.
    ///
    /// # Returns
    ///
    /// The new Export.
    pub fn new() -> Self {
        Self {
            map: HashMap::new(),
        }
    }

    /// Inserts a value in the Export at the given Path.
    ///
    /// # Arguments
    ///
    /// * `path` - The Path where to insert the value.
    /// * `value` - The value to insert.
    ///
    /// # Generic Parameters
    ///
    /// * `A` - The type of the value to insert. It must have a `'static` lifetime.
    /// * `F` - The type of the function to insert.
    ///
    /// # Returns
    ///
    /// The inserted value.
    pub fn put<A: 'static, F>(&mut self, path: Path, value: F) -> A
    where
        F: Fn() -> A,
    {
        self.map.insert(path, Rc::new(Box::new(value())));
        value()
    }

    /// Returns the value at the given Path.
    ///
    /// # Arguments
    ///
    /// * `path` - The Path where to get the value.
    ///
    /// # Generic Parameters
    ///
    /// * `A` - The type of the value to get  to return. It must have a `'static` lifetime.
    ///
    /// # Returns
    ///
    /// The value at the given Path.
    pub fn get<A: 'static + FromStr + Clone>(&self, path: &Path) -> Result<A> {
        let get_result: Result<&A> = self.get_from_map::<A>(path);

        match get_result {
            Ok(any_val) => Ok(any_val.clone()),
            _ => {
                // get deserialized value
                let str_result = self.get_from_map::<String>(path);
                str_result?.parse::<A>().map_err(|_| "Cannot parse".into())
            }
        }
    }

    fn get_from_map<A>(&self, path: &Path) -> Result<&A>
    where
        A: 'static + FromStr + Clone,
    {
        self.map
            .get(path)
            .and_then(|value| value.downcast_ref::<A>())
            .ok_or("No value at the given Path".into())
    }

    /// Obtain the root value.
    ///
    /// # Generic Parameters
    ///
    /// * `A` - The type of the value to return. It must have a `'static` lifetime.
    ///
    /// # Returns
    ///
    /// The root value.
    ///
    /// # Panics
    /// * Panics if there is not a root value (a value at the empty Path).
    /// * Panics if the type of the root value is not the same as the type of the requested value.
    pub fn root<A: 'static + FromStr + Clone>(&self) -> A {
        self.get(&Path::new()).unwrap()
    }

    /// Returns the HashMap of the Export.
    ///
    /// # Returns
    ///
    /// The HashMap of the Export.
    pub fn paths(&self) -> &HashMap<Path, Rc<Box<dyn Any>>> {
        &self.map
    }
}

impl From<HashMap<Path, Rc<Box<dyn Any>>>> for Export {
    fn from(map: HashMap<Path, Rc<Box<dyn Any>>>) -> Self {
        Self { map }
    }
}

/// This private module is needed to serialize and deserialize the HashMap<Path, Rc<Box<dyn Any>>>.
mod sede {
    use crate::path::Path;
    use serde::de::Visitor;
    use serde::{Deserializer, Serialize, Serializer};
    use std::any::Any;
    use std::collections::HashMap;
    use std::rc::Rc;

    pub fn serialize_rc_box_any_map<S>(
        data: &HashMap<Path, Rc<Box<dyn Any>>>,
        serializer: S,
    ) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        // Serialize the data and wrap it in a HashMap<Path, [u8]>
        let serializable_data: HashMap<String, String> = data
            .iter()
            .map(|(key, value)| {
                let key_str = serde_json::to_string(key).unwrap();
                // if value is an i32, cast as String, otherwise panic
                if let Some(value) = value.downcast_ref::<i32>() {
                    (key_str, value.clone().to_string())
                } else if let Some(value) = value.downcast_ref::<bool>() {
                    (key_str, value.clone().to_string())
                } else if let Some(value) = value.downcast_ref::<String>() {
                    (key_str, value.clone())
                } else if let Some(value) = value.downcast_ref::<f64>() {
                    (key_str, value.clone().to_string())
                } else {
                    panic!("Cannot serialize type")
                }
            })
            .collect();
        serializable_data.serialize(serializer)
    }

    struct ExportMapVisitor;

    impl<'de> Visitor<'de> for ExportMapVisitor {
        type Value = HashMap<Path, Rc<Box<dyn Any>>>;
        fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
            formatter.write_str("a map of Paths and Any")
        }

        fn visit_map<A>(self, mut map: A) -> Result<Self::Value, A::Error>
        where
            A: serde::de::MapAccess<'de>,
        {
            let mut result = HashMap::new();
            while let Some((key, value)) = map.next_entry::<String, String>()? {
                let path: Path = serde_json::from_str(&key).unwrap();
                let value: Rc<Box<dyn Any>> = Rc::new(Box::new(value) as Box<dyn Any>);
                result.insert(path, value);
            }
            Ok(result)
        }
    }

    pub fn deserialize_rc_box_any_map<'de, D>(
        deserializer: D,
    ) -> Result<HashMap<Path, Rc<Box<dyn Any>>>, D::Error>
    where
        D: Deserializer<'de>,
    {
        deserializer.deserialize_map(ExportMapVisitor)
    }
}

impl Display for Export {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let string = serde_json::to_string(&self);
        write!(f, "{}", string.unwrap())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::path;
    use crate::path::Path;
    use crate::slot::Slot::{Nbr, Rep};

    #[test]
    fn test_new_empty() {
        let export: Export = Export::new();
        assert!(export.map.is_empty())
    }

    #[test]
    fn test_new() {
        /* showing how the macros saves us from writing this:
        let mut map: HashMap<Path, Rc<Box<dyn Any>>> = HashMap::new();
        map.insert(Path::from(vec![Rep(0), Nbr(0)]), Rc::new(Box::new(10)));
        let export = Export::from(map);*/
        let export = export!((path!(Rep(0), Nbr(0)), 10));
        assert_eq!(export.map.len(), 1);
    }

    #[test]
    fn test_put() {
        let mut export = export!((path!(Rep(0)), 10));
        export.put(path!(Rep(0), Nbr(0)), || 20);
        export.put(Path::from(vec![Nbr(0)]), || "foo");
        assert_eq!(export.paths().len(), 3);
    }

    #[test]
    fn test_get() {
        let export = export!((path!(Nbr(0), Rep(0)), 10));
        assert_eq!(
            //path is written in reverse order in the macro
            export
                .get::<i32>(&Path::from(vec![Rep(0), Nbr(0)]))
                .unwrap(),
            10
        );
    }

    #[test]
    fn test_get_none() {
        let export = export!((path!(Rep(0), Nbr(0)), 10));
        assert!(export
            .get::<String>(&Path::from(vec![Rep(0), Nbr(0)]))
            .is_err());
    }

    #[test]
    fn test_root() {
        let export = export!((Path::new(), 10));
        assert_eq!(export.root::<i32>(), 10);
    }

    #[test]
    #[should_panic]
    fn test_root_panic() {
        let export = export!((Path::new(), 10));
        assert_eq!(export.root::<String>(), "foo");
    }

    #[test]
    fn test_paths() {
        let export = export!((Path::new(), 10));
        let mut map2: HashMap<Path, Rc<Box<dyn Any>>> = HashMap::new();
        map2.insert(Path::new(), Rc::new(Box::new(10)));
        assert!(export.map.keys().eq(map2.keys()));
    }

    #[test]
    fn test_empty_state() {
        let export: Export = Export::new();
        let path = path!(Nbr(0), Rep(0));
        assert!(export.get::<i32>(&Path::new()).is_err());
        assert!(export.get::<i32>(&path).is_err());
    }

    #[test]
    fn test_root_path() {
        let mut export: Export = Export::new();
        export.put(Path::new(), || String::from("foo"));
        assert_eq!(
            export.get::<String>(&Path::new()).unwrap(),
            export.root::<String>()
        );
        assert_eq!(
            export.get::<String>(&Path::new()).unwrap(),
            "foo".to_string()
        );
    }

    #[test]
    fn test_non_root_path() {
        let mut export: Export = Export::new();
        let path = path!(Nbr(0), Rep(0));
        export.put(path.clone(), || String::from("bar"));
        assert_eq!(export.get::<String>(&path).unwrap(), String::from("bar"));
    }

    #[test]
    fn test_overwriting_with_different_type() {
        let mut export: Export = Export::new();
        export.put(Path::new(), || String::from("foo"));
        assert_eq!(
            export.get::<String>(&Path::new()).unwrap(),
            "foo".to_string()
        );
        export.put(Path::new(), || 77);
        assert_eq!(export.get::<i32>(&Path::new()).unwrap(), 77);
    }

    #[test]
    fn test_serialize_and_deserialize() {
        let export = export![
            (path!(Rep(0), Nbr(0)), 10),
            (path!(Nbr(0)), 10),
            (path!(Rep(0)), 10),
            (Path::new(), 10)
        ];
        let export_ser = serde_json::to_string(&export).unwrap();
        let export_des: Export = serde_json::from_str(&export_ser).unwrap();
        let value_at_nbr = export.get::<i32>(&path!(Nbr(0))).unwrap();
        let value_at_nbr_des = export_des.get::<i32>(&path!(Nbr(0))).unwrap();
        assert_eq!(value_at_nbr, value_at_nbr_des);
        let root_value = export.root::<i32>();
        let root_value_des = export_des.root::<i32>();
        assert_eq!(root_value, root_value_des);
    }
}