Skip to content

Commit

Permalink
Improve handling of empty valid yaml files (#716)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
a-kenji committed Sep 13, 2021
1 parent b42ce60 commit 2771b24
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
25 changes: 15 additions & 10 deletions zellij-utils/src/input/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
7 changes: 5 additions & 2 deletions zellij-utils/src/input/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 2771b24

Please sign in to comment.