Skip to main content

mit_commit_message_lints/mit/lib/
author_state.rs

1use time::OffsetDateTime;
2
3/// The state of the authors config
4#[derive(Debug, Eq, PartialEq)]
5pub enum AuthorState<T> {
6    /// Author config all good
7    Some(T),
8
9    /// Author config expired
10    Timeout(OffsetDateTime),
11
12    /// Author config never set
13    None,
14}
15
16impl<T> AuthorState<T> {
17    /// There has never been author config
18    pub const fn is_none(&self) -> bool {
19        matches!(self, Self::None)
20    }
21
22    /// The author config has timed out
23    pub const fn is_timeout(&self) -> bool {
24        matches!(self, Self::Timeout(_))
25    }
26
27    /// The author config looks good
28    pub const fn is_some(&self) -> bool {
29        matches!(self, Self::Some(_))
30    }
31
32    /// Take the value from the state and return it
33    ///
34    /// # Panics
35    ///
36    /// Panics if the state is timeout or none
37    pub fn unwrap(self) -> T {
38        match self {
39            Self::Some(value) => value,
40            Self::None => panic!("called `AuthorState::unwrap()` on a `None` value"),
41            Self::Timeout(value) => panic!(
42                "called `AuthorState::unwrap()` on a `Timeout({})` value",
43                value
44            ),
45        }
46    }
47}
48
49impl<T> From<AuthorState<T>> for Option<T> {
50    fn from(values: AuthorState<T>) -> Self {
51        match values {
52            AuthorState::Some(inner) => Some(inner),
53            AuthorState::Timeout(_) | AuthorState::None => None,
54        }
55    }
56}
57
58#[cfg(test)]
59mod tests {
60    use std::time::SystemTime;
61
62    use time::OffsetDateTime;
63
64    use crate::mit::AuthorState;
65
66    #[test]
67    fn test_unwrap_some_succeeds() {
68        assert!(
69            AuthorState::Some(true).unwrap(),
70            "Expected unwrap on Some state to return the inner value"
71        );
72    }
73
74    #[test]
75    #[should_panic = "called `AuthorState::unwrap()` on a `None` value"]
76    fn test_unwrap_none_panics() {
77        assert!(
78            AuthorState::<bool>::None.unwrap(),
79            "Expected unwrap on None state to panic"
80        );
81    }
82
83    #[test]
84    #[should_panic = "called `AuthorState::unwrap()` on a `Timeout(1970-01-01 0:00:10.0 +00:00:00)` value"]
85    fn test_unwrap_timeout_panics() {
86        assert!(
87            AuthorState::<bool>::Timeout(OffsetDateTime::from_unix_timestamp(10).unwrap()).unwrap(),
88            "Expected unwrap on Timeout state to panic"
89        );
90    }
91
92    #[test]
93    fn test_some_state_is_some() {
94        assert!(
95            AuthorState::Some(true).is_some(),
96            "Expected Some state to report is_some as true"
97        );
98    }
99
100    #[test]
101    fn test_some_state_is_not_none() {
102        assert!(
103            !AuthorState::Some(true).is_none(),
104            "Expected Some state to report is_none as false"
105        );
106    }
107
108    #[test]
109    fn test_some_state_is_not_timeout() {
110        assert!(
111            !AuthorState::Some(true).is_timeout(),
112            "Expected Some state to report is_timeout as false"
113        );
114    }
115
116    #[test]
117    fn test_none_state_is_not_some() {
118        assert!(
119            !AuthorState::<bool>::None.is_some(),
120            "Expected None state to report is_some as false"
121        );
122    }
123
124    #[test]
125    fn test_none_state_is_none() {
126        assert!(
127            AuthorState::<bool>::None.is_none(),
128            "Expected None state to report is_none as true"
129        );
130    }
131
132    #[test]
133    fn test_none_state_is_not_timeout() {
134        assert!(
135            !AuthorState::<bool>::None.is_timeout(),
136            "Expected None state to report is_timeout as false"
137        );
138    }
139
140    #[test]
141    fn test_timeout_state_is_not_some() {
142        assert!(
143            !AuthorState::<bool>::Timeout(OffsetDateTime::now_utc()).is_some(),
144            "Expected Timeout state to report is_some as false"
145        );
146    }
147
148    #[test]
149    fn test_timeout_state_is_not_none() {
150        assert!(
151            !AuthorState::<bool>::Timeout(OffsetDateTime::now_utc()).is_none(),
152            "Expected Timeout state to report is_none as false"
153        );
154    }
155
156    #[test]
157    fn test_timeout_state_recognized() {
158        assert!(
159            AuthorState::<bool>::Timeout(OffsetDateTime::now_utc()).is_timeout(),
160            "Expected Timeout state to report is_timeout as true"
161        );
162    }
163
164    #[test]
165    fn test_system_time_timeout_recognition() {
166        assert!(
167            AuthorState::<bool>::Timeout(SystemTime::now().into()).is_timeout(),
168            "Expected a Timeout constructed from SystemTime to be recognized as timeout"
169        );
170    }
171}