Skip to content

Commit 9f0d819

Browse files
committed
Create helper function to suggest a candidate in case of a typo
1 parent cdc1d9d commit 9f0d819

File tree

1 file changed

+20
-11
lines changed

1 file changed

+20
-11
lines changed

clippy_config/src/conf.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -950,17 +950,10 @@ impl serde::de::Error for FieldError {
950950
}
951951
}
952952

953-
let suggestion = expected
954-
.iter()
955-
.filter_map(|expected| {
956-
let dist = edit_distance(field, expected, 4)?;
957-
Some((dist, expected))
958-
})
959-
.min_by_key(|&(dist, _)| dist)
960-
.map(|(_, suggestion)| Suggestion {
961-
message: "perhaps you meant",
962-
suggestion,
963-
});
953+
let suggestion = suggest_candidate(field, expected).map(|suggestion| Suggestion {
954+
message: "perhaps you meant",
955+
suggestion,
956+
});
964957

965958
Self { error: msg, suggestion }
966959
}
@@ -998,6 +991,22 @@ fn calculate_dimensions(fields: &[&str]) -> (usize, Vec<usize>) {
998991
(rows, column_widths)
999992
}
1000993

994+
/// Given a user-provided value that couldn't be matched to a known option, finds the most likely
995+
/// candidate among candidates that the user might have meant.
996+
fn suggest_candidate<'a, I>(value: &str, candidates: I) -> Option<&'a str>
997+
where
998+
I: IntoIterator<Item = &'a str>,
999+
{
1000+
candidates
1001+
.into_iter()
1002+
.filter_map(|expected| {
1003+
let dist = edit_distance(value, expected, 4)?;
1004+
Some((dist, expected))
1005+
})
1006+
.min_by_key(|&(dist, _)| dist)
1007+
.map(|(_, suggestion)| suggestion)
1008+
}
1009+
10011010
#[cfg(test)]
10021011
mod tests {
10031012
use serde::de::IgnoredAny;

0 commit comments

Comments
 (0)