Add basic 'did you mean' support

This commit is contained in:
Jonathan Turner
2019-09-13 15:44:21 +12:00
parent 4b8d0d62eb
commit 53cb40d8f6
3 changed files with 125 additions and 99 deletions

View File

@ -47,9 +47,20 @@ fn get_member(path: &Tagged<String>, obj: &Tagged<Value>) -> Result<Tagged<Value
match obj.get_data_by_key(&path.item) {
Some(v) => return Ok(v.clone()),
None => {
let possibilities = obj.data_descriptors();
let mut possible_matches: Vec<_> = possibilities
.iter()
.map(|x| {
(natural::distance::levenshtein_distance(x, &path.item), x)
})
.collect();
possible_matches.sort();
return Err(ShellError::labeled_error(
"Unknown column",
"table missing column",
format!("did you mean '{}'?", possible_matches[0].1),
path.span(),
));
}