aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
blob: be23ac31f4587b6ede31f9b77e6a6a93b6d7efb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use xdg;
use rusqlite::Result;

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 {history, filter, total} => {
            let datapath = cli.database_path.expect("database path not found");

            match (history, total) {
                (true, false) => Database::view_history(&datapath, filter)?,
                (false, true) => Database::view_total(&datapath)?,
                (false, false) => Database::view_total(&datapath)?,
                (true, true) => unreachable!()
            }
        },

        Commands::Add {name} => {
            let register = Database::new(name, 0, None, false);
            let datapath = cli.database_path.unwrap();
            register.add_agent(&datapath)?;
        }
    };
    Ok(())
}
Feel free to download, copy and edit any repo