diff --git a/src/config.rs b/src/config.rs index 349c452..bef1358 100644 --- a/src/config.rs +++ b/src/config.rs @@ -12,6 +12,16 @@ pub struct Config { pub debug_view_enabled: bool, } +impl Default for Config { + fn default() -> Self { + Config { + // TODO: handle first start with no profile selected + selected_profile_name: "".to_string(), + debug_view_enabled: false, + } + } +} + impl Config { pub fn get_selected_profile(&self, profiles: &Vec) -> Profile { match profiles.iter().find(|p| {p.name == self.selected_profile_name}) { @@ -19,58 +29,50 @@ impl Config { None => profiles.get(0).expect_dialog("No profiles found").clone(), } } -} -fn config_file_path() -> String { - format!( - "{config}/{name}.json", - config = get_config_dir(), - name = CMD_NAME - ) -} - -fn default_config() -> Config { - Config { - // TODO: handle first start with no profile selected - selected_profile_name: "".to_string(), - debug_view_enabled: false, + pub fn config_file_path() -> String { + format!( + "{config}/{name}.json", + config = get_config_dir(), + name = CMD_NAME + ) } -} -fn get_config_from_path(path_s: String) -> Config { - match File::open(path_s) { - Ok(file) => { - let reader = BufReader::new(file); - match serde_json::from_reader(reader) { - Ok(config) => config, - Err(_) => default_config(), + fn from_path(path_s: String) -> Self { + match File::open(path_s) { + Ok(file) => { + let reader = BufReader::new(file); + match serde_json::from_reader(reader) { + Ok(config) => config, + Err(_) => Self::default(), + } } + Err(_) => Self::default(), } - Err(_) => default_config(), } -} -pub fn get_config() -> Config { - get_config_from_path(config_file_path()) -} + fn save_to_path(&self, path_s: &String) -> Result<(), serde_json::Error> { + let writer = get_writer(path_s); + serde_json::to_writer_pretty(writer, self) + } -fn save_config_to_path(config: &Config, path_s: &String) -> Result<(), serde_json::Error> { - let writer = get_writer(path_s); - serde_json::to_writer_pretty(writer, config) -} + pub fn save(&self) { + self.save_to_path(&Self::config_file_path()).expect_dialog("Failed to save config"); + } -pub fn save_config(config: &Config) { - save_config_to_path(config, &config_file_path()).expect_dialog("Failed to save config"); + pub fn get_config() -> Self { + Self::from_path(Self::config_file_path()) + } } #[cfg(test)] mod tests { - use super::get_config_from_path; + use crate::config::Config; #[test] fn will_load_default_if_config_does_not_exist() { assert_eq!( - get_config_from_path("/non/existing/file.json".into()).debug_view_enabled, + Config::from_path("/non/existing/file.json".into()).debug_view_enabled, false ) } diff --git a/src/ui/app.rs b/src/ui/app.rs index e0b85c6..9bbd01a 100644 --- a/src/ui/app.rs +++ b/src/ui/app.rs @@ -8,7 +8,7 @@ use crate::builders::build_libsurvive::get_build_libsurvive_runner; use crate::builders::build_monado::get_build_monado_runner; use crate::builders::build_opencomposite::get_build_opencomposite_runner; use crate::builders::build_wivrn::get_build_wivrn_runner; -use crate::config::{get_config, save_config, Config}; +use crate::config::Config; use crate::constants::APP_NAME; use crate::dependencies::basalt_deps::get_missing_basalt_deps; use crate::dependencies::libsurvive_deps::get_missing_libsurvive_deps; @@ -211,7 +211,7 @@ impl SimpleComponent for App { Msg::EnableDebugViewChanged(val) => { self.set_enable_debug_view(val); self.config.debug_view_enabled = val; - save_config(&self.config); + self.config.save(); self.debug_view .sender() .emit(DebugViewMsg::EnableDebugViewChanged(val)); @@ -328,7 +328,7 @@ impl SimpleComponent for App { return; } self.config.selected_profile_name = prof_name; - save_config(&self.config); + self.config.save(); let profile = self.get_selected_profile(); self.main_view .sender() @@ -362,7 +362,7 @@ impl SimpleComponent for App { root: &Self::Root, sender: ComponentSender, ) -> ComponentParts { - let config = get_config(); + let config = Config::get_config(); let profiles = vec![ valve_index_profile(), system_valve_index_profile(),