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}