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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
    Appellation: state <module>
    Contrib: FL03 <jo3mccain@icloud.com>
*/
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use strum::{Display, EnumCount, EnumIs, EnumIter, EnumString, VariantNames};

#[cfg_attr(feature = "serde", derive(Deserialize, Serialize,))]
#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct State {
    message: String,
    state: States,
}

impl State {
    pub fn new(message: impl ToString, state: States) -> Self {
        Self {
            message: message.to_string(),
            state,
        }
    }
    /// Invalidates the current state
    pub fn invalidate(&mut self) {
        self.state = States::Invalid;
    }
    /// Checks if the current state is valid
    pub fn is_valid(&self) -> bool {
        self.state.is_valid()
    }
    /// Returns the message
    pub fn message(&self) -> &str {
        &self.message
    }

    pub fn set_message(&mut self, message: impl ToString) {
        self.message = message.to_string();
    }
    pub fn set_state(&mut self, state: States) {
        self.state = state;
    }
    /// Returns the current state
    pub fn state(&self) -> States {
        self.state
    }

    pub fn update(&mut self, state: State) {
        *self = state;
    }
}

impl std::fmt::Display for State {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.message)
    }
}

impl From<States> for State {
    fn from(state: States) -> Self {
        Self::new("", state)
    }
}

impl From<State> for States {
    fn from(q: State) -> Self {
        q.state
    }
}

#[cfg_attr(
    feature = "serde",
    derive(Deserialize, Serialize,),
    serde(rename_all = "lowercase", untagged)
)]
#[derive(
    Clone,
    Copy,
    Debug,
    Default,
    Display,
    EnumCount,
    EnumIs,
    EnumIter,
    EnumString,
    Eq,
    Hash,
    Ord,
    PartialEq,
    PartialOrd,
    VariantNames,
)]
#[repr(u8)]
#[strum(serialize_all = "lowercase")]
pub enum States {
    Invalid = 0,
    #[default]
    Valid = 1,
}

impl States {
    /// [State::Invalid] variant constructor
    pub fn invalid() -> Self {
        Self::Invalid
    }
    /// [State::Valid] variant constructor
    pub fn valid() -> Self {
        Self::Valid
    }
    pub fn update(&mut self, state: Self) {
        *self = state;
    }
}

impl std::ops::Mul for States {
    type Output = States;

    fn mul(self, rhs: Self) -> Self::Output {
        let res = self as u8 * rhs as u8;
        Self::from(res)
    }
}

impl std::ops::MulAssign for States {
    fn mul_assign(&mut self, rhs: Self) {
        *self = *self * rhs;
    }
}

impl From<u8> for States {
    fn from(d: u8) -> Self {
        match d % 2 {
            1 => States::valid(),
            _ => States::invalid(),
        }
    }
}

impl From<usize> for States {
    fn from(d: usize) -> Self {
        Self::from(d as i64)
    }
}

impl From<i64> for States {
    fn from(d: i64) -> Self {
        match d.abs() % 2 {
            1 => States::valid(),
            _ => States::invalid(),
        }
    }
}

impl From<States> for i64 {
    fn from(d: States) -> i64 {
        d as i64
    }
}

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

    #[test]
    fn test_states() {
        let a = States::default();
        let mut b = a;
        b *= a;
        assert_eq!(a, States::valid());
        assert_eq!(b, States::valid());
    }

    #[test]
    fn test_states_iter() {
        let a: Vec<States> = States::iter().collect();
        assert_eq!(a.len(), 2);
        assert_eq!(a[0], States::invalid());
    }
}