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.
This commit is contained in:
a-kenji 2021-10-01 21:49:47 +02:00 committed by GitHub
parent ee7b4a85b0
commit d667dc2a87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 2 deletions

View file

@ -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()),

View file

@ -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) => {