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>
This commit is contained in:
David Knaack
2021-04-06 22:12:37 +02:00
committed by GitHub
parent a2cdc912e7
commit 8af677c811
5 changed files with 63 additions and 4 deletions
+26
View File
@@ -1,6 +1,7 @@
use crate::{config::ModuleConfig, module::ALL_MODULES};
use serde::Serialize;
use std::cmp::Ordering;
// On changes please also update the `FullConfig` struct in `mod.rs`
#[derive(Clone, Serialize)]
@@ -100,6 +101,31 @@ impl<'a> ModuleConfig<'a> for StarshipRootConfig<'a> {
unknown => {
if !ALL_MODULES.contains(&unknown) && unknown != "custom" {
log::warn!("Unknown config key '{}'", unknown);
let did_you_mean = &[
// Root options
"format",
"scan_timeout",
"command_timeout",
"add_newline",
// Modules
"custom",
]
.iter()
.chain(ALL_MODULES.iter())
.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(Ordering::Equal)
},
);
if let Some((_score, field)) = did_you_mean {
log::warn!("Did you mean '{}'?", field);
}
}
}
});