Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
nilaysavant committed Aug 29, 2022
2 parents 2e91cd9 + 00a2231 commit 0290a4a
Showing 1 changed file with 28 additions and 25 deletions.
53 changes: 28 additions & 25 deletions src-tauri/src/models/app_db_state.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use std::{path::PathBuf, sync::{Arc, Mutex}};
use std::{
path::{Path, PathBuf},
sync::{Arc, Mutex},
};

use pickledb::{PickleDb, PickleDbDumpPolicy, SerializationMethod, error::ErrorType};
use log::info;
use pickledb::{error::ErrorType, PickleDb, PickleDbDumpPolicy, SerializationMethod};

/// Database state of the Application
/// #[derive(Clone)]
Expand All @@ -13,33 +17,32 @@ impl AppDbState {
/// Init a new db instance (ie. create/load)
/// and return a new db state struct
pub fn new(db_path: &PathBuf) -> Self {
match PickleDb::load(
db_path,
PickleDbDumpPolicy::AutoDump,
SerializationMethod::Json,
) {
Ok(db) => AppDbState {
// if the db is not found we create a new db instance
if Path::exists(&db_path) == false {
info!("db does not exist, creating new db...");
let db = PickleDb::new(
db_path,
PickleDbDumpPolicy::AutoDump,
SerializationMethod::Json,
);
AppDbState {
db: Arc::new(Mutex::new(db)),
},
Err(e) => {
match e.get_type() {
}
} else {
match PickleDb::load(
db_path,
PickleDbDumpPolicy::AutoDump,
SerializationMethod::Json,
) {
Ok(db) => AppDbState {
db: Arc::new(Mutex::new(db)),
},
Err(e) => match e.get_type() {
ErrorType::Io => {
if e.to_string() == "No such file or directory (os error 2)" {
// if the db is not found we create a new db instance
let db = PickleDb::new(
db_path,
PickleDbDumpPolicy::AutoDump,
SerializationMethod::Json,
);
AppDbState {
db: Arc::new(Mutex::new(db)),
}
} else {
panic!("db io error: {}", e)
}
panic!("db io error: {}", e)
}
ErrorType::Serialization => panic!("db serialization error: {}", e),
}
},
}
}
}
Expand Down

0 comments on commit 0290a4a

Please sign in to comment.