rtimelog 1.1.1

System for tracking time in a text-log-based format.
Documentation
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
//! Module representing an entry in the timelog.
//!
//! # Examples
//!
//! ```rust
//! use timelog::entry::Entry;
//! use std::fs::File;
//! use std::io::{BufRead, BufReader};
//!
//! fn day_entrys(date: &str, file: &mut File) -> Vec<Entry> {
//!     let mut reader = BufReader::new(file);
//!     reader.lines()
//!           .filter_map(|line| Entry::from_line(&line.ok()?).ok())
//!           .filter(|ev| ev.stamp() == String::from(date))
//!           .collect::<Vec<Entry>>()
//! }
//! ```
//!
//! # Description
//!
//! Objects of this type represent the individual lines in the `timelog.txt` file.
//! Each [`Entry`] has a date and time stamp, an optional project, and a task.

use std::fmt::{self, Debug, Display};

use once_cell::sync::Lazy;
use regex::Regex;

const STOP_CMD: &str = "stop";

// These should not be able to fail, hardcoded input strings.
// Still using expect() in case the regex strings ever get changed.

/// Regular expression to match a time stamp
static TIMESTAMP_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"\A(\d{4}[-/](?:0[1-9]|1[0-2])[-/](?:0[1-9]|[12][0-9]|3[01]) (?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-6][0-9])").expect("Date time Regex failed.")
});
/// A somewhat lax regular expression to match an entry line.
static LAX_LINE_RE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"\A(\d{4}[-/](?:0[1-9]|1[0-2])[-/](?:0[1-9]|[12][0-9]|3[01]) (?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-6][0-9])(.)(.+)").expect("Entry line Regex failed.")
});
/// A regular expression matching the project part of an entry line.
pub static PROJECT_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"\+(\S+)").expect("Entry project regex failed."));
/// A regular expression matching the task part of an entry line.
static TASKNAME_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"@(\S+)").expect("Task name Regex failed."));
/// A regular expression matching a stop line
static STOP_LINE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"\A(\d{4}[-/](?:0[1-9]|1[0-2])[-/](?:0[1-9]|[12][0-9]|3[01]) (?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-6][0-9]) stop").expect("Stop line Regex failed")
});
/// A regular expression matching a stop line
static EVENT_LINE: Lazy<Regex> = Lazy::new(|| {
    Regex::new(r"\A(\d{4}[-/](?:0[1-9]|1[0-2])[-/](?:0[1-9]|[12][0-9]|3[01]) (?:[01][0-9]|2[0-3]):[0-5][0-9]:[0-6][0-9])\^.+").expect("Event line Regex failed")
});
/// Regular expression matching the year portion of an entry line.
pub static YEAR_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"^(\d\d\d\d)").expect("Date regex failed"));
/// Regular expression extracting the marker from the line.
pub static MARKER_RE: Lazy<Regex> =
    Lazy::new(|| Regex::new(r"^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d(.)").expect("Marker regex failed"));

#[doc(inline)]
use crate::date::{Date, DateTime};

pub mod error;
pub mod kind;

/// Errors associated with the entry.
pub use error::EntryError;
/// The kind of entry
pub use kind::EntryKind;

/// Representation of an entry in the log
///
/// Objects of this type represent individual lines in the `timelog.txt` file.
/// Each [`Entry`] has a date and time stamp, an optional project, and a task.
#[derive(Debug, Clone, Eq, PartialEq)]
#[must_use]
pub struct Entry {
    /// Time that this entry began
    time:    DateTime,
    /// An optional project name
    project: Option<String>,
    /// The text of the entry from the entry line
    text:    String,
    /// The type of the entry
    kind:    EntryKind
}

/// # Line parsing tools
impl Entry {
    /// Parse the entry line text into the task name and detail parts if they exist.
    pub fn task_breakdown(entry_text: &str) -> (Option<String>, Option<String>) {
        if entry_text.is_empty() {
            return (None, None);
        }

        let task = PROJECT_RE.replace(entry_text, "").trim().to_string();
        if let Some(caps) = TASKNAME_RE.captures(&task) {
            if let Some(tname) = caps.get(1) {
                let detail = TASKNAME_RE.replace(&task, "").trim().to_string();
                let tname = tname.as_str().to_string();
                return (Some(tname), (!detail.is_empty()).then_some(detail));
            }
        }
        (None, (!task.is_empty()).then_some(task))
    }

    /// Return `true` if the supplied string looks like a stop line.
    pub fn is_stop_line(line: &str) -> bool { STOP_LINE.is_match(line) }

    /// Return `true` if the supplied string looks like an event line.
    pub fn is_event_line(line: &str) -> bool { EVENT_LINE.is_match(line) }

    /// Extract a date/time string from a task line
    pub fn datetime_from_line(line: &str) -> Option<&str> {
        if line.is_empty() || Self::is_comment_line(line) {
            return None;
        }

        if let Some(caps) = LAX_LINE_RE.captures(line) {
            return caps.get(1).map(|s| s.as_str());
        }
        None
    }

    /// Extract a date string from a task line
    pub fn date_from_line(line: &str) -> Option<&str> {
        Self::datetime_from_line(line).and_then(|s| s.split_whitespace().next())
    }

    /// Return the year for the supplied entry line, if any.
    pub fn extract_year(line: &str) -> Option<i32> {
        if Self::is_comment_line(line) {
            return None;
        }

        YEAR_RE
            .captures(line)
            .and_then(|cap| cap[0].parse::<i32>().ok())
    }

    /// Return `true` if the supplied line is a comment.
    pub fn is_comment_line(line: &str) -> bool { line.starts_with('#') }
}

/// # Constructors
impl Entry {
    /// Create a new [`Entry`] representing the supplied task at the supplied time.
    pub fn new(entry_text: &str, time: DateTime) -> Self {
        Self::new_marked(entry_text, time, EntryKind::Start)
    }

    /// Create a new [`Entry`] representing the supplied task at the supplied time and optional
    /// mark.
    pub fn new_marked(entry_text: &str, time: DateTime, kind: EntryKind) -> Self {
        let kind = if kind == EntryKind::Start && entry_text == STOP_CMD {
            EntryKind::Stop
        }
        else {
            kind
        };
        let oproject = PROJECT_RE.captures(entry_text)
            .and_then(|caps| caps.get(1).map(|m| String::from(m.as_str())));
        Self { time, project: oproject, text: entry_text.into(), kind }
    }

    /// Create a new [`Entry`] representing a stop entry for the supplied [`DateTime`]
    pub fn new_stop(time: DateTime) -> Self { Self::new_marked(STOP_CMD, time, EntryKind::Stop) }

    /// Create a new [`Entry`] representing the entry from the supplied line.
    ///
    /// This entry must be formatted as described in Format.md.
    ///
    /// # Errors
    ///
    /// Return an [`EntryError`] if the line is empty or formatted incorrectly.
    pub fn from_line(line: &str) -> Result<Self, EntryError> {
        if line.is_empty() {
            return Err(EntryError::BlankLine);
        }

        match LAX_LINE_RE.captures(line) {
            Some(caps) => {
                let Some(stamp) = caps.get(1) else { return Err(EntryError::InvalidTimeStamp); };
                let Ok(time) = stamp.as_str().parse::<DateTime>() else {
                    return Err(EntryError::InvalidTimeStamp);
                };
                let kind = EntryKind::try_new(caps.get(2).and_then(|m| m.as_str().chars().next()))?;
                Ok(Entry::new_marked(
                    caps.get(3).map_or("", |m| m.as_str()),
                    time,
                    kind
                ))
            }
            None => {
                if TIMESTAMP_RE.is_match(line) {
                    Err(EntryError::MissingTask)
                }
                else {
                    Err(EntryError::InvalidTimeStamp)
                }
            }
        }
    }
}

/// # Accessors
impl Entry {
    /// Return the [`&str`] designated as the project, if any, from the [`Entry`].
    pub fn project(&self) -> Option<&str> { self.project.as_deref() }

    /// Return the [`&str`] containing all of the [`Entry`] except the time and date.
    pub fn entry_text(&self) -> &str { &self.text }

    /// Return the [`String`] containing all of the [`Entry`] except the time and date.
    pub fn task(&self) -> Option<String> { Self::task_breakdown(&self.text).0 }

    /// Return the [`String`] containing all of the [`Entry`] except the time and date.
    pub fn detail(&self) -> Option<String> { Self::task_breakdown(&self.text).1 }

    /// Return the [`String`] containing all of the [`Entry`] except the time and date.
    pub fn task_and_detail(&self) -> (Option<String>, Option<String>) {
        Self::task_breakdown(&self.text)
    }

    /// Return the time for the start of the [`Entry`] in epoch seconds.
    pub fn epoch(&self) -> i64 { self.time.timestamp() }

    /// Return the date for the start of the [`Entry`] as a [`Date`]
    pub fn date(&self) -> Date { self.time.date() }

    /// Return the time for the start of the [`Entry`] as a [`DateTime`]
    pub fn date_time(&self) -> DateTime { self.time }

    /// Return the time for the start of the [`Entry`] as a [`DateTime`]
    #[rustfmt::skip]
    pub fn timestamp(&self) -> String {
        format!("{} {:02}:{:02}", self.time.date(), self.time.hour(), self.time.minute())
    }

    /// Return the date stamp of the [`Entry`] in 'YYYY-MM-DD' format.
    pub fn stamp(&self) -> String { self.date().to_string() }

    /// Return `true` if this a start [`Entry`].
    pub fn is_start(&self) -> bool { self.kind == EntryKind::Start }

    /// Return `true` if this was a stop [`Entry`].
    pub fn is_stop(&self) -> bool { self.kind == EntryKind::Stop }

    /// Return `true` if this was an ignored [`Entry`].
    pub fn is_ignore(&self) -> bool { self.kind == EntryKind::Ignored }

    /// Return an ignored [`Entry`] converted from this one.
    pub fn ignore(&self) -> Self { Self { kind: EntryKind::Ignored, ..self.clone() } }

    /// Return `true` if this was a event [`Entry`].
    pub fn is_event(&self) -> bool { self.kind == EntryKind::Event }
}

/// # Mutators
impl Entry {
    /// Return a new copy of the current [`Entry`] with the date and time
    /// reset the the supplied value.
    pub fn change_date_time(&self, date_time: DateTime) -> Self {
        let mut entry = self.clone();
        entry.time = date_time;
        entry
    }

    /// Return a new copy of the current [`Entry`] with the date and time
    /// reset the the supplied value.
    pub fn change_text(&self, task: &str) -> Self {
        if self.is_stop() { return self.clone(); }
        Self::new_marked(task, self.time, self.kind)
    }

    /// Return a new [`Entry`] timestamped as the end of this date
    pub fn to_day_end(&self) -> Self { Self { time: self.date().day_end(), ..self.clone() } }
}

impl Display for Entry {
    /// Format the [`Entry`] formatted as described in Format.md.
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mark: char = self.kind.into();
        write!(f, "{}{mark}{}", self.time, self.text)
    }
}

impl PartialOrd for Entry {
    /// This method returns an ordering between self and other values if one exists.
    #[rustfmt::skip]
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        Some(self.time.cmp(&other.time)
            .then_with(|| self.text.cmp(&other.text)))
    }
}

#[cfg(test)]
mod tests {
    use assert2::{assert, let_assert};
    use rstest::rstest;

    use super::*;

    const CANONICAL_LINE: &str = "2013-06-05 10:00:02 +proj1 @do something";
    const IGNORED_LINE: &str = "2013-06-05 10:00:02!+proj1 @do something";
    const EVENT_LINE: &str = "2013-06-05 10:00:02^+proj1 @do something";
    const STOP_LINE: &str = "2013-06-05 10:00:02 stop";

    fn reference_time() -> i64 { DateTime::new((2013, 6, 5), (10, 0, 2)).expect("Hardcoded value").timestamp() }

    #[test]
    fn from_line_error_if_empty() {
        assert!(Err(EntryError::BlankLine) == Entry::from_line(""));
    }

    #[rstest]
    #[case("# Random comment", "simple comment")]
    #[case("#2013-06-05 10:00:02 +test @Commented", "commented entry")]
    fn is_comment_found(#[case]input: &str, #[case]msg: &str) {
        assert!(Entry::is_comment_line(input), "{msg}");
    }

    #[rstest]
    #[case("", "empty line")]
    #[case("2013-06-05 10:00:02 +test @Commented", "entry line")]
    fn is_comment_not_found(#[case]input: &str, #[case]msg: &str) {
        assert!(!Entry::is_comment_line(input), "{msg}");
    }

    #[rstest]
    #[case("", "empty line")]
    #[case("# Random comment", "simple comment")]
    #[case("#2013-06-05 10:00:02 +test @Commented", "commented entry")]
    fn test_datetime_not_found(#[case]input: &str, #[case]msg: &str) {
        assert!(None == Entry::datetime_from_line(input), "{msg}");
    }

    #[rstest]
    #[case(CANONICAL_LINE, "2013-06-05 10:00:02", "entry line")]
    #[case(IGNORED_LINE,  "2013-06-05 10:00:02", "ignored line")]
    #[case(EVENT_LINE,  "2013-06-05 10:00:02", "event line")]
    #[case(STOP_LINE,  "2013-06-05 10:00:02", "stop line")]
    fn test_datetime_from_line(#[case]input: &str, #[case]expected: &str, #[case]msg: &str) {
        let_assert!(Some(dt) = Entry::datetime_from_line(input));
        assert!(dt == expected, "{msg}");
    }

    #[rstest]
    #[case("", "empty line")]
    #[case("# Random comment", "simple comment")]
    #[case("#2013-06-05 10:00:02 +test @Commented", "entry line")]
    fn test_date_not_found(#[case]input: &str, #[case]msg: &str) {
        assert!(None == Entry::date_from_line(input), "{msg}");
    }

    #[rstest]
    #[case(CANONICAL_LINE, "2013-06-05", "entry line")]
    #[case(IGNORED_LINE,  "2013-06-05", "ignored line")]
    #[case(EVENT_LINE,  "2013-06-05", "event line")]
    #[case(STOP_LINE,  "2013-06-05", "stop line")]
    fn test_date_from_line(#[case]input: &str, #[case]expected: &str, #[case]msg: &str) {
        let_assert!(Some(dt) = Entry::date_from_line(input));
        assert!(dt == expected, "{msg}");
    }

    #[test]
    fn from_line_error_if_not_entry() {
        assert!(Err(EntryError::InvalidTimeStamp) == Entry::from_line("This is not an entry"));
    }

    #[test]
    fn from_line_canonical_entry() {
        let_assert!(Ok(entry) = Entry::from_line(CANONICAL_LINE));
        assert!(entry.stamp() == String::from("2013-06-05"));
        assert!(entry.project() == Some("proj1"));
        assert!(entry.entry_text() == "+proj1 @do something");
        assert!(entry.task() == Some(String::from("do")));
        assert!(entry.detail() == Some(String::from("something")));
        assert!(entry.task_and_detail() == (Some(String::from("do")), Some(String::from("something"))));
        assert!(entry.epoch() == reference_time());
        assert!(entry.to_string().as_str() == CANONICAL_LINE);
        assert!(!entry.is_stop());
        assert!(!entry.is_ignore());
        assert!(!entry.is_event());
    }

    #[test]
    fn new_canonical_entry() {
        let_assert!(Ok(canonical_time) = "2013-06-05 10:00:02".parse::<DateTime>());
        let entry = Entry::new("+proj1 @do something", canonical_time);
        assert!(entry.stamp() == String::from("2013-06-05"));
        assert!(entry.project() == Some("proj1"));
        assert!(entry.entry_text() == "+proj1 @do something");
        assert!(entry.task() == Some(String::from("do")));
        assert!(entry.detail() == Some(String::from("something")));
        assert!(entry.task_and_detail() == (Some(String::from("do")), Some(String::from("something"))));
        assert!(entry.epoch() == reference_time());
        assert!(entry.to_string().as_str() == CANONICAL_LINE);
        assert!(!entry.is_stop());
        assert!(!entry.is_ignore());
        assert!(!entry.is_event());
    }

    #[test]
    fn from_line_no_task_entry() {
        const LINE: &str = "2013-06-05 10:00:02 +proj1 do something";
        let_assert!(Ok(entry) = Entry::from_line(LINE));
        assert!(entry.stamp() == String::from("2013-06-05"));
        assert!(entry.project() == Some("proj1"));
        assert!(entry.entry_text() == "+proj1 do something");
        assert!(entry.task() == None);
        assert!(entry.detail() == Some(String::from("do something")));
        assert!(entry.task_and_detail() == (None, Some(String::from("do something"))));
        assert!(entry.epoch() == reference_time());
        assert!(entry.to_string().as_str() == LINE);
        assert!(!entry.is_stop());
        assert!(!entry.is_ignore());
        assert!(!entry.is_event());
    }

    #[test]
    fn from_line_no_detail_entry() {
        const LINE: &str = "2013-06-05 10:00:02 +proj1 @something";
        let_assert!(Ok(entry) = Entry::from_line(LINE));
        assert!(entry.stamp() == String::from("2013-06-05"));
        assert!(entry.project() == Some("proj1"));
        assert!(entry.entry_text() == "+proj1 @something");
        assert!(entry.task() == Some(String::from("something")));
        assert!(entry.detail() == None);
        assert!(entry.task_and_detail() == (Some(String::from("something")), None));
        assert!(entry.epoch() == reference_time());
        assert!(entry.to_string().as_str() == LINE);
        assert!(!entry.is_stop());
        assert!(!entry.is_ignore());
        assert!(!entry.is_event());
    }

    #[test]
    fn from_line_no_entry_text() {
        const LINE: &str = "2013-06-05 10:00:02 +proj1";
        let_assert!(Ok(entry) = Entry::from_line(LINE));
        assert!(entry.stamp() == String::from("2013-06-05"));
        assert!(entry.project() == Some("proj1"));
        assert!(entry.entry_text() == "+proj1");
        assert!(entry.task() == None);
        assert!(entry.detail() == None);
        assert!(entry.task_and_detail() == (None, None));
        assert!(entry.epoch() == reference_time());
        assert!(entry.to_string().as_str() == LINE);
        assert!(!entry.is_stop());
        assert!(!entry.is_ignore());
        assert!(!entry.is_event());
    }

    #[test]
    fn from_line_stop_entry() {
        let_assert!(Ok(entry) = Entry::from_line(STOP_LINE));
        assert!(entry.stamp() == String::from("2013-06-05"));
        assert!(entry.project() == None);
        assert!(entry.entry_text() == "stop");
        assert!(entry.task() == None);
        assert!(entry.detail() == Some(String::from("stop")));
        assert!(entry.task_and_detail() == (None, Some(String::from("stop"))));
        assert!(entry.epoch() == reference_time());
        assert!(entry.to_string().as_str() == STOP_LINE);
        assert!(entry.is_stop());
        assert!(!entry.is_ignore());
        assert!(!entry.is_event());
    }

    #[test]
    fn test_extract_year() {
        let line = "2018-11-20 12:34:43 +test @Event";
        assert!(Some(2018) == Entry::extract_year(line));
    }

    #[test]
    fn test_extract_year_fail() {
        let line = "xyzzy 2018-11-20 12:34:43 +test @Event";
        assert!(Entry::extract_year(line) == None);
    }

    #[test]
    fn new_stop_entry() {
        let_assert!(Ok(canonical_time) = "2013-06-05 10:00:02".parse::<DateTime>());
        let entry = Entry::new("stop", canonical_time);
        assert!(entry.stamp() == String::from("2013-06-05"));
        assert!(entry.project() == None);
        assert!(entry.entry_text() == "stop");
        assert!(entry.task() == None);
        assert!(entry.detail() == Some(String::from("stop")));
        assert!(entry.task_and_detail() == (None, Some(String::from("stop"))));
        assert!(entry.epoch() == reference_time());
        assert!(entry.to_string().as_str() == STOP_LINE);
        assert!(entry.is_stop());
        assert!(!entry.is_ignore());
        assert!(!entry.is_event());
    }

    #[test]
    fn from_line_ignored_entry() {
        let_assert!(Ok(entry) = Entry::from_line(IGNORED_LINE));
        assert!(entry.stamp() == String::from("2013-06-05"));
        assert!(entry.project() == Some("proj1"));
        assert!(entry.entry_text() == "+proj1 @do something");
        assert!(entry.task() == Some(String::from("do")));
        assert!(entry.detail() == Some(String::from("something")));
        assert!(entry.task_and_detail() == (Some(String::from("do")), Some(String::from("something"))));
        assert!(entry.epoch() == reference_time());
        assert!(entry.to_string().as_str() == IGNORED_LINE);
        assert!(!entry.is_stop());
        assert!(entry.is_ignore());
        assert!(!entry.is_event());
    }

    #[test]
    fn new_ignored_entry() {
        let_assert!(Ok(canonical_time) = "2013-06-05 10:00:02".parse::<DateTime>());
        let entry = Entry::new_marked("+proj1 @do something", canonical_time, EntryKind::Ignored);
        assert!(entry.stamp() == String::from("2013-06-05"));
        assert!(entry.project() == Some("proj1"));
        assert!(entry.entry_text() == "+proj1 @do something");
        assert!(entry.task() == Some(String::from("do")));
        assert!(entry.detail() == Some(String::from("something")));
        assert!(entry.task_and_detail() == (Some(String::from("do")), Some(String::from("something"))));
        assert!(entry.epoch() == reference_time());
        assert!(entry.to_string().as_str() == IGNORED_LINE);
        assert!(!entry.is_stop());
        assert!(entry.is_ignore());
        assert!(!entry.is_event());
    }

    #[test]
    fn from_line_event_entry() {
        let_assert!(Ok(entry) = Entry::from_line(EVENT_LINE));
        assert!(entry.stamp() == String::from("2013-06-05"));
        assert!(entry.project() == Some("proj1"));
        assert!(entry.entry_text() == "+proj1 @do something");
        assert!(entry.task() == Some(String::from("do")));
        assert!(entry.detail() == Some(String::from("something")));
        assert!(entry.task_and_detail() == (Some(String::from("do")), Some(String::from("something"))));
        assert!(entry.epoch() == reference_time());
        assert!(entry.to_string().as_str() == EVENT_LINE);
        assert!(!entry.is_stop());
        assert!(!entry.is_ignore());
        assert!(entry.is_event());
    }

    #[test]
    fn new_event_entry() {
        let_assert!(Ok(canonical_time) = "2013-06-05 10:00:02".parse::<DateTime>());
        let entry = Entry::new_marked("+proj1 @do something", canonical_time, EntryKind::Event);
        assert!(entry.stamp() == String::from("2013-06-05"));
        assert!(entry.project() == Some("proj1"));
        assert!(entry.entry_text() == "+proj1 @do something");
        assert!(entry.task() == Some(String::from("do")));
        assert!(entry.detail() == Some(String::from("something")));
        assert!(entry.task_and_detail() == (Some(String::from("do")), Some(String::from("something"))));
        assert!(entry.epoch() == reference_time());
        assert!(entry.to_string().as_str() == EVENT_LINE);
        assert!(!entry.is_stop());
        assert!(!entry.is_ignore());
        assert!(entry.is_event());
    }

    #[test]
    fn compare_entry() {
        let_assert!(Ok(entry1) = Entry::from_line("2013-06-05 10:00:02 +proj1"));
        let_assert!(Ok(entry2) = Entry::from_line("2013-06-05 11:00:02 +proj1"));
        assert!(entry2 > entry1);
        assert!(entry1 < entry2);
        assert!(entry1 == entry1);
    }

    #[test]
    fn test_change_date_time_start() {
        let_assert!(Ok(entry) = Entry::from_line("2022-12-27 10:00:00 +proj1"));
        let_assert!(Ok(dt) = DateTime::new((2022, 12, 27), (9, 50, 00)));
        let new_entry = entry.change_date_time(dt);

        assert!(new_entry != entry);
        let_assert!(Ok(dt) = DateTime::new((2022, 12, 27), (9, 50, 00)));
        assert!(new_entry.date_time() == dt);
        assert!(new_entry.entry_text() == entry.entry_text());
        assert!(new_entry.is_start());
    }

    #[test]
    fn test_change_date_time_event() {
        let_assert!(Ok(entry) = Entry::from_line("2022-12-27 10:00:00^+event"));
        let_assert!(Ok(dt) = DateTime::new((2022, 12, 27), (9, 50, 00)));
        let new_entry = entry.change_date_time(dt);

        assert!(new_entry != entry);
        let_assert!(Ok(dt) = DateTime::new((2022, 12, 27), (9, 50, 00)));
        assert!(new_entry.date_time() == dt);
        assert!(new_entry.entry_text() == entry.entry_text());
        assert!(new_entry.is_event());
    }

    #[test]
    fn test_change_date_time_ignored() {
        let_assert!(Ok(entry) = Entry::from_line("2022-12-27 10:00:00!+proj1"));
        let_assert!(Ok(dt) = DateTime::new((2022, 12, 27), (9, 50, 00)));
        let new_entry = entry.change_date_time(dt);

        assert!(new_entry != entry);
        let_assert!(Ok(dt) = DateTime::new((2022, 12, 27), (9, 50, 00)));
        assert!(new_entry.date_time() == dt);
        assert!(new_entry.entry_text() == entry.entry_text());
        assert!(new_entry.is_ignore());
    }

    #[test]
    fn test_change_date_time_stop() {
        let_assert!(Ok(entry) = Entry::from_line("2022-12-27 10:00:00 stop"));
        let_assert!(Ok(dt) = DateTime::new((2022, 12, 27), (9, 50, 00)));
        let new_entry = entry.change_date_time(dt);

        assert!(new_entry != entry);
        let_assert!(Ok(dt) = DateTime::new((2022, 12, 27), (9, 50, 00)));
        assert!(new_entry.date_time() == dt);
        assert!(new_entry.entry_text() == entry.entry_text());
        assert!(new_entry.is_stop());
    }

    #[test]
    fn test_change_text_start() {
        let_assert!(Ok(entry) = Entry::from_line("2022-12-27 10:00:00 +proj1"));
        let new_entry = entry.change_text("+proj2 @Changed");

        assert!(new_entry != entry);
        assert!(new_entry.date_time() == entry.date_time());
        assert!(new_entry.entry_text() == "+proj2 @Changed");
        assert!(new_entry.is_start());
    }

    #[test]
    fn test_change_text_event() {
        let_assert!(Ok(entry) = Entry::from_line("2022-12-27 10:00:00^+event"));
        let new_entry = entry.change_text("+proj2 @Changed");

        assert!(new_entry != entry);
        assert!(new_entry.date_time() == entry.date_time());
        assert!(new_entry.entry_text() == "+proj2 @Changed");
        assert!(new_entry.is_event());
    }

    #[test]
    fn test_change_text_ignored() {
        let_assert!(Ok(entry) = Entry::from_line("2022-12-27 10:00:00!+proj1"));
        let new_entry = entry.change_text("+proj2 @Changed");

        assert!(new_entry != entry);
        assert!(new_entry.date_time() == entry.date_time());
        assert!(new_entry.entry_text() == "+proj2 @Changed");
        assert!(new_entry.is_ignore());
    }

    #[test]
    fn test_change_text_stop() {
        let_assert!(Ok(entry) = Entry::from_line("2022-12-27 10:00:00 stop"));
        let new_entry = entry.change_text("+proj2 @Changed");

        assert!(new_entry == entry);
        assert!(new_entry.date_time() == entry.date_time());
        assert!(new_entry.entry_text() == "stop");
        assert!(new_entry.is_stop());
    }
}