//! 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, /// 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