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
use crate::{NeverConflict, StateMachine};
use im_rc::OrdMap;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::fmt::Debug;

#[derive(Clone, Debug, Serialize, Deserialize, Default)]
pub struct Map<
    T: Serialize + DeserializeOwned + Ord + PartialEq + Clone + Debug + 'static,
    V: StateMachine,
> {
    #[serde(bound = "")]
    inner: OrdMap<T, V>,
}

impl<
        T: Serialize + DeserializeOwned + Ord + PartialEq + Clone + Debug + 'static,
        V: StateMachine + PartialEq,
    > Map<T, V>
{
    pub fn new() -> Self {
        Map {
            inner: OrdMap::default(),
        }
    }

    pub fn insert(&self, key: T, value: V) -> MapTransition<T, V> {
        MapTransition {
            key,
            value: Some(value),
        }
    }

    pub fn delete(&self, key: T) -> MapTransition<T, V> {
        MapTransition { key, value: None }
    }

    pub fn get(&self, key: &T) -> Option<&V> {
        self.inner.get(key)
    }

    /// Returns an iterator over [ListItem] views into this list.
    pub fn iter(&self) -> impl Iterator<Item = (&T, &V)> {
        self.inner.iter()
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub struct MapTransition<T: PartialEq, V: PartialEq> {
    key: T,
    value: Option<V>,
}

impl<
        T: Serialize + DeserializeOwned + Ord + PartialEq + Clone + Debug + 'static,
        V: StateMachine + PartialEq,
    > StateMachine for Map<T, V>
{
    type Transition = MapTransition<T, V>;
    type Conflict = NeverConflict;

    fn apply(&self, transition: &Self::Transition) -> Result<Self, Self::Conflict> {
        match &transition.value {
            Some(v) => {
                let mut c = self.inner.clone();
                c.insert(transition.key.clone(), v.clone());
                Ok(Map { inner: c })
            }
            None => {
                let mut c = self.inner.clone();
                c.remove(&transition.key);
                Ok(Map { inner: c })
            }
        }
    }
}

#[cfg(test)]
mod test {
    use crate::data_structures::Atom;

    use super::*;

    #[test]
    fn map_is_empty() {
        let map: Map<String, Atom<u32>> = Map::new();

        let values: Vec<(&String, &Atom<u32>)> = map.iter().collect();

        assert!(values.is_empty());
    }

    #[test]
    fn map_inserts() {
        let mut map: Map<String, Atom<u32>> = Map::new();

        let transition = map.insert("foo".into(), Atom::new(44));
        map = map.apply(&transition).unwrap();

        let values: Vec<(&String, &Atom<u32>)> = map.iter().collect();
        assert_eq!(vec![(&"foo".to_string(), &Atom::new(44))], values);
    }
}