aboutsummaryrefslogtreecommitdiff
path: root/apps/cassiopeia/src/data.rs
diff options
context:
space:
mode:
Diffstat (limited to 'apps/cassiopeia/src/data.rs')
-rw-r--r--apps/cassiopeia/src/data.rs66
1 files changed, 60 insertions, 6 deletions
diff --git a/apps/cassiopeia/src/data.rs b/apps/cassiopeia/src/data.rs
index 8ebc67f016c5..3911345109ca 100644
--- a/apps/cassiopeia/src/data.rs
+++ b/apps/cassiopeia/src/data.rs
@@ -5,25 +5,79 @@
//! analysis tasks.
use crate::format::LineCfg;
-use chrono::{Date, DateTime, FixedOffset as Offset};
+use chrono::{DateTime, Duration, FixedOffset as Offset, NaiveDate};
use std::collections::BTreeMap;
-#[derive(Default)]
+#[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>,
- invoices: Vec<Date<Offset>>,
+ /// A parsed invoice list
+ invoices: Vec<Invoice>,
}
impl TimeFile {
- pub(crate) fn append(self, line: LineCfg) -> Self {
- println!("{:?}", line);
+ 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: 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 }
+ }
}