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
//! Module representing the kind of an entry.
//!
//! # Examples
//!
//! ```rust
//! use timelog::entry::EntryKind;
//!
//! # fn main() {
//!     let ek = EntryKind::new(Some('!'));
//!     println!("Is ignored: {}", ek.is_ignored());
//!
//!     let ek = EntryKind::from_entry_line("2022-06-20 21:15:34^+happening Something");
//!     println!("Is event: {}", ek.is_event());
//! # }
//! ```
//!
//! # Description
//!
//! Objects of this type represent the kind of an [`Entry`].

#[cfg(doc)]
use crate::entry::Entry;
use crate::entry::{EntryError, MARKER_RE};

/// Enumeration of different kinds of task entries
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum EntryKind {
    /// An entry that starts a task.
    Start,
    /// Stop the most current task.
    Stop,
    /// An entry that is ignored.
    Ignored,
    /// An entry that marks a zero duration event.
    Event
}

impl From<EntryKind> for char {
    /// Return the matching mark character for the supplied kind.
    fn from(k: EntryKind) -> char {
        match k {
            EntryKind::Start => ' ',
            EntryKind::Stop => ' ',
            EntryKind::Ignored => '!',
            EntryKind::Event => '^'
        }
    }
}

impl EntryKind {
    /// Create a new [`EntryKind`] from an optional character, defaulting to [`EntryKind::Start`]
    /// if the character is not recognized.
    pub fn new(mark: Option<char>) -> Self {
        Self::try_new(mark).unwrap_or(Self::Start)
    }

    /// Create a new [`EntryKind`] from an optional character.
    ///
    /// ## Errors
    ///
    /// Return [`EntryError::InvalidMarker`] if character not recognized.
    pub fn try_new(mark: Option<char>) -> Result<Self, EntryError> {
        match mark {
            None => Ok(Self::Start),
            Some(' ') => Ok(Self::Start),
            Some('!') => Ok(Self::Ignored),
            Some('^') => Ok(Self::Event),
            Some(_) => Err(EntryError::InvalidMarker),
        }
    }

    /// Extract the appropriate [`EntryKind`] from supplied entry line.
    pub fn from_entry_line(line: &str) -> Self {
        let marker = MARKER_RE.captures(line)
            .map(|cap| cap.get(1).and_then(|m| m.as_str().chars().next()));
        Self::new(marker.flatten())
    }

    pub fn is_start(&self) -> bool { *self == Self::Start }
    pub fn is_ignored(&self) -> bool { *self == Self::Ignored }
    pub fn is_event(&self) -> bool { *self == Self::Event }
    pub fn is_stop(&self) -> bool { *self == Self::Stop }
}

#[cfg(test)]
mod tests {
    use super::*;
    use spectral::prelude::*;

    #[test]
    fn try_new_successes() {
        assert_that!(EntryKind::try_new(None)).is_ok().is_equal_to(EntryKind::Start);
        assert_that!(EntryKind::try_new(Some(' '))).is_ok().is_equal_to(EntryKind::Start);
        assert_that!(EntryKind::try_new(Some('!'))).is_ok().is_equal_to(EntryKind::Ignored);
        assert_that!(EntryKind::try_new(Some('^'))).is_ok().is_equal_to(EntryKind::Event);
    }

    #[test]
    fn try_new_fails() {
        assert_that!(EntryKind::try_new(Some('a'))).is_err();
        assert_that!(EntryKind::try_new(Some('8'))).is_err();
        assert_that!(EntryKind::try_new(Some('*'))).is_err();
    }

    #[test]
    fn new_never_fails() {
        assert_that!(EntryKind::new(None)).is_equal_to(EntryKind::Start);
        assert_that!(EntryKind::new(Some(' '))).is_equal_to(EntryKind::Start);
        assert_that!(EntryKind::new(Some('!'))).is_equal_to(EntryKind::Ignored);
        assert_that!(EntryKind::new(Some('^'))).is_equal_to(EntryKind::Event);
        assert_that!(EntryKind::new(Some('a'))).is_equal_to(EntryKind::Start);
        assert_that!(EntryKind::new(Some('8'))).is_equal_to(EntryKind::Start);
        assert_that!(EntryKind::new(Some('*'))).is_equal_to(EntryKind::Start);
    }

    #[test]
    fn char_from_kind() {
        assert_that!(char::from(EntryKind::Start)).is_equal_to(' ');
        assert_that!(char::from(EntryKind::Stop)).is_equal_to(' ');
        assert_that!(char::from(EntryKind::Ignored)).is_equal_to('!');
        assert_that!(char::from(EntryKind::Event)).is_equal_to('^');
    }

    #[test]
    fn from_entry_line_on_start() {
        assert_that!(EntryKind::from_entry_line("2013-06-05 10:00:02 +test @Commented"))
            .is_equal_to(EntryKind::Start);
    }

    #[test]
    fn from_entry_line_on_ignored() {
        assert_that!(EntryKind::from_entry_line("2013-06-05 10:00:02!+test @Ignored"))
            .is_equal_to(EntryKind::Ignored);
    }

    #[test]
    fn from_entry_line_on_pin() {
        assert_that!(EntryKind::from_entry_line("2013-06-05 10:00:02^+test @Event"))
            .is_equal_to(EntryKind::Event);
    }

    #[test]
    fn get_line_entry_kind_on_comment() {
        assert_that!(EntryKind::from_entry_line("#2013-06-05 10:00:02 +test @Commented"))
            .is_equal_to(EntryKind::Start);
    }
}