diff options
author | David Knaack <davidkna@users.noreply.github.com> | 2021-04-06 22:12:37 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-06 22:12:37 +0200 |
commit | 8af677c8117beb52e0c419eff1ed5d0102c86ae3 (patch) | |
tree | 88793930f1e17f8e3f941ebeb79b0dbbd7932d66 /starship_module_config_derive | |
parent | a2cdc912e75852db6e45cd01f126a636937edcd9 (diff) |
feat(config): print a suggestion for unknown fields (#2560)
* feat(config): print a suggestion for unknown fields
* Fix typo
Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>
Co-authored-by: Thomas O'Donnell <andytom@users.noreply.github.com>
Diffstat (limited to 'starship_module_config_derive')
-rw-r--r-- | starship_module_config_derive/Cargo.toml | 2 | ||||
-rw-r--r-- | starship_module_config_derive/src/lib.rs | 25 |
2 files changed, 26 insertions, 1 deletions
diff --git a/starship_module_config_derive/Cargo.toml b/starship_module_config_derive/Cargo.toml index c798ef9bf..928eef5f6 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.2.0" +version = "0.2.1" edition = "2018" authors = ["Matan Kushner <hello@matchai.me>"] homepage = "https://starship.rs" diff --git a/starship_module_config_derive/src/lib.rs b/starship_module_config_derive/src/lib.rs index 254b0a3cc..40d4301b9 100644 --- a/starship_module_config_derive/src/lib.rs +++ b/starship_module_config_derive/src/lib.rs @@ -18,6 +18,7 @@ fn impl_module_config(dinput: DeriveInput) -> proc_macro::TokenStream { if let syn::Data::Struct(data) = dinput.data { if let syn::Fields::Named(fields_named) = data.fields { let mut load_tokens = quote! {}; + let mut fields = quote! {}; for field in fields_named.named.iter() { let ident = field.ident.as_ref().unwrap(); @@ -26,10 +27,19 @@ fn impl_module_config(dinput: DeriveInput) -> proc_macro::TokenStream { stringify!(#ident) => self.#ident.load_config(v), }; + let new_field = quote! { + stringify!(#ident), + }; + load_tokens = quote! { #load_tokens #new_load_tokens }; + + fields = quote! { + #fields + #new_field + }; } load_config = quote! { @@ -40,6 +50,21 @@ fn impl_module_config(dinput: DeriveInput) -> proc_macro::TokenStream { #load_tokens unknown => { ::log::warn!("Unknown config key '{}'", unknown); + + let did_you_mean = ::std::array::IntoIter::new([#fields]) + .filter_map(|field| { + let score = ::strsim::jaro_winkler(unknown, field); + (score > 0.8).then(|| (score, field)) + }) + .max_by( + |(score_a, _field_a), (score_b, _field_b)| { + score_a.partial_cmp(score_b).unwrap_or(::std::cmp::Ordering::Equal) + }, + ); + + if let Some((_score, field)) = did_you_mean { + ::log::warn!("Did you mean '{}'?", field); + } }, } }); |