Skip to main content

amethystate_core/
change.rs

1use uuid::Uuid;
2
3#[derive(Debug, Clone, PartialEq)]
4pub struct Change<T> {
5    pub source: Option<Uuid>,
6    pub old_value: T,
7    pub new_value: T,
8}
9
10#[derive(Debug, Clone, PartialEq)]
11pub enum MapChange<K, V> {
12    Insert {
13        key: K,
14        value: V,
15        source: Option<Uuid>,
16    },
17    Update {
18        key: K,
19        old_value: V,
20        new_value: V,
21        source: Option<Uuid>,
22    },
23    Remove {
24        key: K,
25        old_value: V,
26        source: Option<Uuid>,
27    },
28    Clear {
29        source: Option<Uuid>,
30    },
31}
32
33impl<K, V> MapChange<K, V> {
34    pub fn key(&self) -> Option<&K> {
35        match self {
36            MapChange::Insert { key, .. } => Some(key),
37            MapChange::Update { key, .. } => Some(key),
38            MapChange::Remove { key, .. } => Some(key),
39            MapChange::Clear { .. } => None,
40        }
41    }
42
43    pub fn source(&self) -> Option<Uuid> {
44        match self {
45            MapChange::Insert { source, .. } => *source,
46            MapChange::Update { source, .. } => *source,
47            MapChange::Remove { source, .. } => *source,
48            MapChange::Clear { source } => *source,
49        }
50    }
51}