aboutsummaryrefslogtreecommitdiff
path: root/apps/cassiopeia/src/data.rs
blob: 3034d020b1e97844311f95f02fcd74d2a712d297 (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
//! 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::{
    error::{ParseError, ParseResult, UserResult},
    format::ir::{IrItem, IrType, MakeIr},
    timeline::{Entry, Timeline},
    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
    pub(crate) header: BTreeMap<String, String>,
    /// A parsed timeline of events
    pub(crate) timeline: Timeline,
}

impl TimeFile {
    /// Append entries to the timeline from the parsed IR
    ///
    /// Report any errors that occur back to the parser, that will
    /// print a message to the user and terminate the program.
    pub(crate) fn append(&mut self, line: IrItem) -> ParseResult<()> {
        match line {
            IrItem {
                tt: IrType::Header(ref header),
                ..
            } => Ok(header.iter().for_each(|(k, v)| {
                self.header.insert(k.clone(), v.clone());
            })),
            IrItem {
                tt: IrType::Start(time),
                lo,
            } => Ok(self.timeline.start(time).map(|_| ())?),
            IrItem {
                tt: IrType::Stop(time),
                lo,
            } => Ok(self.timeline.stop(time).map(|_| ())?),
            IrItem {
                tt: IrType::Invoice(date),
                lo,
            } => Ok(self.timeline.invoice(date).map(|_| ())?),
            _ => Err(ParseError::Unknown),
        }
    }
}

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

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

    /// Finalise a session with a stop time
    pub(crate) 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)
    }
}

#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct Invoice {
    pub(crate) date: Date,
}

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

/// Changes to the timeline are encoded in a delta
pub(crate) enum Delta {
    Start(Time),
    Stop(Time),
    Invoice(Date),
}

impl MakeIr for Delta {
    fn make_ir(&self) -> IrType {
        match self {
            Self::Start(ref time) => IrType::Start(time.clone()),
            Self::Stop(ref time) => IrType::Stop(time.clone()),
            Self::Invoice(ref date) => IrType::Invoice(date.clone()),
        }
    }
}