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:
parent
ee7b4a85b0
commit
d667dc2a87
2 changed files with 22 additions and 2 deletions
|
|
@ -106,7 +106,17 @@ impl TryFrom<&CliArgs> for Config {
|
||||||
impl Config {
|
impl Config {
|
||||||
/// Uses defaults, but lets config override them.
|
/// Uses defaults, but lets config override them.
|
||||||
pub fn from_yaml(yaml_config: &str) -> ConfigResult {
|
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 {
|
match config_from_yaml {
|
||||||
None => Ok(Config::default()),
|
None => Ok(Config::default()),
|
||||||
|
|
|
||||||
|
|
@ -161,7 +161,17 @@ impl LayoutFromYaml {
|
||||||
|
|
||||||
let mut layout = String::new();
|
let mut layout = String::new();
|
||||||
layout_file.read_to_string(&mut layout)?;
|
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 {
|
match layout {
|
||||||
Some(layout) => {
|
Some(layout) => {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue