aboutsummaryrefslogtreecommitdiff
path: root/apps/cassiopeia/src/data.rs
blob: 3911345109caa6d86d28eddd654853421cde68b1 (plain)
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
//! Typed time file for cassiopeia
//!
//! This data gets generated by the `format` module, and can later be
//! used to generate new files, and perform various lookups and
//! analysis tasks.

use crate::format::LineCfg;
use chrono::{DateTime, Duration, FixedOffset as Offset, NaiveDate};
use std::collections::BTreeMap;

#[derive(Debug, Default)]
pub struct TimeFile {
    /// Raw line buffers to echo back into the file
    lines: Vec<LineCfg>,
    /// A parsed header structure
    header: BTreeMap<String, String>,
    /// A parsed session structure
    sessions: Vec<Session>,
    /// A parsed invoice list
    invoices: Vec<Invoice>,
}

impl TimeFile {
    pub(crate) fn append(mut self, line: LineCfg) -> Self {
        let lo = self.lines.len();
        match line {
            LineCfg::Header(ref header) => self.header = header.clone(),
            LineCfg::Start(Some(time)) => self.sessions.push(Session::start(time, lo)),
            LineCfg::Stop(Some(time)) => self.get_last_session().stop(time, lo),
            LineCfg::Invoice(Some(date)) => self.invoices.push(Invoice::new(date, lo)),
            _ => {}
        }

        self.lines.push(line);
        self
    }

    fn get_last_session(&mut self) -> &mut Session {
        self.sessions.last_mut().unwrap()
    }
}

#[derive(Debug)]
pub struct Session {
    start: DateTime<Offset>,
    stop: Option<DateTime<Offset>>,
    /// Track the lines this session took place in
    lines: (usize, usize),
}

impl Session {
    /// Create a new session with a start time
    fn start(start: DateTime<Offset>, line: usize) -> Self {
        Self {
            start,
            stop: None,
            lines: (line, 0),
        }
    }

    /// Finalise a session with a stop time
    fn stop(&mut self, stop: DateTime<Offset>, line: usize) {
        self.stop = Some(stop);
        self.lines.1 = line;
    }

    /// Get the length of the session, if it was already finished
    pub fn length(&self) -> Option<Duration> {
        self.stop.map(|stop| stop - self.start)
    }
}

#[derive(Debug)]
pub struct Invoice {
    date: NaiveDate,
    line: usize,
}

impl Invoice {
    fn new(date: NaiveDate, line: usize) -> Self {
        Self { date, line }
    }
}