diff options
author | a-kenji <aks.kenji@protonmail.com> | 2021-09-13 12:24:19 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-13 12:24:19 +0200 |
commit | 2771b247acf73e6166bcf2625a9446ea50e14d24 (patch) | |
tree | 0830e75f3d7e70252207cd6401a9762eb7cfab31 /zellij-utils | |
parent | b42ce60348dbb92708cebae661111c409eb2161b (diff) |
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.
Diffstat (limited to 'zellij-utils')
-rw-r--r-- | zellij-utils/src/input/config.rs | 25 | ||||
-rw-r--r-- | zellij-utils/src/input/layout.rs | 7 |
2 files changed, 20 insertions, 12 deletions
diff --git a/zellij-utils/src/input/config.rs b/zellij-utils/src/input/config.rs index 8bee3d0a0..0339d4dce 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; - - Ok(Config { - keybinds, - options, - themes, - }) + let config_from_yaml: Option<ConfigFromYaml> = serde_yaml::from_str(yaml_config)?; + + 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 7d1024514..98fdb0d12 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<LayoutFromYaml> = 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. |