From 51972801de888f475d03e968a97ff01f0a2ebd7a Mon Sep 17 00:00:00 2001 From: David Knaack Date: Wed, 31 Mar 2021 20:13:23 +0200 Subject: [PATCH] feat(config): warn about unknown config key names (#2527) --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/config.rs | 29 ++++++++++++------------ src/configs/starship_root.rs | 29 +++++++++++++++++++++--- starship_module_config_derive/Cargo.toml | 2 +- starship_module_config_derive/src/lib.rs | 27 +++++++++++----------- 6 files changed, 57 insertions(+), 34 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4372916f..bdcc14cc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1628,7 +1628,7 @@ dependencies = [ [[package]] name = "starship_module_config_derive" -version = "0.1.3" +version = "0.2.0" dependencies = [ "proc-macro2", "quote 1.0.9", diff --git a/Cargo.toml b/Cargo.toml index 9c7939b7..9589c082 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -48,7 +48,7 @@ once_cell = "1.7.2" chrono = "0.4.19" sys-info = "0.8.0" byte-unit = "4.0.10" -starship_module_config_derive = { version = "0.1.2", path = "starship_module_config_derive" } +starship_module_config_derive = { version = "0.2.0", path = "starship_module_config_derive" } yaml-rust = "0.4.5" pest = "2.1.3" pest_derive = "2.1.0" diff --git a/src/config.rs b/src/config.rs index 0e6a35df..2a5163b6 100644 --- a/src/config.rs +++ b/src/config.rs @@ -20,10 +20,9 @@ where /// Load root module config from given Value and fill unset variables with default /// values. fn load(config: &'a Value) -> Self { - if config.get("prompt_order").is_some() { - log::warn!("\"prompt_order\" has been removed in favor of \"format\". For more details, see: https://starship.rs/migrating-to-0.45.0/") - } - Self::default().load_config(config) + let mut out = Self::default(); + out.load_config(config); + out } /// Helper function that will call RootModuleConfig::load(config) if config is Some, @@ -50,8 +49,10 @@ where } /// Merge `self` with config from a toml table. - fn load_config(&self, config: &'a Value) -> Self { - Self::from_config(config).unwrap_or_else(|| self.clone()) + fn load_config(&mut self, config: &'a Value) { + if let Some(value) = Self::from_config(config) { + let _ = std::mem::replace(self, value); + } } } @@ -492,12 +493,12 @@ mod tests { disabled = true some_array = ["A"] }; - let default_config = TestConfig { + let mut rust_config = TestConfig { symbol: "S ", disabled: false, some_array: vec!["A", "B", "C"], }; - let rust_config = default_config.load_config(&config); + rust_config.load_config(&config); assert_eq!(rust_config.symbol, "T "); assert_eq!(rust_config.disabled, true); @@ -523,7 +524,7 @@ mod tests { modified = { value = "•", style = "red" } }; - let default_config = TestConfig { + let mut git_status_config = TestConfig { untracked: SegmentDisplayConfig { value: "?", style: Color::Red.bold(), @@ -533,7 +534,7 @@ mod tests { style: Color::Red.bold(), }, }; - let git_status_config = default_config.load_config(&config); + git_status_config.load_config(&config); assert_eq!( git_status_config.untracked, @@ -562,11 +563,11 @@ mod tests { let config = toml::toml! { optional = "test" }; - let default_config = TestConfig { + let mut rust_config = TestConfig { optional: None, hidden: None, }; - let rust_config = default_config.load_config(&config); + rust_config.load_config(&config); assert_eq!(rust_config.optional, Some("test")); assert_eq!(rust_config.hidden, None); @@ -607,12 +608,12 @@ mod tests { switch_a = "on" switch_b = "any" }; - let default_config = TestConfig { + let mut rust_config = TestConfig { switch_a: Switch::Off, switch_b: Switch::Off, switch_c: Switch::Off, }; - let rust_config = default_config.load_config(&config); + rust_config.load_config(&config); assert_eq!(rust_config.switch_a, Switch::On); assert_eq!(rust_config.switch_b, Switch::Off); diff --git a/src/configs/starship_root.rs b/src/configs/starship_root.rs index 4d26fd1c..cefa8507 100644 --- a/src/configs/starship_root.rs +++ b/src/configs/starship_root.rs @@ -1,9 +1,8 @@ -use crate::config::ModuleConfig; +use crate::{config::ModuleConfig, module::ALL_MODULES}; use serde::Serialize; -use starship_module_config_derive::ModuleConfig; -#[derive(Clone, ModuleConfig, Serialize)] +#[derive(Clone, Serialize)] pub struct StarshipRootConfig<'a> { pub format: &'a str, pub scan_timeout: u64, @@ -88,3 +87,27 @@ impl<'a> Default for StarshipRootConfig<'a> { } } } + +impl<'a> ModuleConfig<'a> for StarshipRootConfig<'a> { + fn load_config(&mut self, config: &'a toml::Value) { + if let toml::Value::Table(config) = config { + config.iter().for_each(|(k, v)| match k.as_str() { + "format" => self.format.load_config(v), + "scan_timeout" => self.scan_timeout.load_config(v), + "command_timeout" => self.command_timeout.load_config(v), + "add_newline" => self.add_newline.load_config(v), + unknown => { + if !ALL_MODULES.contains(&unknown) && unknown != "custom" { + log::warn!("Unknown config key '{}'", unknown); + } + } + }); + } + } + + fn from_config(config: &'a toml::Value) -> Option { + let mut out = Self::default(); + out.load_config(config); + Some(out) + } +} diff --git a/starship_module_config_derive/Cargo.toml b/starship_module_config_derive/Cargo.toml index 04352616..642d424e 100644 --- a/starship_module_config_derive/Cargo.toml +++ b/starship_module_config_derive/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "starship_module_config_derive" -version = "0.1.3" +version = "0.2.0" edition = "2018" authors = ["Matan Kushner "] homepage = "https://starship.rs" diff --git a/starship_module_config_derive/src/lib.rs b/starship_module_config_derive/src/lib.rs index 8594a902..254b0a3c 100644 --- a/starship_module_config_derive/src/lib.rs +++ b/starship_module_config_derive/src/lib.rs @@ -23,9 +23,7 @@ fn impl_module_config(dinput: DeriveInput) -> proc_macro::TokenStream { let ident = field.ident.as_ref().unwrap(); let new_load_tokens = quote! { - if let Some(config_str) = config.get(stringify!(#ident)) { - new_module_config.#ident = new_module_config.#ident.load_config(config_str); - } + stringify!(#ident) => self.#ident.load_config(v), }; load_tokens = quote! { @@ -35,23 +33,24 @@ fn impl_module_config(dinput: DeriveInput) -> proc_macro::TokenStream { } load_config = quote! { - fn load_config(&self, config: &'a toml::Value) -> Self { - let mut new_module_config = self.clone(); + fn load_config(&mut self, config: &'a toml::Value) { if let toml::Value::Table(config) = config { - if config.get("prefix").is_some() { - log::warn!("\"prefix\" has been removed in favor of \"format\". For more details, see: https://starship.rs/migrating-to-0.45.0/") - } - if config.get("suffix").is_some() { - log::warn!("\"suffix\" has been removed in favor of \"format\". For more details, see: https://starship.rs/migrating-to-0.45.0/") - } - #load_tokens + config.iter().for_each(|(k, v)| { + match k.as_str() { + #load_tokens + unknown => { + ::log::warn!("Unknown config key '{}'", unknown); + }, + } + }); } - new_module_config } }; from_config = quote! { fn from_config(config: &'a toml::Value) -> Option { - Some(Self::default().load_config(config)) + let mut out = Self::default(); + out.load_config(config); + Some(out) } }; }