feat(config): warn about unknown config key names (#2527)

This commit is contained in:
David Knaack 2021-03-31 20:13:23 +02:00 committed by GitHub
parent 9d15eb135b
commit 51972801de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 57 additions and 34 deletions

2
Cargo.lock generated
View File

@ -1628,7 +1628,7 @@ dependencies = [
[[package]] [[package]]
name = "starship_module_config_derive" name = "starship_module_config_derive"
version = "0.1.3" version = "0.2.0"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote 1.0.9", "quote 1.0.9",

View File

@ -48,7 +48,7 @@ once_cell = "1.7.2"
chrono = "0.4.19" chrono = "0.4.19"
sys-info = "0.8.0" sys-info = "0.8.0"
byte-unit = "4.0.10" 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" yaml-rust = "0.4.5"
pest = "2.1.3" pest = "2.1.3"
pest_derive = "2.1.0" pest_derive = "2.1.0"

View File

@ -20,10 +20,9 @@ where
/// Load root module config from given Value and fill unset variables with default /// Load root module config from given Value and fill unset variables with default
/// values. /// values.
fn load(config: &'a Value) -> Self { fn load(config: &'a Value) -> Self {
if config.get("prompt_order").is_some() { let mut out = Self::default();
log::warn!("\"prompt_order\" has been removed in favor of \"format\". For more details, see: https://starship.rs/migrating-to-0.45.0/") out.load_config(config);
} out
Self::default().load_config(config)
} }
/// Helper function that will call RootModuleConfig::load(config) if config is Some, /// 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. /// Merge `self` with config from a toml table.
fn load_config(&self, config: &'a Value) -> Self { fn load_config(&mut self, config: &'a Value) {
Self::from_config(config).unwrap_or_else(|| self.clone()) if let Some(value) = Self::from_config(config) {
let _ = std::mem::replace(self, value);
}
} }
} }
@ -492,12 +493,12 @@ mod tests {
disabled = true disabled = true
some_array = ["A"] some_array = ["A"]
}; };
let default_config = TestConfig { let mut rust_config = TestConfig {
symbol: "S ", symbol: "S ",
disabled: false, disabled: false,
some_array: vec!["A", "B", "C"], 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.symbol, "T ");
assert_eq!(rust_config.disabled, true); assert_eq!(rust_config.disabled, true);
@ -523,7 +524,7 @@ mod tests {
modified = { value = "", style = "red" } modified = { value = "", style = "red" }
}; };
let default_config = TestConfig { let mut git_status_config = TestConfig {
untracked: SegmentDisplayConfig { untracked: SegmentDisplayConfig {
value: "?", value: "?",
style: Color::Red.bold(), style: Color::Red.bold(),
@ -533,7 +534,7 @@ mod tests {
style: Color::Red.bold(), style: Color::Red.bold(),
}, },
}; };
let git_status_config = default_config.load_config(&config); git_status_config.load_config(&config);
assert_eq!( assert_eq!(
git_status_config.untracked, git_status_config.untracked,
@ -562,11 +563,11 @@ mod tests {
let config = toml::toml! { let config = toml::toml! {
optional = "test" optional = "test"
}; };
let default_config = TestConfig { let mut rust_config = TestConfig {
optional: None, optional: None,
hidden: 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.optional, Some("test"));
assert_eq!(rust_config.hidden, None); assert_eq!(rust_config.hidden, None);
@ -607,12 +608,12 @@ mod tests {
switch_a = "on" switch_a = "on"
switch_b = "any" switch_b = "any"
}; };
let default_config = TestConfig { let mut rust_config = TestConfig {
switch_a: Switch::Off, switch_a: Switch::Off,
switch_b: Switch::Off, switch_b: Switch::Off,
switch_c: 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_a, Switch::On);
assert_eq!(rust_config.switch_b, Switch::Off); assert_eq!(rust_config.switch_b, Switch::Off);

View File

@ -1,9 +1,8 @@
use crate::config::ModuleConfig; use crate::{config::ModuleConfig, module::ALL_MODULES};
use serde::Serialize; use serde::Serialize;
use starship_module_config_derive::ModuleConfig;
#[derive(Clone, ModuleConfig, Serialize)] #[derive(Clone, Serialize)]
pub struct StarshipRootConfig<'a> { pub struct StarshipRootConfig<'a> {
pub format: &'a str, pub format: &'a str,
pub scan_timeout: u64, 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<Self> {
let mut out = Self::default();
out.load_config(config);
Some(out)
}
}

View File

@ -1,6 +1,6 @@
[package] [package]
name = "starship_module_config_derive" name = "starship_module_config_derive"
version = "0.1.3" version = "0.2.0"
edition = "2018" edition = "2018"
authors = ["Matan Kushner <hello@matchai.me>"] authors = ["Matan Kushner <hello@matchai.me>"]
homepage = "https://starship.rs" homepage = "https://starship.rs"

View File

@ -23,9 +23,7 @@ fn impl_module_config(dinput: DeriveInput) -> proc_macro::TokenStream {
let ident = field.ident.as_ref().unwrap(); let ident = field.ident.as_ref().unwrap();
let new_load_tokens = quote! { let new_load_tokens = quote! {
if let Some(config_str) = config.get(stringify!(#ident)) { stringify!(#ident) => self.#ident.load_config(v),
new_module_config.#ident = new_module_config.#ident.load_config(config_str);
}
}; };
load_tokens = quote! { load_tokens = quote! {
@ -35,23 +33,24 @@ fn impl_module_config(dinput: DeriveInput) -> proc_macro::TokenStream {
} }
load_config = quote! { load_config = quote! {
fn load_config(&self, config: &'a toml::Value) -> Self { fn load_config(&mut self, config: &'a toml::Value) {
let mut new_module_config = self.clone();
if let toml::Value::Table(config) = config { if let toml::Value::Table(config) = config {
if config.get("prefix").is_some() { config.iter().for_each(|(k, v)| {
log::warn!("\"prefix\" has been removed in favor of \"format\". For more details, see: https://starship.rs/migrating-to-0.45.0/") match k.as_str() {
}
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 #load_tokens
unknown => {
::log::warn!("Unknown config key '{}'", unknown);
},
}
});
} }
new_module_config
} }
}; };
from_config = quote! { from_config = quote! {
fn from_config(config: &'a toml::Value) -> Option<Self> { fn from_config(config: &'a toml::Value) -> Option<Self> {
Some(Self::default().load_config(config)) let mut out = Self::default();
out.load_config(config);
Some(out)
} }
}; };
} }