summaryrefslogtreecommitdiffstats
path: root/src/app/frozen_state.rs
blob: 4fc7293da37757400e453d313bf76e399c83e732 (plain)
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
use super::DataCollection;

/// The [`FrozenState`] indicates whether the application state should be frozen. It is either not frozen or
/// frozen and containing a copy of the state at the time.
pub enum FrozenState {
    NotFrozen,
    Frozen(Box<DataCollection>),
}

impl Default for FrozenState {
    fn default() -> Self {
        Self::NotFrozen
    }
}

pub type IsFrozen = bool;

impl FrozenState {
    /// Checks whether the [`FrozenState`] is currently frozen.
    pub fn is_frozen(&self) -> IsFrozen {
        matches!(self, FrozenState::Frozen(_))
    }

    /// Freezes the [`FrozenState`].
    pub fn freeze(&mut self, data: Box<DataCollection>) {
        *self = FrozenState::Frozen(data);
    }

    /// Unfreezes the [`FrozenState`].
    pub fn thaw(&mut self) {
        *self = FrozenState::NotFrozen;
    }

    /// Toggles the [`FrozenState`] and returns whether it is now frozen.
    pub fn toggle(&mut self, data: &DataCollection) -> IsFrozen {
        if self.is_frozen() {
            self.thaw();
            false
        } else {
            self.freeze(Box::new(data.clone()));
            true
        }
    }
}