From 220fcc0d65e77a30f744e5539d4d266a8720b7a2 Mon Sep 17 00:00:00 2001 From: Meng Zhang Date: Mon, 7 Aug 2023 17:53:00 +0800 Subject: [PATCH] fix: make `config.experimental` optional (#339) * fix: make config.experimental` optional * add unit test for empty toml config --- crates/tabby-common/src/config.rs | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/crates/tabby-common/src/config.rs b/crates/tabby-common/src/config.rs index 8b9047f..52f86db 100644 --- a/crates/tabby-common/src/config.rs +++ b/crates/tabby-common/src/config.rs @@ -10,22 +10,30 @@ use crate::path::{config_file, repositories_dir}; #[derive(Deserialize, Default)] pub struct Config { + #[serde(default)] pub repositories: Vec, + + #[serde(default)] pub experimental: Experimental, } #[derive(Deserialize, Default)] pub struct Experimental { + #[serde(default = "default_as_false")] pub enable_prompt_rewrite: bool, } impl Config { pub fn load() -> Result { let file = serdeconv::from_toml_file(crate::path::config_file().as_path()); - file.map_err(|_| { + file.map_err(|err| { Error::new( ErrorKind::InvalidData, - format!("Config {:?} doesn't exist or is not valid", config_file()), + format!( + "Config {:?} doesn't exist or is not valid: `{:?}`", + config_file(), + err + ), ) }) } @@ -41,3 +49,18 @@ impl Repository { repositories_dir().join(filenamify(&self.git_url)) } } + +fn default_as_false() -> bool { + false +} + +#[cfg(test)] +mod tests { + use super::Config; + + #[test] + fn it_parses_empty_config() { + let config = serdeconv::from_toml_str::(""); + debug_assert!(config.is_ok(), "{}", config.err().unwrap()); + } +}