What this allows us to do is much better relationship tracking between sessions and invoices. The CASS file already has all the structure we need, and it would be silly to replicate it via complicated time association algorithms. This approach uses the linear nature of the data file to track the position relative to other entries. The timeline module is then responsible for making changes to the internal representation (in case it is being used as a library for multi-query commands), and emitting a `Delta` type that can be used to easily patch the IR in question, because the mapping between the timeline and IR representations is linear.wip/yesman
parent
b9c988f425
commit
69eaad1c9f
@ -0,0 +1,72 @@ |
||||
//! A set of error types for cassiopeia
|
||||
|
||||
use std::error::Error; |
||||
use std::fmt::{self, Display, Formatter}; |
||||
|
||||
/// User errors that can occur when using cassiopeia
|
||||
///
|
||||
/// None of these errors are the fault of the program, but rather
|
||||
/// fault of the user for giving invalid commands. They must never
|
||||
/// make the program crash, but instead need to print human friendly
|
||||
/// error messages.
|
||||
#[derive(Debug)] |
||||
pub enum UserError { |
||||
/// Trying to start a session when one exists
|
||||
ActiveSessionExists, |
||||
/// Trying to stop a session when none exists
|
||||
NoActiveSession, |
||||
/// Trying to create a second invoice on the same day
|
||||
SameDayInvoice, |
||||
/// No work was done since the last invoice
|
||||
NoWorkInvoice, |
||||
} |
||||
|
||||
impl Display for UserError { |
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
||||
write!(f, "You're doing it wrong!") |
||||
} |
||||
} |
||||
|
||||
impl Error for UserError {} |
||||
|
||||
pub type UserResult<T> = Result<T, UserError>; |
||||
|
||||
/// Errors that occur when parsing a file
|
||||
///
|
||||
/// These errors can pre-maturely terminate the run of the program,
|
||||
/// but must print a detailed error about what is wrong. Also,
|
||||
/// because they are technically a superset of
|
||||
/// [`UserError`](self::UserError), one of the variants is an embedded
|
||||
/// user error.
|
||||
#[derive(Debug)] |
||||
pub enum ParseError { |
||||
/// An embedded user error
|
||||
///
|
||||
/// This error means that the structure of the parsed file is
|
||||
/// wrong, with an invalid sequence of events expressed
|
||||
User(UserError), |
||||
/// An invalid keyword was found
|
||||
BadKeyword { line: usize, tokn: String }, |
||||
/// A bad timestamp was found
|
||||
BadTimestamp { line: usize, tokn: String }, |
||||
/// A bad date was found
|
||||
BadDate { line: usize, tokn: String }, |
||||
/// An unknown parse error occured
|
||||
Unknown, |
||||
} |
||||
|
||||
impl Display for ParseError { |
||||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { |
||||
write!(f, "The parsed file was bad :(") |
||||
} |
||||
} |
||||
|
||||
impl Error for ParseError {} |
||||
|
||||
pub type ParseResult<T> = Result<T, ParseError>; |
||||
|
||||
impl From<UserError> for ParseError { |
||||
fn from(user: UserError) -> Self { |
||||
ParseError::User(user) |
||||
} |
||||
} |
@ -0,0 +1,132 @@ |
||||
use crate::{ |
||||
data::{Delta, Invoice, Session}, |
||||
error::{UserError, UserResult}, |
||||
Date, Time, |
||||
}; |
||||
|
||||
/// A timeline entry of sessions and invoices
|
||||
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] |
||||
pub(crate) enum Entry { |
||||
Session(Session), |
||||
Invoice(Invoice), |
||||
} |
||||
|
||||
impl From<Session> for Entry { |
||||
fn from(s: Session) -> Self { |
||||
Self::Session(s) |
||||
} |
||||
} |
||||
|
||||
impl From<Invoice> for Entry { |
||||
fn from(i: Invoice) -> Self { |
||||
Self::Invoice(i) |
||||
} |
||||
} |
||||
|
||||
/// A timeline of sessions and invoices, ordered chronologically
|
||||
#[derive(Debug, Default, Clone)] |
||||
pub(crate) struct Timeline { |
||||
inner: Vec<Entry>, |
||||
} |
||||
|
||||
impl Timeline { |
||||
/// Take a set of sessions and invoices to sort into a timeline
|
||||
pub(crate) fn build(s: Vec<Session>, i: Vec<Invoice>) -> Self { |
||||
let mut inner: Vec<_> = s.into_iter().map(|s| Entry::Session(s)).collect(); |
||||
inner.append(&mut i.into_iter().map(|i| Entry::Invoice(i)).collect()); |
||||
Self { inner } |
||||
} |
||||
|
||||
/// Utility function to get the last session in the timeline
|
||||
fn last_session(&mut self) -> Option<&mut Session> { |
||||
self.inner |
||||
.iter_mut() |
||||
.find(|e| match e { |
||||
Entry::Session(_) => true, |
||||
_ => false, |
||||
}) |
||||
.map(|e| match e { |
||||
Entry::Session(ref mut s) => s, |
||||
_ => unreachable!(), |
||||
}) |
||||
} |
||||
|
||||
/// Utility function to get the last invoice in the timeline
|
||||
fn last_invoice(&self) -> Option<&Invoice> { |
||||
self.inner |
||||
.iter() |
||||
.find(|e| match e { |
||||
Entry::Invoice(_) => true, |
||||
_ => false, |
||||
}) |
||||
.map(|e| match e { |
||||
Entry::Invoice(ref s) => s, |
||||
_ => unreachable!(), |
||||
}) |
||||
} |
||||
|
||||
/// Get a list of sessions that happened up to a certain invoice date
|
||||
///
|
||||
/// **WARNING** If there is no invoice with the given date, this
|
||||
/// function will return garbage data, so don't call it with
|
||||
/// invoice dates that don't exist.
|
||||
///
|
||||
/// Because: if the date passes other invoices on the way, the accumulator
|
||||
/// will be discarded and a new count will be started.
|
||||
pub(crate) fn session_iter(&self, date: &Date) -> Vec<&Session> { |
||||
self.inner |
||||
.iter() |
||||
.fold((false, vec![]), |(mut done, mut acc), entry| { |
||||
match (done, entry) { |
||||
// Put sessions into the accumulator
|
||||
(false, Entry::Session(ref s)) => acc.push(s), |
||||
// When we reach the target invoice, terminate the iterator
|
||||
(false, Entry::Invoice(ref i)) if &i.date == date => done = true, |
||||
// When we hit another invoice, empty accumulator
|
||||
(false, Entry::Invoice(_)) => acc.clear(), |
||||
// When we are ever "done", skip all other entries
|
||||
(true, _) => {} |
||||
} |
||||
|
||||
(done, acc) |
||||
}) |
||||
.1 |
||||
} |
||||
|
||||
/// Start a new session, if no active session is already in progress
|
||||
pub(crate) fn start(&mut self, time: Time) -> UserResult<Delta> { |
||||
match self.last_session() { |
||||
Some(s) if !s.finished() => Err(UserError::ActiveSessionExists), |
||||
_ => Ok(()), |
||||
}?; |
||||
|
||||
self.inner.push(Session::start(time.clone()).into()); |
||||
Ok(Delta::Start(time)) |
||||
} |
||||
|
||||
/// Stop an ongoing session, if one exists
|
||||
pub(crate) fn stop(&mut self, time: Time) -> UserResult<Delta> { |
||||
match self.last_session() { |
||||
Some(s) if s.finished() => Err(UserError::NoActiveSession), |
||||
_ => Ok(()), |
||||
}?; |
||||
|
||||
self.last_session().unwrap().stop(time.clone()); |
||||
Ok(Delta::Stop(time)) |
||||
} |
||||
|
||||
/// Create a new invoice on the given day
|
||||
pub(crate) fn invoice(&mut self, date: Date) -> UserResult<Delta> { |
||||
match self.last_invoice() { |
||||
// If an invoice on the same day exists already
|
||||
Some(i) if i.date == date => Err(UserError::SameDayInvoice), |
||||
// If there was no work since the last invoice
|
||||
Some(ref i) if self.session_iter(&i.date).len() == 0 => Err(UserError::NoWorkInvoice), |
||||
// Otherwise everything is coolio
|
||||
_ => Ok(()), |
||||
}?; |
||||
|
||||
self.inner.push(Invoice::new(date.clone()).into()); |
||||
Ok(Delta::Invoice(date)) |
||||
} |
||||
} |
Loading…
Reference in new issue