| 1 |
package tw |
| 2 |
|
| 3 |
// State represents an on/off state |
| 4 |
type State int |
| 5 |
|
| 6 |
// Public: Methods for State type |
| 7 |
|
| 8 |
// Enabled checks if the state is on |
| 9 |
func (o State) Enabled() bool { return o == Success } |
| 10 |
|
| 11 |
// Default checks if the state is unknown |
| 12 |
func (o State) Default() bool { return o == Unknown } |
| 13 |
|
| 14 |
// Disabled checks if the state is off |
| 15 |
func (o State) Disabled() bool { return o == Fail } |
| 16 |
|
| 17 |
// Toggle switches the state between on and off |
| 18 |
func (o State) Toggle() State { |
| 19 |
if o == Fail { |
| 20 |
return Success |
| 21 |
} |
| 22 |
return Fail |
| 23 |
} |
| 24 |
|
| 25 |
// Cond executes a condition if the state is enabled |
| 26 |
func (o State) Cond(c func() bool) bool { |
| 27 |
if o.Enabled() { |
| 28 |
return c() |
| 29 |
} |
| 30 |
return false |
| 31 |
} |
| 32 |
|
| 33 |
// Or returns this state if enabled, else the provided state |
| 34 |
func (o State) Or(c State) State { |
| 35 |
if o.Enabled() { |
| 36 |
return o |
| 37 |
} |
| 38 |
return c |
| 39 |
} |
| 40 |
|
| 41 |
// String returns the string representation of the state |
| 42 |
func (o State) String() string { |
| 43 |
if o.Enabled() { |
| 44 |
return "on" |
| 45 |
} |
| 46 |
|
| 47 |
if o.Disabled() { |
| 48 |
return "off" |
| 49 |
} |
| 50 |
return "undefined" |
| 51 |
} |