From 785fa945d8f31e48bd62cab37bdc9bb37069f71c Mon Sep 17 00:00:00 2001 From: Ashelyn Rose Date: Sun, 19 Nov 2023 19:12:49 -0700 Subject: [PATCH] Save and load config file --- src-tauri/src/plugins/config.rs | 81 ++++++++++++++++++++++++++++++--- 1 file changed, 75 insertions(+), 6 deletions(-) diff --git a/src-tauri/src/plugins/config.rs b/src-tauri/src/plugins/config.rs index 75b1e00..d0eb99f 100644 --- a/src-tauri/src/plugins/config.rs +++ b/src-tauri/src/plugins/config.rs @@ -1,12 +1,20 @@ use std::sync::Mutex; use tauri::{ plugin::{Builder, TauriPlugin}, - Manager, Runtime, + Manager, Runtime, api::path::config_dir, }; use serde::{Serialize, Deserialize}; +use std::fs; +fn fls() -> bool { + false +} + +#[derive(Serialize, Deserialize, Clone)] struct AppConfig { - pub theme: ApplicationTheme + #[serde(skip, default="fls")] + pub write_file: bool, + pub theme: ApplicationTheme, } #[derive(Serialize, Deserialize, Clone)] @@ -16,16 +24,77 @@ enum ApplicationTheme { } impl AppConfig { - pub fn load() -> Self { - println!("Mock loading config"); - + fn default() -> Self { AppConfig { theme: ApplicationTheme::Light, + write_file: false, + } + } + + fn try_load() -> Option { + let path = config_dir()?.join("longmont/client.json"); + + let read_result = fs::read_to_string(path.clone()); + if let Err(error) = read_result { + println!("Unable to read file {}", path.to_str()?); + println!("{}", error); + return None + } + + let parse_result = serde_json::from_str(read_result.unwrap().as_str()); + if let Err(error) = parse_result { + println!("Invalid syntax in config file {}", path.to_str()?); + println!("{}", error); + return None + } + + Some(parse_result.unwrap()) + } + + pub fn load() -> Self { + // Check for config file + let path = config_dir().expect("cannot find config dir").join("longmont/client.json"); + if !path.exists() { + println!("No config file found, using defaults"); + let mut config = AppConfig::default(); + config.write_file = true; + return config } + + // If we can read it, we'll allow writing it back + if let Some(parsed_config) = AppConfig::try_load() { + println!("Loaded config file {}", path.to_str().unwrap()); + let mut config = parsed_config.clone(); + config.write_file = true; + return config + } + + // Otherwise, load defaults and don't allow writing it back + println!("Error encountered, using defaults"); + AppConfig::default() } pub fn save(&self) { - println!("Mock saving config") + if !self.write_file { + println!("Did not initially load config from a file, not saving"); + return + } + + let dir_path = config_dir().expect("cannot find config dir").join("longmont"); + let path = dir_path.join("client.json"); + + if !dir_path.exists() { + fs::create_dir(dir_path).expect("could not create config dir"); + } + + let config_str = serde_json::to_string(self).expect("Cannot serialize config"); + + if let Err(err) = fs::write(path.clone(), config_str) { + println!("Could not write config file {}", path.to_str().unwrap()); + println!("{}", err); + } else { + println!("Saved config to {}", path.to_str().unwrap()) + } } }