Skip to main content

amethystate_core/
change.rs

1use uuid::Uuid;
2
3/// Who made a change the store is telling people about.
4#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum Source {
6    /// A handle in this process, by the id its writes carry.
7    Handle(Uuid),
8
9    /// A write through the store's own API, with no handle behind it.
10    Store,
11
12    /// The file was edited outside this process, and the watcher read it.
13    Disk,
14}
15
16impl Source {
17    /// The handle that made it, for a subscriber telling its own writes from
18    /// everyone else's.
19    pub fn handle(self) -> Option<Uuid> {
20        match self {
21            Source::Handle(id) => Some(id),
22            Source::Store | Source::Disk => None,
23        }
24    }
25}
26
27impl From<Option<Uuid>> for Source {
28    fn from(handle: Option<Uuid>) -> Self {
29        handle.map_or(Source::Store, Source::Handle)
30    }
31}
32
33#[derive(Debug, Clone, PartialEq)]
34pub struct Change<T> {
35    pub source: Option<Uuid>,
36    pub old_value: T,
37    pub new_value: T,
38}
39
40#[derive(Debug, Clone, PartialEq)]
41pub enum MapChange<K, V> {
42    Insert {
43        key: K,
44        value: V,
45        source: Option<Uuid>,
46    },
47    Update {
48        key: K,
49        /// What was there before. Bytes that will not read as `V` fall back to
50        /// what the map last held, so a key it knows always carries one.
51        old_value: Option<V>,
52        new_value: V,
53        source: Option<Uuid>,
54    },
55    Remove {
56        key: K,
57        old_value: Option<V>,
58        source: Option<Uuid>,
59    },
60    Clear {
61        source: Option<Uuid>,
62    },
63}
64
65impl<K, V> MapChange<K, V> {
66    pub fn key(&self) -> Option<&K> {
67        match self {
68            MapChange::Insert { key, .. } => Some(key),
69            MapChange::Update { key, .. } => Some(key),
70            MapChange::Remove { key, .. } => Some(key),
71            MapChange::Clear { .. } => None,
72        }
73    }
74
75    pub fn source(&self) -> Option<Uuid> {
76        match self {
77            MapChange::Insert { source, .. } => *source,
78            MapChange::Update { source, .. } => *source,
79            MapChange::Remove { source, .. } => *source,
80            MapChange::Clear { source } => *source,
81        }
82    }
83}