diff options
Diffstat (limited to 'apps/koffice/libko/src')
-rw-r--r-- | apps/koffice/libko/src/cass/data.rs | 116 | ||||
-rw-r--r-- | apps/koffice/libko/src/cass/date.rs | 32 | ||||
-rw-r--r-- | apps/koffice/libko/src/cass/error.rs | 91 | ||||
-rw-r--r-- | apps/koffice/libko/src/cass/format/gen.rs | 32 | ||||
-rw-r--r-- | apps/koffice/libko/src/cass/format/ir.rs | 99 | ||||
-rw-r--r-- | apps/koffice/libko/src/cass/format/lexer.rs | 151 | ||||
-rw-r--r-- | apps/koffice/libko/src/cass/format/mod.rs | 76 | ||||
-rw-r--r-- | apps/koffice/libko/src/cass/format/parser.rs | 73 | ||||
-rw-r--r-- | apps/koffice/libko/src/cass/meta.rs | 46 | ||||
-rw-r--r-- | apps/koffice/libko/src/cass/mod.rs | 160 | ||||
-rw-r--r-- | apps/koffice/libko/src/cass/time.rs | 141 | ||||
-rw-r--r-- | apps/koffice/libko/src/cass/timeline.rs | 132 | ||||
-rw-r--r-- | apps/koffice/libko/src/client.rs | 39 | ||||
-rw-r--r-- | apps/koffice/libko/src/config.rs | 5 | ||||
-rw-r--r-- | apps/koffice/libko/src/invoice.rs | 35 | ||||
-rw-r--r-- | apps/koffice/libko/src/lib.rs | 35 | ||||
-rw-r--r-- | apps/koffice/libko/src/proj.rs | 15 | ||||
-rw-r--r-- | apps/koffice/libko/src/store.rs | 94 | ||||
-rw-r--r-- | apps/koffice/libko/src/timefile.rs | 0 |
19 files changed, 1372 insertions, 0 deletions
diff --git a/apps/koffice/libko/src/cass/data.rs b/apps/koffice/libko/src/cass/data.rs new file mode 100644 index 000000000000..4ff4dc93020e --- /dev/null +++ b/apps/koffice/libko/src/cass/data.rs @@ -0,0 +1,116 @@ +//! 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::cass::{ + 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(Clone, 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 { + pub fn project(&self) -> Option<&String> { + self.header.get("project") + } + + pub fn client(&self) -> Option<&String> { + self.header.get("client") + } + + /// 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 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()), + } + } +} diff --git a/apps/koffice/libko/src/cass/date.rs b/apps/koffice/libko/src/cass/date.rs new file mode 100644 index 000000000000..e35b2a96f0f0 --- /dev/null +++ b/apps/koffice/libko/src/cass/date.rs @@ -0,0 +1,32 @@ +use crate::cass::Time; +use chrono::{FixedOffset as Offset, NaiveDate}; + +/// A convenienc wrapper around [chrono::NaiveDate](chrono::NaiveDate) +#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] +pub struct Date { + inner: NaiveDate, +} + +impl Date { + pub fn today() -> Self { + Self::from(Time::now().date()) + } + + pub(crate) fn from(d: chrono::Date<Offset>) -> Self { + Self { + inner: d.naive_local(), + } + } +} + +impl From<NaiveDate> for Date { + fn from(inner: NaiveDate) -> Self { + Self { inner } + } +} + +impl ToString for Date { + fn to_string(&self) -> String { + format!("{}", self.inner.format("%Y-%m-%d")) + } +} diff --git a/apps/koffice/libko/src/cass/error.rs b/apps/koffice/libko/src/cass/error.rs new file mode 100644 index 000000000000..24bbb4965494 --- /dev/null +++ b/apps/koffice/libko/src/cass/error.rs @@ -0,0 +1,91 @@ +//! A set of error types for cassiopeia + +use std::fmt::{self, Display, Formatter}; +use std::{error::Error, io}; + +/// 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), + /// The requested file did not exist + NoSuchFile, + /// The file could not be read + BadPermissions, + /// The file could not be written to + FileNotWritable, + /// Other file related errors + FileUnknown(String), + /// 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) + } +} + +impl From<io::Error> for ParseError { + fn from(e: io::Error) -> Self { + use io::ErrorKind::*; + match e.kind() { + NotFound => Self::NoSuchFile, + PermissionDenied => Self::BadPermissions, + _ => Self::FileUnknown(format!("{}", e)), + } + } +} diff --git a/apps/koffice/libko/src/cass/format/gen.rs b/apps/koffice/libko/src/cass/format/gen.rs new file mode 100644 index 000000000000..f77bcdc90e84 --- /dev/null +++ b/apps/koffice/libko/src/cass/format/gen.rs @@ -0,0 +1,32 @@ +//! Cassiopeia line generator +//! +//! This module takes a set of IR lines, and generates strings from +//! them that are in accordance with the way that the parser of the +//! same version expects them. + +use crate::cass::format::ir::{IrItem, IrType}; + +/// Take a line of IR and generate a string to print into a file +pub(crate) fn line(ir: &IrItem) -> String { + let IrItem { tt, lo } = ir; + match tt { + IrType::Ignore => "".into(), + IrType::Header(map) => format!( + "HEADER {}", + map.iter() + .map(|(k, v)| format!("{}={},", k, v)) + .collect::<Vec<_>>() + .join("") + ), + IrType::Start(time) => format!("START {}", time.to_string()), + + // FIXME: find a better way to align the lines here rather + // than having to manually having to pad the 'STOP' commands + IrType::Stop(time) => format!("STOP {}", time.to_string()), + IrType::Invoice(date) => format!("INVOICE {}", date.to_string()), + } +} + +pub(crate) fn head_comment() -> String { + ";; generated by cassiopeia, be careful about editing by hand!".into() +} diff --git a/apps/koffice/libko/src/cass/format/ir.rs b/apps/koffice/libko/src/cass/format/ir.rs new file mode 100644 index 000000000000..d1a3a62c1508 --- /dev/null +++ b/apps/koffice/libko/src/cass/format/ir.rs @@ -0,0 +1,99 @@ +use crate::cass::{format::LineCfg, Date, Time, TimeFile}; +use std::collections::BTreeMap; + +/// A set of IR parsed items that makes up a whole cass file +pub(crate) type IrStream = Vec<IrItem>; + +/// Intermediate representation for parsing and generating files +/// +/// The CASS IR is largely based on the output of the parser's +/// [`LineCfg`](crate::format::LineCfg), but with concrete types used +/// in the data layer (namely [`Date`][date] and [`Time`][time]), +/// while also keeping track of the line numbers to allow idempotent +/// file changes. +/// +/// Something not yet implemented is comment pass-through (this needs +/// to happen in the parser first), but will likely be implemented in +/// a future version. +/// +/// [date]: crate::Date +/// [time]: crate::Time +#[derive(Debug, Clone)] +pub(crate) struct IrItem { + pub(crate) tt: IrType, + pub(crate) lo: usize, +} + +/// Disambiguate between different IR line types with their payload +#[derive(Debug, Clone)] +pub(crate) enum IrType { + /// A line with parsed header information + Header(BTreeMap<String, String>), + /// Start a session at a given timestapm + Start(Time), + /// Stop a session at a given timestamp + Stop(Time), + /// Invoice a block of previous work + Invoice(Date), + /// An item that gets ignored + Ignore, +} + +/// Generate a stream of IR items from the raw parser output +pub(crate) fn generate_ir(buf: impl Iterator<Item = LineCfg>) -> IrStream { + buf.enumerate().fold(vec![], |mut buf, (lo, item)| { + #[cfg_attr(rustfmt, rustfmt_skip)] + buf.push(match item { + LineCfg::Header(map) => IrItem { tt: IrType::Header(map.into_iter().map(|(k, v)| (k, v.replace(",", ""))).collect()), lo }, + LineCfg::Start(Some(time)) => IrItem { tt: IrType::Start(time.into()), lo }, + LineCfg::Stop(Some(time)) => IrItem { tt: IrType::Stop(time.into()), lo }, + LineCfg::Invoice(Some(date)) => IrItem { tt: IrType::Invoice(date.into()), lo }, + LineCfg::Ignore => IrItem { tt: IrType::Ignore, lo }, + _ => IrItem { tt: IrType::Ignore, lo }, + }); + + buf + }) +} + +pub(crate) trait MakeIr { + /// Make a new IR line from an object + fn make_ir(&self) -> IrType; +} + +pub(crate) fn clean_ir(ir: &mut IrStream) { + ir.remove(0); // FIXME: this is required to remove the leading + // comment, which will be manually re-generated at + // the moment, but which would just add more blank + // lines between the new comment, and the first line + // in this current format. This is very bad, yikes + // yikes yikes, but what can I do, I have a deadline + // (not really) lol + + // FIXME: this hack gets rid of a trailing empty line if it exists + // to make sure we don't have any gaps between work sessions. + if match ir.last() { + Some(IrItem { + tt: IrType::Ignore, .. + }) => true, + _ => false, + } { + ir.pop(); + } +} + +/// Taken an IrType and append it to an existing IR stream +pub(crate) fn append_ir(ir: &mut IrStream, tt: IrType) { + let lo = ir.last().unwrap().lo; + ir.push(IrItem { tt, lo }); +} + +/// Search for the header that contains the version string and update it +pub(crate) fn update_header(ir: &mut IrStream) { + ir.iter_mut().for_each(|item| match item.tt { + IrType::Header(ref mut map) if map.contains_key("version") => { + map.insert("version".into(), crate::cass::meta::VERSION.into()); + } + _ => {} + }); +} diff --git a/apps/koffice/libko/src/cass/format/lexer.rs b/apps/koffice/libko/src/cass/format/lexer.rs new file mode 100644 index 000000000000..bdb89f5180e5 --- /dev/null +++ b/apps/koffice/libko/src/cass/format/lexer.rs @@ -0,0 +1,151 @@ +//! Cassiopeia file lexer + +use logos::{Lexer, Logos}; +use std::iter::Iterator; + +/// A basic line lexer type +/// +/// This lexer distinguishes between comments, and keyword lines. It +/// does not attempt to parse the line specifics. This is what the +/// content lexer is for. +#[derive(Logos, Debug, PartialEq)] +pub(crate) enum Token { + #[token("HEADER")] + Header, + + #[token("START")] + Start, + + #[token("STOP")] + Stop, + + #[token("INVOICE")] + Invoice, + + #[regex(r"\w+=[^,$]+[,$]")] + HeaderData, + + // FIXME: this will have a leading whitespace that we could remove + // with ^\w, but logos does not support this at the moment + #[regex(r"[0-9-:+ ]+")] + Date, + + #[token(" ", logos::skip)] + Space, + + #[regex(";;.*")] + Comment, + + #[error] + Error, +} + +/// A single token type on a line +#[derive(Debug)] +pub(crate) struct LineToken<'l> { + pub(crate) tt: Token, + pub(crate) slice: &'l str, +} + +/// A lexer wrapped for a single line +pub(crate) struct LineLexer<'l> { + lexer: Lexer<'l, Token>, +} + +impl<'l> LineLexer<'l> { + pub(crate) fn get_all(self) -> Vec<LineToken<'l>> { + let mut acc = vec![]; + for l in self { + acc.push(l); + } + acc + } +} + +impl<'l> Iterator for LineLexer<'l> { + type Item = LineToken<'l>; + + fn next(&mut self) -> Option<Self::Item> { + self.lexer.next().map(|tt| Self::Item { + tt, + slice: self.lexer.slice(), + }) + } +} + +/// Take a line of input and lex it into a stream of tokens +pub(crate) fn lex<'l>(line: &'l mut String) -> LineLexer<'l> { + LineLexer { + lexer: Token::lexer(line), + } +} + +#[test] +fn basic_header() { + let mut lex = Token::lexer("HEADER version=0.0.0,location=Berlin Lichtenberg,"); + + assert_eq!(lex.next(), Some(Token::Header)); + assert_eq!(lex.span(), 0..6); + assert_eq!(lex.slice(), "HEADER"); + + assert_eq!(lex.next(), Some(Token::HeaderData)); + assert_eq!(lex.span(), 7..21); + assert_eq!(lex.slice(), "version=0.0.0,"); + + assert_eq!(lex.next(), Some(Token::HeaderData)); + assert_eq!(lex.span(), 21..49); + assert_eq!(lex.slice(), "location=Berlin Lichtenberg,"); + + assert_eq!(lex.next(), None); +} + +#[test] +fn basic_start() { + let mut lex = Token::lexer("START 2020-11-11 13:00:00+01:00"); + + assert_eq!(lex.next(), Some(Token::Start)); + assert_eq!(lex.span(), 0..5); + assert_eq!(lex.slice(), "START"); + + assert_eq!(lex.next(), Some(Token::Date)); + assert_eq!(lex.span(), 5..31); + assert_eq!(lex.slice(), " 2020-11-11 13:00:00+01:00"); + + assert_eq!(lex.next(), None); +} + +#[test] +fn basic_stop() { + let mut lex = Token::lexer("STOP 2020-11-11 13:00:00+01:00"); + + assert_eq!(lex.next(), Some(Token::Stop)); + assert_eq!(lex.span(), 0..4); + assert_eq!(lex.slice(), "STOP"); + + assert_eq!(lex.next(), Some(Token::Date)); + assert_eq!(lex.span(), 4..30); + assert_eq!(lex.slice(), " 2020-11-11 13:00:00+01:00"); + + assert_eq!(lex.next(), None); +} + +#[test] +fn basic_invoice() { + let mut lex = Token::lexer("INVOICE 2020-11-11 13:00:00+01:00"); + + assert_eq!(lex.next(), Some(Token::Invoice)); + assert_eq!(lex.span(), 0..7); + assert_eq!(lex.slice(), "INVOICE"); + + assert_eq!(lex.next(), Some(Token::Date)); + assert_eq!(lex.span(), 7..33); + assert_eq!(lex.slice(), " 2020-11-11 13:00:00+01:00"); + + assert_eq!(lex.next(), None); +} + +#[test] +fn basic_comment() { + let mut lex = Token::lexer(";; This file is auto generated!"); + assert_eq!(lex.next(), Some(Token::Comment)); +} diff --git a/apps/koffice/libko/src/cass/format/mod.rs b/apps/koffice/libko/src/cass/format/mod.rs new file mode 100644 index 000000000000..2983653898b6 --- /dev/null +++ b/apps/koffice/libko/src/cass/format/mod.rs @@ -0,0 +1,76 @@ +//! cassiopeia file format + +mod gen; +pub(crate) mod ir; +mod lexer; +mod parser; + +pub(crate) use lexer::{LineLexer, LineToken, Token}; +pub(crate) use parser::LineCfg; + +use crate::{ + cass::error::{ParseError, ParseResult}, + cass::TimeFile, +}; +use ir::{IrItem, IrStream}; +use std::{ + fs::{File, OpenOptions}, + io::{Read, Write}, +}; + +/// A crate internal representation of the IR stream and timefile +#[derive(Default)] +pub(crate) struct ParseOutput { + pub(crate) ir: IrStream, + pub(crate) tf: TimeFile, +} + +impl ParseOutput { + fn append(mut self, ir: IrItem) -> ParseResult<Self> { + self.tf.append(ir.clone())?; + self.ir.push(ir); + Ok(self) + } +} + +/// Load a file from disk and parse it into a +/// [`TimeFile`](crate::TimeFile) +pub(crate) fn load_file(path: &str) -> ParseResult<ParseOutput> { + // Load the raw file contents + let mut f = File::open(path)?; + let mut content = String::new(); + f.read_to_string(&mut content)?; + + // Split the file by lines - .cass is a line based format + let mut lines: Vec<String> = content.split("\n").map(|l| l.to_owned()).collect(); + + // Build an iterator over parsed lines + let parsed = lines + .iter_mut() + .map(|line| lexer::lex(line)) + .map(|lex| parser::parse(lex)); + + // Generate the IR from parse output, then build the timefile + ir::generate_ir(parsed) + .into_iter() + .fold(Ok(ParseOutput::default()), |out, ir| match out { + Ok(out) => out.append(ir), + e @ Err(_) => e, + }) +} + +/// Write a file with the updated IR stream +pub(crate) fn write_file(path: &str, ir: &mut IrStream) -> ParseResult<()> { + ir::update_header(ir); + let mut lines = ir.into_iter().map(|ir| gen::line(ir)).collect::<Vec<_>>(); + lines.insert(0, gen::head_comment()); + + // let mut f = OpenOptions::new() + // .write(true) + // .create(true) + // .truncate(true) + // .open(path) + // .ok()?; + // f.write_all(lines.join("\n").as_bytes()).ok()?; + Ok(()) +} diff --git a/apps/koffice/libko/src/cass/format/parser.rs b/apps/koffice/libko/src/cass/format/parser.rs new file mode 100644 index 000000000000..8e0602d440d2 --- /dev/null +++ b/apps/koffice/libko/src/cass/format/parser.rs @@ -0,0 +1,73 @@ +//! cassiopeia parser +//! +//! Takes a lexer's token stream as an input, and outputs a fully +//! parsed time file. + +use crate::cass::format::{LineLexer, LineToken, Token}; +use chrono::{DateTime, FixedOffset as Offset, NaiveDate}; +use std::collections::BTreeMap; +use std::iter::Iterator; + +/// A type-parsed line in a time file +#[derive(Debug)] +pub enum LineCfg { + /// A header line with a set of keys and values + Header(BTreeMap<String, String>), + /// A session start line with a date and time + Start(Option<DateTime<Offset>>), + /// A session stop line with a date and time + Stop(Option<DateTime<Offset>>), + /// An invoice line with a date + Invoice(Option<NaiveDate>), + /// A temporary value that is invalid + #[doc(hidden)] + Ignore, +} + +pub(crate) fn parse<'l>(lex: LineLexer<'l>) -> LineCfg { + use LineCfg::*; + use Token as T; + + #[cfg_attr(rustfmt, rustfmt_skip)] + lex.get_all().into_iter().fold(Ignore, |cfg, tok| match (cfg, tok) { + // If the first token is a comment, we ignore it + (Ignore, LineToken { tt: T::Comment, .. }, ) => Ignore, + // If the first token is a keyword, we wait for more data + (Ignore, LineToken { tt: T::Header, .. }) => Header(Default::default()), + (Ignore, LineToken { tt: T::Start, .. }) => Start(None), + (Ignore, LineToken { tt: T::Stop, .. }) => Stop(None), + (Ignore, LineToken { tt: T::Invoice, .. }) => Invoice(None), + + // If the first token _was_ a keyword, fill in the data + (Header(map), LineToken { tt: T::HeaderData, slice }) => Header(append_data(map, slice)), + (Start(_), LineToken { tt: T::Date, slice }) => Start(parse_datetime(slice)), + (Stop(_), LineToken { tt: T::Date, slice }) => Stop(parse_datetime(slice)), + (Invoice(_), LineToken { tt: T::Date, slice }) => Invoice(parse_date(slice)), + + // Pass empty lines through, + (empty, _) => empty, + + // Ignore everything else (which will be filtered) + _ => Ignore, + }) +} + +fn append_data(mut map: BTreeMap<String, String>, slice: &str) -> BTreeMap<String, String> { + let split = slice.split("=").collect::<Vec<_>>(); + map.insert(split[0].into(), split[1].into()); + map +} + +fn parse_datetime(slice: &str) -> Option<DateTime<Offset>> { + Some( + DateTime::parse_from_str(slice, "%Y-%m-%d %H:%M:%S%:z") + .expect("Failed to parse date; invalid format!"), + ) +} + +fn parse_date(slice: &str) -> Option<NaiveDate> { + Some( + NaiveDate::parse_from_str(slice, "%Y-%m-%d") + .expect("Failed to parse date; invalid format!"), + ) +} diff --git a/apps/koffice/libko/src/cass/meta.rs b/apps/koffice/libko/src/cass/meta.rs new file mode 100644 index 000000000000..ec71498986e3 --- /dev/null +++ b/apps/koffice/libko/src/cass/meta.rs @@ -0,0 +1,46 @@ +//! Metadata and strings for this application +// TODO: translate this + +pub const NAME: &'static str = env!("CARGO_PKG_NAME"); +pub const VERSION: &'static str = env!("CARGO_PKG_VERSION"); +pub const AUTHOR: &'static str = env!("CARGO_PKG_AUTHORS"); +pub const ABOUT: &'static str = env!("CARGO_PKG_DESCRIPTION"); + +pub const ARG_FILE: &'static str = "CASS_FILE"; +pub const ARG_FILE_ABOUT: &'static str = "Provide a .cass file to operate on"; + +pub const CMD_START: &'static str = "start"; +pub const CMD_START_ABOUT: &'static str = "Start a work session"; + +pub const CMD_STOP: &'static str = "stop"; +pub const CMD_STOP_ABOUT: &'static str = "Stop the current work session"; + +pub const ARG_ROUNDING: &'static str = "CASS_ROUNDING"; +pub const ARG_ROUNDING_ABOUT: &'static str = "Disable the (default) 15 minute rounding period"; + +pub const CMD_INVOICE: &'static str = "invoice"; +pub const CMD_INVOICE_ABOUT: &'static str = "Create an invoice. You get to choose between simply adding a \ +statement to your time file, or generating .yml configuration to build an invoice generator from. See invoice(1) \ +for more detail!"; + +pub const CMD_UPDATE: &'static str = "update"; +pub const CMD_UPDATE_ABOUT: &'static str = "Update the selected file to a new version"; + +pub const ARG_CLIENT: &'static str = "CLIENT"; +pub const ARG_CLIENT_ABOUT: &'static str = + "Provide the name of the current client for invoice generation"; + +pub const ARG_PROJECT: &'static str = "PROJECT"; +pub const ARG_PROJECT_ABOUT: &'static str = + "Provide the name of the current project for invoice generation"; + +pub const ARG_GEN_YAML: &'static str = "GEN_YAML"; +pub const ARG_GEN_YAML_ABOUT: &'static str = + "Specify whether to generate a .yml invoice configuration"; + +pub const ARG_CLIENT_DB: &'static str = "CLIENT_DB"; +pub const ARG_CLIENT_DB_ABOUT: &'static str = + "Provide your client database file (.yml format) used by invoice(1)"; + +pub const CMD_STAT: &'static str = "stat"; +pub const CMD_STAT_ABOUT: &'static str = "Get statistics of previous work sessions"; diff --git a/apps/koffice/libko/src/cass/mod.rs b/apps/koffice/libko/src/cass/mod.rs new file mode 100644 index 000000000000..43dd79b0fbd7 --- /dev/null +++ b/apps/koffice/libko/src/cass/mod.rs @@ -0,0 +1,160 @@ +//! Cassiopeia plain text time tracking tool +//! +//! Versions `0.1` and `0.2` were written in Ruby and are thus +//! deprecated. Most likely you are interested in `cass(1)`, the +//! simple plain text time tracking utility, part of the kookie-office +//! suite of commandline tools! This is the library powering it. +//! +//! For more documentation, check out: +//! https://git.spacekookie.de/kookienomicon/tree/apps/cassiopeia + +mod data; +mod date; +mod format; +mod time; +mod timeline; + +pub mod error; +pub mod meta; + +pub use date::Date; +pub use time::Time; + +pub(crate) use data::TimeFile; +use data::{Invoice, Session}; +use error::{ParseError, ParseResult}; +use format::{ + ir::{append_ir, clean_ir, IrStream, MakeIr}, + ParseOutput, +}; + +/// A state handler and primary API for all cass interactions +/// +/// +pub struct Cassiopeia { + path: String, + tf: TimeFile, + ir: IrStream, +} + +impl Cassiopeia { + /// Load a cass file from disk, parsing it into a [`TimeFile`](crate::TimeFile) + pub fn load(path: &str) -> ParseResult<Self> { + let path = path.to_owned(); + format::load_file(path.as_str()).map(|ParseOutput { tf, ir }| Self { path, tf, ir }) + } + + pub(crate) fn timefile(&self) -> TimeFile { + self.tf.clone() + } + + /// Start a new work session (with optional 15 minute rounding) + pub fn start(&mut self, round: bool) -> ParseResult<()> { + let delta = self.tf.timeline.start(Time::rounded(round))?; + clean_ir(&mut self.ir); + append_ir(&mut self.ir, delta.make_ir()); + format::write_file(self.path.as_str(), &mut self.ir) + } + + /// Stop the existing work session (with optional 15 minute rounding) + pub fn stop(&mut self, round: bool) -> ParseResult<()> { + let delta = self.tf.timeline.stop(Time::rounded(round))?; + clean_ir(&mut self.ir); + append_ir(&mut self.ir, delta.make_ir()); + format::write_file(self.path.as_str(), &mut self.ir) + } + + /// Add an invoice block to the time file + pub fn invoice<'slf>(&'slf mut self) -> Invoicer<'slf> { + Invoicer::new(self) + } + + /// Write out the file IR as is, updating only the header version + pub fn update(&mut self) -> ParseResult<()> { + clean_ir(&mut self.ir); + format::write_file(self.path.as_str(), &mut self.ir) + } + + /// Collect statistics on previous work sessions + pub fn stat(&self) -> ParseResult<String> { + todo!() + } +} + +/// An invoice generator builder +/// +/// The most simple use-case of this type is to provide no parameters +/// and simply add an `INVOICE` line to the cass file. Adittionally +/// you may provide the client and project name, which will then +/// require the `client_db` path to be set as well. +/// +/// ```rust,no_run +/// # let mut cass = cassiopeia::Cassiopeia::load("").unwrap(); +/// cass.invoice().run(); +/// ``` +/// +/// Additional errors can be thrown if the client or project are not +/// known in the client db. +/// +/// ```rust,no_run +/// # let mut cass = cassiopeia::Cassiopeia::load("").unwrap(); +/// cass.invoice() +/// .generate() +/// .db("/home/office/clients.yml".into()) +/// .client("ACME".into()) +/// .run(); +/// ``` +#[allow(unused)] +pub struct Invoicer<'cass> { + tf: &'cass mut Cassiopeia, + generate: bool, + client_db: String, + client: String, + project: String, +} + +impl<'cass> Invoicer<'cass> { + pub fn new(tf: &'cass mut Cassiopeia) -> Self { + Self { + tf, + generate: false, + client_db: String::new(), + client: String::new(), + project: String::new(), + } + } + + /// Enable the invoice generation feature + pub fn generate(self) -> Self { + Self { + generate: true, + ..self + } + } + + /// Provide the client database file (.yml format) + pub fn db(self, client_db: String) -> Self { + Self { client_db, ..self } + } + + /// Provide the client to invoice + pub fn client(self, client: String) -> Self { + Self { client, ..self } + } + + pub fn project(self, project: String) -> Self { + Self { project, ..self } + } + + pub fn run(mut self) -> ParseResult<()> { + if self.generate { + eprintln!("Integration with invoice(1) is currently not implemented. Sorry :("); + return Err(ParseError::Unknown); + } + + let delta = self.tf.tf.timeline.invoice(Date::today())?; + clean_ir(&mut self.tf.ir); + append_ir(&mut self.tf.ir, delta.make_ir()); + format::write_file(self.tf.path.as_str(), &mut self.tf.ir) + } +} diff --git a/apps/koffice/libko/src/cass/time.rs b/apps/koffice/libko/src/cass/time.rs new file mode 100644 index 000000000000..48d4d5f9be2e --- /dev/null +++ b/apps/koffice/libko/src/cass/time.rs @@ -0,0 +1,141 @@ +use crate::cass::Date; +use chrono::{ + DateTime, Duration, FixedOffset as Offset, Local, NaiveDateTime, NaiveTime, TimeZone, Timelike, + Utc, +}; +use std::{cmp::Ordering, ops::Sub}; + +/// A convenience wrapper around [DateTime][t] with fixed timezone +/// +/// [t]: chrono::DateTime +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub struct Time { + inner: DateTime<Offset>, +} + +impl From<DateTime<Offset>> for Time { + fn from(inner: DateTime<Offset>) -> Self { + Self { inner } + } +} + +impl<'t> Sub for &'t Time { + type Output = Duration; + + fn sub(self, o: &'t Time) -> Self::Output { + self.inner - o.inner + } +} + +impl ToString for Time { + fn to_string(&self) -> String { + format!("{}", self.inner.format("%Y-%m-%d %H:%M:%S%:z")) + } +} + +impl Time { + /// Get the current local time and pin it to a fixed Tz offset + pub fn now() -> Self { + let now = Local::now(); + Self { + inner: build_datetime( + now.time() + .with_second(0) + .and_then(|t| t.with_nanosecond(0)) + .unwrap(), + ), + } + } + + /// Get the time that might be rounded to the next 15 minutes + pub(crate) fn rounded(r: bool) -> Self { + if r { + Time::now().round() + } else { + Time::now() + } + } + + pub(crate) fn date(&self) -> chrono::Date<Offset> { + self.inner.date() + } + + /// Check if a time stamp happened _after_ a date + pub fn after(&self, date: &Date) -> bool { + &Date::from(self.date()) > date + } + + #[cfg(test)] + pub(crate) fn fixed(hour: u32, min: u32, sec: u32) -> Self { + Self { + inner: build_datetime(NaiveTime::from_hms(hour, min, sec)), + } + } + + /// Return a new instance that is rounded to nearest 15 minutes + /// + /// It uses the internally provided offset to do rounding, meaning + /// that the timezone information will not change, even when + /// rounding values that were created in a different timezone. + pub fn round(&self) -> Self { + let naive = self.inner.time(); + let (new_min, incr_hour) = match naive.minute() { + // 0-7 => (0, false) + m if m < 7 => (0, false), + // 7-22 => (15, false) + m if m >= 7 && m < 22 => (15, false), + // 22-37 => (30, false) + m if m >= 22 && m < 37 => (30, false), + // 37-52 => (45, false) + m if m >= 37 && m < 52 => (45, false), + // 52-59 => (0, true) + m if m >= 52 && m <= 59 => (0, true), + _ => unreachable!(), + }; + + let hour = naive.hour(); + let new = NaiveTime::from_hms(if incr_hour { hour + 1 } else { hour }, new_min, 0); + let offset = self.inner.offset(); + let date = self.inner.date(); + + Self { + inner: DateTime::from_utc(NaiveDateTime::new(date.naive_local(), new), *offset), + } + } + + pub fn hour(&self) -> u32 { + self.inner.hour() + } + + pub fn minute(&self) -> u32 { + self.inner.minute() + } + + pub fn second(&self) -> u32 { + self.inner.second() + } +} + +/// Build a DateTime with the current local fixed offset +fn build_datetime(nt: NaiveTime) -> DateTime<Offset> { + let date = Utc::now().date().naive_local(); + let offset = Local.offset_from_utc_date(&date); + + DateTime::from_utc(NaiveDateTime::new(date, nt), offset) +} + +#[test] +fn simple() { + let t = Time::fixed(10, 44, 0); + let round = t.round(); + assert_eq!(round.minute(), 45); + + let t = Time::fixed(6, 8, 0); + let round = t.round(); + assert_eq!(round.minute(), 15); + + let t = Time::fixed(6, 55, 0); + let round = t.round(); + assert_eq!(round.minute(), 0); + assert_eq!(round.hour(), 7); +} diff --git a/apps/koffice/libko/src/cass/timeline.rs b/apps/koffice/libko/src/cass/timeline.rs new file mode 100644 index 000000000000..057f0b47d7da --- /dev/null +++ b/apps/koffice/libko/src/cass/timeline.rs @@ -0,0 +1,132 @@ +use crate::cass::{ + data::{Delta, Invoice, Session}, + error::{UserError, UserResult}, + Date, Time, +}; + +/// A timeline entry of sessions and invoices +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] +pub 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 struct Timeline { + inner: Vec<Entry>, +} + +impl Timeline { + /// Take a set of sessions and invoices to sort into a timeline + pub 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 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 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 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 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)) + } +} diff --git a/apps/koffice/libko/src/client.rs b/apps/koffice/libko/src/client.rs new file mode 100644 index 000000000000..8e256160d3d3 --- /dev/null +++ b/apps/koffice/libko/src/client.rs @@ -0,0 +1,39 @@ +use chrono::NaiveDate; +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Worker { + pub name: String, + pub address: Address, + pub account: Account, +} + +/// An entry in the client database +#[derive(Debug, Serialize, Deserialize)] +pub struct Client { + pub name: String, + pub address: Address, + pub last_project: Option<NaiveDate>, +} + +/// An address with all associated data +#[derive(Debug, Serialize, Deserialize)] +pub struct Address { + pub name: String, + pub street: String, + pub no: String, + pub zip: String, + pub city: String, + pub country: String, +} + +/// A bank account with a account, and bank number +/// +/// This is kept as generically as possible, to allow as many +/// different account representations to work. +#[derive(Debug, Serialize, Deserialize)] +pub struct Account { + pub bank_name: String, + pub acc_num: String, + pub bank_num: String, +} diff --git a/apps/koffice/libko/src/config.rs b/apps/koffice/libko/src/config.rs new file mode 100644 index 000000000000..bef9c6fd2377 --- /dev/null +++ b/apps/koffice/libko/src/config.rs @@ -0,0 +1,5 @@ +use std::path::PathBuf; + +pub struct AppSettings { + +} diff --git a/apps/koffice/libko/src/invoice.rs b/apps/koffice/libko/src/invoice.rs new file mode 100644 index 000000000000..737513785916 --- /dev/null +++ b/apps/koffice/libko/src/invoice.rs @@ -0,0 +1,35 @@ +use chrono::NaiveDate; +use serde::{Deserialize, Serialize}; +use std::string::ToString; + +/// A specification to build invoice IDs with +#[derive(Serialize, Deserialize)] +pub enum InvoiceId { + YearMonthId(u16, u8, usize), +} + +impl ToString for InvoiceId { + fn to_string(&self) -> String { + match self { + Self::YearMonthId(yr, mo, id) => format!("#{}-{:02}-{:04}", yr, mo, id), + } + } +} + +/// An invoice for a specific project +#[derive(Serialize, Deserialize)] +pub struct Invoice { + id: InvoiceId, + client: String, + project: String, + date: NaiveDate, + amount: usize, + currency: String, + vat: u8, +} + +#[test] +fn invoice_id_fmt() { + let inv_id = InvoiceId::YearMonthId(2020, 06, 0055); + assert_eq!(inv_id.to_string(), "#2020-06-0055".to_string()); +} diff --git a/apps/koffice/libko/src/lib.rs b/apps/koffice/libko/src/lib.rs new file mode 100644 index 000000000000..33d6b38ae1b1 --- /dev/null +++ b/apps/koffice/libko/src/lib.rs @@ -0,0 +1,35 @@ +//! A library that provides basic building blocks of k-office tools + +pub mod cass; + +mod client; +pub use client::*; + +mod invoice; +pub use invoice::*; + +mod proj; +pub use proj::*; + +mod store; +pub use store::*; + +use serde::{de::DeserializeOwned, Serialize}; + +pub trait Io { + fn to_yaml(&self) -> String; + fn from_yaml(s: impl Into<String>) -> Self; +} + +impl<T> Io for T +where + T: Serialize + DeserializeOwned, +{ + fn to_yaml(&self) -> String { + serde_yaml::to_string(self).unwrap() + } + + fn from_yaml(s: impl Into<String>) -> Self { + serde_yaml::from_str(s.into().as_str()).unwrap() + } +} diff --git a/apps/koffice/libko/src/proj.rs b/apps/koffice/libko/src/proj.rs new file mode 100644 index 000000000000..1e370fc2aaaf --- /dev/null +++ b/apps/koffice/libko/src/proj.rs @@ -0,0 +1,15 @@ +use chrono::NaiveDate; +use serde::{Deserialize, Serialize}; + +/// Represent a project that is being done +#[derive(Serialize, Deserialize)] +pub struct Project { + client: String, + date: NaiveDate, +} + +impl Project { + pub fn new(client: String, date: NaiveDate) -> Self { + Self { client, date } + } +} diff --git a/apps/koffice/libko/src/store.rs b/apps/koffice/libko/src/store.rs new file mode 100644 index 000000000000..7ce33ce5fd51 --- /dev/null +++ b/apps/koffice/libko/src/store.rs @@ -0,0 +1,94 @@ +use crate::{ + cass::{Cassiopeia, TimeFile}, + Address, Client, Io, +}; +use serde::{Deserialize, Serialize}; +use std::collections::BTreeMap; +use std::{ + fs::File, + io::{Read, Write}, + path::PathBuf, +}; +use xdg::BaseDirectories as BaseDirs; + +#[derive(Debug)] +pub struct Meta { + clients: BTreeMap<String, Client>, + pub dir: BaseDirs, + pub invoice_dir: PathBuf, + pub template: Option<PathBuf>, + pub revisioning: bool, + + /// Optional current timefile path + pub timefile: Option<TimeFile>, + pub project_id: Option<String>, +} + +/// +#[derive(Debug, Serialize, Deserialize)] +pub struct Config { + pub revisioning: bool, + pub invoice_dir: PathBuf, +} + +impl Meta { + pub fn new(dir: BaseDirs) -> Self { + // Get the path to the configuration, and make sure a default + // configuration is created if none exists yet. + let path = dir.find_config_file("config.yml").unwrap_or_else(|| { + let path = dir.place_config_file("config.yml").unwrap(); + let mut cfg = File::create(path.clone()).unwrap(); + + let buf = "revisioning: true +invoicedir: $HOME/.local/k-office/"; + cfg.write_all(buf.as_bytes()).unwrap(); + path + }); + + let mut cfg = File::open(path).unwrap(); + + let mut buf = String::new(); + cfg.read_to_string(&mut buf).unwrap(); + let yml = Config::from_yaml(buf); + + Self { + dir, + clients: BTreeMap::new(), + invoice_dir: yml.invoice_dir, + template: None, + revisioning: yml.revisioning, + timefile: None, + project_id: None, + } + } + + pub fn load_timefile(&mut self, path: &str) { + let timefile = Cassiopeia::load(path) + .expect("Timefile not found") + .timefile(); + self.timefile = Some(timefile); + } + + pub fn client_mut(&mut self, name: &str) -> Option<&mut Client> { + self.clients.get_mut(name) + } + + pub fn new_client(&mut self, name: &str, address: Address) { + self.clients.insert( + name.to_string(), + Client { + name: name.to_string(), + address, + last_project: None, + }, + ); + } +} + +/// Initialise a k-office application state +pub fn initialise() -> Meta { + let dir = BaseDirs::with_prefix("k-koffice").unwrap(); + dir.create_config_directory("") + .expect("Couldn't create config directory"); + Meta::new(dir) +} diff --git a/apps/koffice/libko/src/timefile.rs b/apps/koffice/libko/src/timefile.rs new file mode 100644 index 000000000000..e69de29bb2d1 --- /dev/null +++ b/apps/koffice/libko/src/timefile.rs |