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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
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;

/// Represents the Result of a query made to the Export.
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"
    )]
    map: HashMap<Path, Rc<Box<dyn Any>>>,
}

#[macro_export]
macro_rules! export {
        ($($x:expr),*) => {{
            let mut temp_map = std::collections::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.
    pub fn put<A: 'static>(&mut self, path: Path, value: A) {
        self.map.insert(path, Rc::new(Box::new(value)));
    }

    /// Inserts a value in the Export at the given Path. The value is calculated from the provided
    /// function.
    ///
    /// # Arguments
    ///
    /// * `path` - The Path where to insert the value.
    /// * `fun` - The function from which we calculate 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 from which the value is calculated.
    pub fn put_lazy<A: 'static, F>(&mut self, path: Path, fun: F)
    where
        F: FnOnce() -> A,
    {
        let value = fun();
        self.put(path, value);
    }

    /// Inserts a value in the Export at the given Path. The value is calculated from the provided
    /// function and then returned to the caller.
    ///
    /// # Arguments
    ///
    /// * `path` - The Path where to insert the value.
    /// * `fun` - The function from which we calculate 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 from which the value is calculated.
    ///
    /// # Returns
    ///
    /// The calculated value.
    pub fn put_lazy_and_return<A: 'static + Clone, F>(&mut self, path: Path, fun: F) -> A
    where
        F: FnOnce() -> A,
    {
        let value = fun();
        self.put(path, value.clone());
        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. This function may panic, so it is preferable to use the non-panicking
    /// counterpart [Export::root_as_result]
    ///
    /// # 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()
    }

    /// Obtain the root value. This method is the non-panicking version of [Export::root]
    ///
    /// # Generic Parameters
    ///
    /// * `A` - The type of the value to return. It must have a `'static` lifetime.
    ///
    /// # Returns
    ///
    /// A Result containing the root value if present and an error otherwise.
    pub fn root_as_result<A: 'static + FromStr + Clone>(&self) -> Result<A> {
        self.get(&Path::new())
    }

    /// 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 Default for Export {
    fn default() -> Self {
        Self::new()
    }
}

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

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

impl PartialEq for Export {
    fn eq(&self, other: &Self) -> bool {
        let keys_len_equality = self.map.keys().len() == other.map.keys().len();

        let values_equality = self.map.iter().all(|(key, _value)| {
            if let Ok(value) = self.get::<i32>(key) {
                if let Ok(other_value) = other.get::<i32>(key) {
                    value == other_value
                } else {
                    false
                }
            } else if let Ok(value) = self.get::<bool>(key) {
                if let Ok(other_value) = other.get::<bool>(key) {
                    value == other_value
                } else {
                    false
                }
            } else if let Ok(value) = self.get::<String>(key) {
                if let Ok(other_value) = other.get::<String>(key) {
                    value == other_value
                } else {
                    false
                }
            } else if let Ok(value) = self.get::<f64>(key) {
                if let Ok(other_value) = other.get::<f64>(key) {
                    value == other_value
                } else {
                    false
                }
            } else {
                false
            }
        });

        keys_len_equality && values_equality
    }
}

/// 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)
    }
}

#[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_put_lazy() {
        let mut export: Export = Export::new();
        export.put_lazy(path!(Nbr(0)), || 10);
        assert_eq!(export.get::<i32>(&path!(Nbr(0))).unwrap(), 10);
    }

    #[test]
    fn test_put_lazy_and_return() {
        let mut export: Export = Export::new();
        let value = export.put_lazy_and_return(path!(Nbr(0)), || 10);
        assert_eq!(value, 10);
        assert_eq!(export.get::<i32>(&path!(Nbr(0))).unwrap(), 10);
    }

    #[test]
    fn test_partial_eq() {
        //assert the equality of two exports
        let export1 = export!((path!(Rep(0), Nbr(0)), 10));
        let export2 = export!((path!(Rep(0), Nbr(0)), 10));
        assert_eq!(export1, export2);

        //assert the inequality of two exports
        let export3 = export!((path!(Rep(0), Nbr(0)), 100));
        assert_ne!(export1, export3);
    }

    #[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();
        assert_eq!(export, export_des);
    }
}