Skip to content

Commit

Permalink
feat(config): Allow empty config files (#720)
Browse files Browse the repository at this point in the history
Fix #714

Allow empty `config` and `layout` files

- Currently empty files are parsed as yaml documents, since they
  are empty they are invalid yaml files and a deseralization error would
  follow.

  Now we ignore the incorrect yaml on an empty document and treat it as
  an empty yaml document.

  Eg:
  ```
  ```
  and
  ```
  ---
  ```

  Are now treated equally.

Alternative: Keep treating the files as `yaml` documents.
  • Loading branch information
a-kenji authored Oct 1, 2021
1 parent ee7b4a8 commit d667dc2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
12 changes: 11 additions & 1 deletion zellij-utils/src/input/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,17 @@ 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: Option<ConfigFromYaml> = serde_yaml::from_str(yaml_config)?;
let config_from_yaml: Option<ConfigFromYaml> = match serde_yaml::from_str(yaml_config) {
Err(e) => {
// needs direct check, as `[ErrorImpl]` is private
// https://github.com/dtolnay/serde-yaml/issues/121
if yaml_config.is_empty() {
return Ok(Config::default());
}
return Err(ConfigError::Serde(e));
}
Ok(config) => config,
};

match config_from_yaml {
None => Ok(Config::default()),
Expand Down
12 changes: 11 additions & 1 deletion zellij-utils/src/input/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,17 @@ impl LayoutFromYaml {

let mut layout = String::new();
layout_file.read_to_string(&mut layout)?;
let layout: Option<LayoutFromYaml> = serde_yaml::from_str(&layout)?;
let layout: Option<LayoutFromYaml> = match serde_yaml::from_str(&layout) {
Err(e) => {
// needs direct check, as `[ErrorImpl]` is private
// https://github.com/dtolnay/serde-yaml/issues/121
if layout.is_empty() {
return Ok(LayoutFromYaml::default());
}
return Err(ConfigError::Serde(e));
}
Ok(config) => config,
};

match layout {
Some(layout) => {
Expand Down

0 comments on commit d667dc2

Please sign in to comment.