From e625a2278307968fafe8fe36e118800c10f6f696 Mon Sep 17 00:00:00 2001 From: Louis Dispa Date: Mon, 18 Mar 2024 11:36:33 +0100 Subject: [PATCH] fix: Avoid crash on malformed keymap config (#361) * fix: Crash on malformed config * fix: Have default mappings when missing config --------- Co-authored-by: Victoria Brekenfeld <4404502+Drakulix@users.noreply.github.com> --- src/config/mod.rs | 41 ++++++++++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 11 deletions(-) diff --git a/src/config/mod.rs b/src/config/mod.rs index 3fec7a1f..9b9ec9bf 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -207,20 +207,39 @@ impl Config { debug!("Trying config location: {}", path.display()); if path.exists() { info!("Using config at {}", path.display()); - let mut config: StaticConfig = - ron::de::from_reader(OpenOptions::new().read(true).open(path).unwrap()) - .expect("Malformed config file"); - - key_bindings::add_default_bindings(&mut config.key_bindings, workspace_layout); - - return config; + let Ok(file) = OpenOptions::new().read(true).open(path) else { + error!("Failed to open config file."); + continue; + }; + match ron::de::from_reader::<_, StaticConfig>(file) { + Ok(mut config) => { + key_bindings::add_default_bindings( + &mut config.key_bindings, + workspace_layout, + ); + return config; + } + Err(err) => { + error!("Malformed config file (skipping): {}", err); + continue; + } + } } } - StaticConfig { - key_bindings: HashMap::new(), - data_control_enabled: false, - } + info!("No config found, consider installing a config file. Using default mapping."); + + let mut config = ron::from_str(include_str!("../../config.ron")).unwrap_or_else(|err| { + debug!("Failed to load internal default config: {}", err); + StaticConfig { + // Small useful keybindings by default + key_bindings: HashMap::new(), + data_control_enabled: false, + } + }); + + key_bindings::add_default_bindings(&mut config.key_bindings, workspace_layout); + config } fn load_dynamic(xdg: Option<&xdg::BaseDirectories>) -> DynamicConfig {