From 2771b247acf73e6166bcf2625a9446ea50e14d24 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Mon, 13 Sep 2021 12:24:19 +0200 Subject: [PATCH] Improve handling of empty valid `yaml` files (#716) Improves the way empty valid `yaml` files are handled. When deserializing a `config` or `layout` file, that is an empty valid `yaml` file, eg: ``` --- ``` We now assume the default configuration is desired. --- zellij-utils/src/input/config.rs | 23 ++++++++++++++--------- zellij-utils/src/input/layout.rs | 7 +++++-- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/zellij-utils/src/input/config.rs b/zellij-utils/src/input/config.rs index 8bee3d0a..0339d4dc 100644 --- a/zellij-utils/src/input/config.rs +++ b/zellij-utils/src/input/config.rs @@ -99,16 +99,21 @@ impl TryFrom<&CliArgs> for Config { impl Config { /// Uses defaults, but lets config override them. pub fn from_yaml(yaml_config: &str) -> ConfigResult { - let config_from_yaml: ConfigFromYaml = serde_yaml::from_str(yaml_config)?; - let keybinds = Keybinds::get_default_keybinds_with_config(config_from_yaml.keybinds); - let options = Options::from_yaml(config_from_yaml.options); - let themes = config_from_yaml.themes; + let config_from_yaml: Option = serde_yaml::from_str(yaml_config)?; - Ok(Config { - keybinds, - options, - themes, - }) + match config_from_yaml { + None => Ok(Config::default()), + Some(config) => { + let keybinds = Keybinds::get_default_keybinds_with_config(config.keybinds); + let options = Options::from_yaml(config.options); + let themes = config.themes; + Ok(Config { + keybinds, + options, + themes, + }) + } + } } /// Deserializes from given path. diff --git a/zellij-utils/src/input/layout.rs b/zellij-utils/src/input/layout.rs index 7d102451..98fdb0d1 100644 --- a/zellij-utils/src/input/layout.rs +++ b/zellij-utils/src/input/layout.rs @@ -103,9 +103,12 @@ impl LayoutFromYaml { let mut layout = String::new(); layout_file.read_to_string(&mut layout)?; - let layout: LayoutFromYaml = serde_yaml::from_str(&layout)?; + let layout: Option = serde_yaml::from_str(&layout)?; - Ok(layout) + match layout { + Some(layout) => Ok(layout), + None => Ok(LayoutFromYaml::default()), + } } // It wants to use Path here, but that doesn't compile.