From ba0be3e4a7ba719efbadae8fd761492babf1ad2f Mon Sep 17 00:00:00 2001 From: Terren <11605395+terreng@users.noreply.github.com> Date: Sat, 22 Oct 2022 11:51:41 -0400 Subject: [PATCH] Save and load config.json --- src-tauri/Cargo.lock | 21 +++++++++++++++++++++ src-tauri/Cargo.toml | 1 + src-tauri/src/main.rs | 20 ++++++++++++++++---- 3 files changed, 38 insertions(+), 4 deletions(-) diff --git a/src-tauri/Cargo.lock b/src-tauri/Cargo.lock index 3a5d3f6b..c75115ff 100644 --- a/src-tauri/Cargo.lock +++ b/src-tauri/Cargo.lock @@ -48,6 +48,7 @@ checksum = "98161a4e3e2184da77bb14f02184cdd111e83bbbcc9979dfee3c44b9a85f5602" name = "app" version = "2.0.0" dependencies = [ + "dirs", "serde", "serde_json", "tauri", @@ -525,6 +526,15 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + [[package]] name = "dirs-next" version = "2.0.0" @@ -535,6 +545,17 @@ dependencies = [ "dirs-sys-next", ] +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "dirs-sys-next" version = "0.1.2" diff --git a/src-tauri/Cargo.toml b/src-tauri/Cargo.toml index 3f83f1e6..a6889f02 100644 --- a/src-tauri/Cargo.toml +++ b/src-tauri/Cargo.toml @@ -18,6 +18,7 @@ tauri-build = { version = "1.1.1", features = [] } serde_json = "1.0" serde = { version = "1.0", features = ["derive"] } tauri = { version = "1.1.1", features = ["api-all"] } +dirs = "4.0.0" [features] # by default Tauri runs in production mode diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 0a2aebd0..070715d5 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -2,6 +2,12 @@ all(not(debug_assertions), target_os = "windows"), windows_subsystem = "windows" )] +#[cfg(target_os = "macos")] +static PLATFORM: &str = "macos"; +#[cfg(target_os = "linux")] +static PLATFORM: &str = "linux"; +#[cfg(target_os = "windows")] +static PLATFORM: &str = "windows"; use serde_json::{Value}; use serde_json::json; use std::fs::File; @@ -13,9 +19,15 @@ use std::path::Path; const VERSION : i32 = 1001004; const INSTALL_SOURCE : &str = "website"; +fn savepath() -> String { + let config_directory : String = dirs::config_dir().unwrap().display().to_string(); + let program_subdirectory : &str = if PLATFORM == "macos" {"/Simple Web Server/"} else {"\\Simple Web Server\\"}; + return config_directory + program_subdirectory; +} + fn main() { tauri::Builder::default() - .invoke_handler(tauri::generate_handler![init]) + .invoke_handler(tauri::generate_handler![init, saveconfig]) .run(tauri::generate_context!()) .expect("error while running tauri application"); } @@ -23,8 +35,8 @@ fn main() { #[tauri::command] fn init() -> Value { let mut config : Value = json!({}); - if Path::new("config.json").exists() { - let file = File::open("config.json").expect("Unable to open"); + if Path::new(&(savepath() + "config.json")).exists() { + let file = File::open(savepath() + "config.json").expect("Unable to open"); let reader = BufReader::new(file); config = serde_json::from_reader(reader).expect("Unable to read JSON"); } @@ -38,7 +50,7 @@ fn init() -> Value { #[tauri::command] fn saveconfig(config: Value) { let data = config.to_string(); - let f = File::create("config.json").expect("Unable to create file"); + let f = File::create(savepath() + "config.json").expect("Unable to create file"); let mut f = BufWriter::new(f); f.write_all(data.as_bytes()).expect("Unable to write data"); } \ No newline at end of file