Skip to content

Commit

Permalink
fix failed to read default config
Browse files Browse the repository at this point in the history
  • Loading branch information
SeakMengs committed Aug 23, 2023
1 parent 6c20e06 commit 35ccc0c
Showing 1 changed file with 7 additions and 19 deletions.
26 changes: 7 additions & 19 deletions src-tauri/src/app/utils.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use serde_json::json;
use std::path::{Path, PathBuf};
use std::{env, fs};
use std::env;
use tauri::api::path;
use tauri::{App, AppHandle};
use tauri_plugin_store::StoreBuilder;
Expand Down Expand Up @@ -51,17 +51,6 @@ pub fn combine_config_path(config_name: &str, folder_name: Option<String>) -> Op
);
}

pub fn read_file_to_json(path: &String) -> Option<serde_json::Value> {
let data = match fs::read_to_string(path) {
Ok(data) => data,
Err(err) => {
println!("Error reading file: {}", err);
return None;
}
};
return Some(serde_json::from_str(&data).unwrap());
}

pub fn if_app_config_does_not_exist_create_default(app: &mut App, config_name: &str) {
let setting_config_path = combine_config_path(config_name, None).unwrap();
let is_config_already_exist = Path::new(&setting_config_path).exists();
Expand All @@ -70,14 +59,13 @@ pub fn if_app_config_does_not_exist_create_default(app: &mut App, config_name: &
return;
}

let default_settings = convert_path(&("src/app/default/".to_owned() + config_name)).unwrap();
let json_data = match read_file_to_json(&default_settings) {
Some(data) => data,
None => {
println!("Error reading file: {}", default_settings);
return;
}
// because read_to_string can't read from binary file, so we use include_str! to read from project file
let data = match config_name {
"settings.json" => include_str!("default/settings.json"),
"pets.json" => include_str!("default/pets.json"),
_ => return,
};
let json_data: serde_json::Value = serde_json::from_str(data).unwrap();
let mut store = StoreBuilder::new(app.handle(), PathBuf::from(setting_config_path)).build();

// note that values must be serd_json::Value to be compatible with JS
Expand Down

0 comments on commit 35ccc0c

Please sign in to comment.