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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
//! Type `Switch` represents a value which can be turned on or off. It is similar to `(T,bool)`,
//! but with many utility functions allowing for convenient workflow. An example use case would be
//! passing around information if a particular node in a tree was hovered or not. You can pass
//! `Switch<Crumb>` value then, where `Crumb` stores a path to the node from the root of the tree.



// ==============
// === Switch ===
// ==============

/// The `Switch` type. Read module docs to learn more.
#[derive(Clone,Copy,Debug,Default,Eq,PartialEq,Hash)]
#[allow(missing_docs)]
pub struct Switch<T> {
    pub value : T,
    is_on     : bool,
}


// === Construction ===

impl<T> Switch<T> {
    /// Constructor.
    pub fn new(value:T, is_on:bool) -> Self {
        Self {value,is_on}
    }

    /// Constructor.
    #[allow(non_snake_case)]
    pub fn On(value:T) -> Self {
        Self::new(value,true)
    }

    /// Constructor.
    #[allow(non_snake_case)]
    pub fn Off(value:T) -> Self {
        Self::new(value,false)
    }
}


// === Modifiers ===

impl<T> Switch<T> {
    /// Change the on / off status.
    pub fn switch(&mut self, is_on:bool) {
        self.is_on = is_on;
    }

    /// Toggle the on / off status.
    pub fn toggle(&mut self) {
        self.is_on = !self.is_on;
    }

    /// Change the on / off status while consuming the value.
    pub fn switched(mut self, is_on:bool) -> Self {
        self.switch(is_on);
        self
    }

    /// Toggle the on / off status while consuming the value.
    pub fn toggled(mut self) -> Self {
        self.toggle();
        self
    }
}


// === Status ===

impl<T> Switch<T> {
    /// Check whether the value is enabled.
    pub fn is_on(&self) -> bool {
        self.is_on
    }

    /// Check whether the value is disabled.
    pub fn is_off(&self) -> bool {
        !self.is_on()
    }
}


// === Getters ===

impl<T> Switch<T> {
    /// Get the value if it was turned on.
    pub fn on(&self) -> Option<&T> {
        if self.is_on() { Some(&self.value) } else { None }
    }

    /// Get the value if it was turned off.
    pub fn off(&self) -> Option<&T> {
        if self.is_off() { Some(&self.value) } else { None }
    }

    /// Get the value if it was turned on while consuming self.
    pub fn into_on(self) -> Option<T> {
        if self.is_on() { Some(self.value) } else { None }
    }

    /// Get the value if it was turned off while consuming self.
    pub fn into_off(self) -> Option<T> {
        if self.is_off() { Some(self.value) } else { None }
    }

    /// Get the value if the switch was turned on while consuming self, or a default it it was off.
    pub fn into_on_or(self, default:T) -> T {
        if self.is_on() { self.value } else { default }
    }

    /// Get the value if the switch was turned off while consuming self or a default if it was on.
    pub fn into_off_or(self, default:T) -> T {
        if self.is_off() { self.value } else { default }
    }
}



// =============
// === Tests ===
// =============

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_into_on_or() {
        let switch_on = Switch::On(1.0);
        assert_eq!(switch_on.into_on_or(0.0), 1.0);

        let switch_off = Switch::Off(2.0);
        assert_eq!(switch_off.into_on_or(0.0), 0.0);
    }

    #[test]
    fn test_into_off_or() {
        let switch_on = Switch::On(1.0);
        assert_eq!(switch_on.into_off_or(0.0), 0.0);

        let switch_off = Switch::Off(2.0);
        assert_eq!(switch_off.into_off_or(0.0), 2.0);
    }
}