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
use anchor_lang::prelude::*;

/// Three-state operation that allows the ability to set, clear or do nothing with a value.
/// Useful to use in lieu of an Option when the None variant could be ambiguous
/// about whether to clear or do nothing.
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Debug, PartialEq, Eq, Hash)]
pub enum Operation<T> {
    None,
    Clear,
    Set(T),
}

impl<T> Operation<T> {
    pub fn is_none(&self) -> bool {
        matches!(self, Operation::None)
    }

    pub fn is_clear(&self) -> bool {
        matches!(self, Operation::Clear)
    }

    pub fn is_set(&self) -> bool {
        matches!(self, Operation::Set(_))
    }

    pub fn value(&self) -> Option<&T> {
        match self {
            Operation::Set(value) => Some(value),
            _ => None,
        }
    }

    pub fn into_value(self) -> Option<T> {
        match self {
            Operation::Set(value) => Some(value),
            _ => None,
        }
    }
}