mod data;
mod date;
mod format;
pub mod meta;
mod time;
pub use date::Date;
pub use time::Time;
use data::{Invoice, Session, TimeFile};
use format::{
ir::{append_ir, clean_ir, IrStream, MakeIr},
ParseOutput,
};
pub struct Cassiopeia {
path: String,
tf: TimeFile,
ir: IrStream,
}
impl Cassiopeia {
pub fn load(path: &str) -> Option<Self> {
let path = path.to_owned();
format::load_file(path.as_str()).map(|ParseOutput { tf, ir }| Self { path, tf, ir })
}
pub fn store(&self) -> Option<()> {
Some(())
}
pub fn start(&mut self, round: bool) -> Option<()> {
let s = self.tf.start(round)?;
clean_ir(&mut self.ir);
append_ir(&mut self.ir, s.make_ir());
format::write_file(self.path.as_str(), &mut self.ir)
}
pub fn stop(&mut self, round: bool) -> Option<()> {
let s = self.tf.stop(round)?;
clean_ir(&mut self.ir);
append_ir(&mut self.ir, s.make_ir());
format::write_file(self.path.as_str(), &mut self.ir)
}
pub fn invoice<'slf>(&'slf mut self) -> Invoicer<'slf> {
Invoicer::new(self)
}
pub fn update(&mut self) -> Option<()> {
clean_ir(&mut self.ir);
format::write_file(self.path.as_str(), &mut self.ir)
}
}
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(),
}
}
pub fn generate(self) -> Self {
Self {
generate: true,
..self
}
}
pub fn db(self, client_db: String) -> Self {
Self { client_db, ..self }
}
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) -> Option<()> {
if self.generate {
eprintln!("Integration with invoice(1) is currently not implemented. Sorry :(");
return None;
}
let inv = self.tf.tf.invoice()?;
clean_ir(&mut self.tf.ir);
append_ir(&mut self.tf.ir, inv.make_ir());
format::write_file(self.tf.path.as_str(), &mut self.tf.ir)
}
}