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.
This commit is contained in:
a-kenji 2021-09-13 12:24:19 +02:00 committed by GitHub
parent b42ce60348
commit 2771b247ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 11 deletions

View File

@ -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<ConfigFromYaml> = 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.

View File

@ -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.