Skip to content

Commit

Permalink
Save and load config.json
Browse files Browse the repository at this point in the history
  • Loading branch information
terreng committed Oct 26, 2024
1 parent 5805aad commit ba0be3e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
21 changes: 21 additions & 0 deletions src-tauri/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,18 +19,24 @@ 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");
}

#[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");
}
Expand All @@ -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");
}

0 comments on commit ba0be3e

Please sign in to comment.