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
#[cfg(doc)]
use crate::entry::Entry;
use crate::entry::{EntryError, MARKER_RE};
#[derive(Debug, Clone, Eq, PartialEq)]
pub enum EntryKind {
Start,
Stop,
Ignored,
Event
}
impl From<EntryKind> for char {
fn from(k: EntryKind) -> char {
match k {
EntryKind::Start => ' ',
EntryKind::Stop => ' ',
EntryKind::Ignored => '!',
EntryKind::Event => '^'
}
}
}
impl EntryKind {
pub fn new(mark: Option<char>) -> Self {
Self::try_new(mark).unwrap_or(Self::Start)
}
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),
}
}
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);
}
}