use xdg; use rusqlite::{Result, Connection}; mod database; mod cli; use cli::{Cli, Commands}; use crate::database::Database; fn main() -> Result<()> { let cli = Cli::new(); match cli.action { Commands::Init => { if let Some(datapath) = cli.database_path { Database::init_database(&datapath)?; } else { let datapath = xdg::BaseDirectories::with_prefix("debt") .expect("xdg file setup failed") .place_data_file("cache.db") .expect("xdg directory creaton failed"); Database::init_database(&datapath)?; } }, Commands::Register {person, amount, note, payment} => { let register = Database::new(person, amount as i32, note, !payment); let datapath = cli.database_path.unwrap(); register.add_register(&datapath)?; }, Commands::View {agent, filter_note, history, total} => { let datapath = cli.database_path.expect("database path not found"); let conn = Connection::open(&datapath)?; match (history, total) { (true, false) => Database::view_history(conn, agent, filter_note)?, (false, true) => Database::view_total(conn)?, (false, false) => Database::view_total(conn)?, (true, true) => unreachable!() } }, Commands::Agent {name, add, delete, update} => { let register = Database::new(name, 0, None, false); let datapath = cli.database_path.unwrap(); match (add, delete, update) { (true, false, None) => register.add_agent(&datapath)?, (false, true, None) => register.delete_agent(&datapath)?, (false, false, None) => register.add_agent(&datapath)?, (false, false, Some(new_name)) => register.update_agent(&datapath, new_name)?, (_, _, _) => unreachable!() } } }; Ok(()) }