Parcourir la source

[Main] User struct added

Signed-off-by: Slava Barinov <rayslava@gmail.com>
Slava Barinov il y a 4 ans
Parent
commit
58b5fed49f
1 fichiers modifiés avec 53 ajouts et 1 suppressions
  1. 53 1
      src/main.rs

+ 53 - 1
src/main.rs

@@ -85,6 +85,58 @@ fn gen_trans<'a>(
     }
 }
 
+/// Configuration for single user
+struct User {
+    /// Telegram user id
+    uid: i128,
+    /// Categories statistics for the user
+    catmap: CatStats,
+    /// Available accounts for the user
+    accounts: Vec<String>,
+}
+
+const default_db_path: &str = "~/.config/receqif/";
+
+fn prep_user(uid: i128) -> User {
+    let ten_sec = Duration::from_secs(10);
+    let path = default_db_path.to_owned() + &uid.to_string() + ".db";
+
+    let confpath: &str = &tilde(&path);
+
+    let confpath = PathBuf::from(confpath);
+
+    let db = PickleDb::load(
+        &confpath,
+        PickleDbDumpPolicy::PeriodicDump(ten_sec),
+        SerializationMethod::Json,
+    );
+
+    let mut db = match db {
+        Ok(db) => db,
+        Err(_) => PickleDb::new(
+            &confpath,
+            PickleDbDumpPolicy::PeriodicDump(ten_sec),
+            SerializationMethod::Json,
+        ),
+    };
+
+    let mut catmap: CatStats = match db.get("catmap") {
+        Some(v) => v,
+        None => Trie::new(),
+    };
+
+    let accounts = match db.get("accounts") {
+        Some(a) => a,
+        None => vec![],
+    };
+
+    User {
+        uid: uid,
+        catmap: catmap,
+        accounts: accounts,
+    }
+}
+
 /// Search for a pattern in a file and display the lines that contain it.
 #[derive(StructOpt)]
 struct Cli {
@@ -94,7 +146,7 @@ struct Cli {
     #[structopt(parse(from_os_str), long, help = "Accounts csv file")]
     accounts: Option<PathBuf>,
 
-    #[structopt(short, long, default_value = "~/.config/receqif/rc.db")]
+    #[structopt(short, long, default_value = &(default_db_path.to_owned() + "rc.db"))]
     database: String,
 }