aboutsummaryrefslogtreecommitdiff
path: root/apps/cassiopeia/src/data.rs
blob: 188c0255203d29754d63c6301f21fc42512f318b (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
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
//! 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::ir::{IrItem, IrType, MakeIr},
    Date, Time,
};
use chrono::{DateTime, Duration, FixedOffset as Offset, Local, NaiveDate};
use std::collections::BTreeMap;

#[derive(Debug, Default)]
pub struct TimeFile {
    /// 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: IrItem) {
        match line {
            IrItem {
                tt: IrType::Header(ref header),
                ..
            } => self.header = header.clone(),
            IrItem {
                tt: IrType::Start(time),
                lo,
            } => self.sessions.push(Session::start(time.into())),
            IrItem {
                tt: IrType::Stop(time),
                lo,
            } => self.get_last_session().unwrap().stop(time.into()),
            IrItem {
                tt: IrType::Invoice(date),
                lo,
            } => self.invoices.push(Invoice::new(date.into())),
            _ => {}
        }
    }

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

    fn get_last_invoice(&mut self) -> Option<&mut Invoice> {
        self.invoices.last_mut()
    }

    /// Start a new session (optionally 15-minute rounded)
    ///
    /// This function returns the new session object that will have to
    /// be turned into an IR line to be written back into the file
    pub(crate) fn start(&mut self, round: bool) -> Option<Session> {
        // Check if the last session was closed
        match self.get_last_session() {
            Some(s) if !s.finished() => return None,
            _ => {}
        }

        // Create a new time
        let now = if round {
            Time::now().round()
        } else {
            Time::now()
        };

        Some(Session::start(now))
    }

    /// Stop the last session that was started, returning a completed
    /// session
    pub(crate) fn stop(&mut self, round: bool) -> Option<Session> {
        match self.get_last_session() {
            Some(s) if s.finished() => return None,
            None => return None,
            _ => {}
        }

        // Create a new time
        let now = if round {
            Time::now().round()
        } else {
            Time::now()
        };

        self.get_last_session().cloned().map(|mut s| {
            s.stop(now);
            s
        })
    }

    /// Add a new invoice block to the time file
    pub(crate) fn invoice(&mut self) -> Option<Invoice> {
        let today = Date::today();

        let last_sess = self.get_last_session().cloned();

        match self.get_last_invoice() {
            // Check if _today_ there has been already an invoice
            Some(i) if i.date == today => return None,

            // Check if since the last invoice there has been at least
            // _one_ terminated session.
            Some(i)
                if !last_sess
                    .map(|s| !s.stop.map(|s| s.after(&i.date)).unwrap_or(false))
                    .unwrap_or(false) =>
            {
                return None
            }

            // Otherwise, we create an invoice
            _ => {}
        }

        Some(Invoice::new(today))
    }
}

#[derive(Clone, Debug)]
pub struct Session {
    start: Time,
    stop: Option<Time>,
}

impl Session {
    /// Create a new session with a start time
    fn start(start: Time) -> Self {
        Self { start, stop: None }
    }

    /// Finalise a session with a stop time
    fn stop(&mut self, stop: Time) {
        self.stop = Some(stop);
    }

    /// Check whether this session was already finished
    pub fn finished(&self) -> bool {
        self.stop.is_some()
    }

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

impl MakeIr for Session {
    fn make_ir(&self) -> IrType {
        match self.stop {
            Some(ref time) => IrType::Stop(time.clone()),
            None => IrType::Start(self.start.clone()),
        }
    }
}

#[derive(Debug)]
pub struct Invoice {
    date: Date,
}

impl Invoice {
    fn new(date: Date) -> Self {
        Self { date }
    }
}

impl MakeIr for Invoice {
    fn make_ir(&self) -> IrType {
        IrType::Invoice(self.date.clone())
    }
}