Bläddra i källkod

[Main] PickleDB storage added

Slava Barinov 5 år sedan
förälder
incheckning
3f6dfac845
4 ändrade filer med 108 tillägg och 17 borttagningar
  1. 67 0
      Cargo.lock
  2. 2 0
      Cargo.toml
  3. 17 0
      src/import.rs
  4. 22 17
      src/main.rs

+ 67 - 0
Cargo.lock

@@ -60,6 +60,12 @@ version = "1.4.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ae44d1a3d5a19df61dd0c8beb138458ac2a53a7ac09eba97d55592540004306b"
 
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
 [[package]]
 name = "chrono"
 version = "0.4.19"
@@ -110,12 +116,44 @@ dependencies = [
  "memchr",
 ]
 
+[[package]]
+name = "dirs-next"
+version = "2.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b98cf8ebf19c3d1b223e151f99a4f9f0690dca41414773390fc824184ac833e1"
+dependencies = [
+ "cfg-if",
+ "dirs-sys-next",
+]
+
+[[package]]
+name = "dirs-sys-next"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4ebda144c4fe02d1f7ea1a7d9641b6fc6b580adcfa024ae48797ecdeb6825b4d"
+dependencies = [
+ "libc",
+ "redox_users",
+ "winapi",
+]
+
 [[package]]
 name = "dtoa"
 version = "0.4.7"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "88d7ed2934d741c6b37e33e3832298e8850b53fd2d2bea03873375596c7cea4e"
 
+[[package]]
+name = "getrandom"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "4060f4657be78b8e766215b02b18a2e862d83745545de804638e2b545e81aee6"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
 [[package]]
 name = "half"
 version = "1.6.0"
@@ -261,9 +299,29 @@ dependencies = [
  "qif_generator",
  "serde",
  "serde_json",
+ "shellexpand",
  "structopt",
 ]
 
+[[package]]
+name = "redox_syscall"
+version = "0.2.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "05ec8ca9416c5ea37062b502703cd7fcb207736bc294f6e0cf367ac6fc234570"
+dependencies = [
+ "bitflags",
+]
+
+[[package]]
+name = "redox_users"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "528532f3d801c87aec9def2add9ca802fe569e44a544afe633765267840abe64"
+dependencies = [
+ "getrandom",
+ "redox_syscall",
+]
+
 [[package]]
 name = "regex-automata"
 version = "0.1.9"
@@ -332,6 +390,15 @@ dependencies = [
  "yaml-rust",
 ]
 
+[[package]]
+name = "shellexpand"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "83bdb7831b2d85ddf4a7b148aa19d0587eddbe8671a436b7bd1182eaad0f2829"
+dependencies = [
+ "dirs-next",
+]
+
 [[package]]
 name = "strsim"
 version = "0.8.0"

+ 2 - 0
Cargo.toml

@@ -9,5 +9,7 @@ serde = { version = "1.0", features = ["derive"] }
 serde_json = "1.0"
 qif_generator = "0.1.4"
 chrono = "0.4"
+pickledb = "0.4.1"
 csv = "1.1"
 structopt = "0.3"
+shellexpand = "2.1"

+ 17 - 0
src/import.rs

@@ -0,0 +1,17 @@
+use csv::ReaderBuilder;
+use std::error::Error;
+use std::path::Path;
+
+/// Read accounts from GnuCash csv export file. Only EXPENSE accounts make
+/// sense, since they're used as transaction categories.
+pub fn read_accounts(path: &Path) -> Result<Vec<String>, Box<dyn Error>> {
+    let mut rdr = ReaderBuilder::new().has_headers(true).from_path(path)?;
+    let mut result = Vec::<String>::new();
+    for e in rdr.records() {
+        let record = e?;
+        if record[0].eq("EXPENSE") {
+            result.push(String::from(&record[1]));
+        }
+    }
+    Ok(result)
+}

+ 22 - 17
src/main.rs

@@ -1,11 +1,14 @@
 use chrono::{Date, Utc};
+use pickledb::{PickleDb, PickleDbDumpPolicy, SerializationMethod};
 use qif_generator::{
     account::{Account, AccountType},
     split::Split,
     transaction::Transaction,
 };
-use std::env;
+use shellexpand::tilde;
 use std::fs;
+use std::path::{Path, PathBuf};
+use std::time::Duration;
 use structopt::StructOpt;
 
 mod import;
@@ -19,7 +22,6 @@ fn read_receipt(f: &str) -> receipt::Receipt {
 #[cfg(test)]
 mod tests {
     use super::*;
-    use std::path::PathBuf;
 
     #[test]
     fn test_read_receipt() {
@@ -78,26 +80,28 @@ struct Cli {
     filename: String,
 
     #[structopt(parse(from_os_str), long, help = "Accounts csv file")]
-    accounts: Option<std::path::PathBuf>,
-
-    #[structopt(
-        parse(from_os_str),
-        short,
-        long,
-        default_value = "~/.config/receqif/rc.db"
-    )]
-    database: Option<std::path::PathBuf>,
+    accounts: Option<PathBuf>,
+
+    #[structopt(short, long, default_value = "~/.config/receqif/rc.db")]
+    database: String,
 }
 
 #[cfg(not(tarpaulin_include))]
 fn main() {
     let args = Cli::from_args();
-    match args.accounts {
-        Some(filename) => {
-            let accounts = import::read_accounts(std::path::Path::new(&filename)).unwrap();
-            println!("{:?}", accounts);
-        }
-        None => {}
+
+    let confpath: &str = &tilde(&args.database);
+    let confpath = PathBuf::from(confpath);
+
+    let mut db = PickleDb::new(
+        confpath,
+        PickleDbDumpPolicy::PeriodicDump(Duration::from_secs(10)),
+        SerializationMethod::Json,
+    );
+
+    if let Some(filename) = args.accounts {
+        let accounts = import::read_accounts(Path::new(&filename)).unwrap();
+        db.set("accounts", &accounts).unwrap();
     }
 
     let receipt = read_receipt(&args.filename);
@@ -110,4 +114,5 @@ fn main() {
     let t = gen_trans(&acc, receipt.date(), receipt.total_sum(), &splits).unwrap();
     print!("{}", acc.to_string());
     println!("{}", t.to_string());
+    db.dump().unwrap();
 }