From d667dc2a8707e6154ba3a182244ed8b553f27f06 Mon Sep 17 00:00:00 2001 From: a-kenji Date: Fri, 1 Oct 2021 21:49:47 +0200 Subject: feat(config): Allow empty config files (#720) 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. --- zellij-utils/src/input/config.rs | 12 +++++++++++- zellij-utils/src/input/layout.rs | 12 +++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) (limited to 'zellij-utils/src') diff --git a/zellij-utils/src/input/config.rs b/zellij-utils/src/input/config.rs index e55798e75..ecfe64c7c 100644 --- a/zellij-utils/src/input/config.rs +++ b/zellij-utils/src/input/config.rs @@ -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 = serde_yaml::from_str(yaml_config)?; + let config_from_yaml: Option = 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()), diff --git a/zellij-utils/src/input/layout.rs b/zellij-utils/src/input/layout.rs index 033614151..2bbc8cb48 100644 --- a/zellij-utils/src/input/layout.rs +++ b/zellij-utils/src/input/layout.rs @@ -161,7 +161,17 @@ impl LayoutFromYaml { let mut layout = String::new(); layout_file.read_to_string(&mut layout)?; - let layout: Option = serde_yaml::from_str(&layout)?; + let layout: Option = 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) => { -- cgit v1.2.3