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