From c720cc00e36547da1db7c8e15082dcfb734c7c56 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Tue, 24 Sep 2019 08:24:51 +1200 Subject: [PATCH] More 'did you mean?' errors --- src/errors.rs | 18 ------------------ src/evaluate/evaluator.rs | 20 +++++++++++++++----- 2 files changed, 15 insertions(+), 23 deletions(-) diff --git a/src/errors.rs b/src/errors.rs index db70e13548..7e9c14b239 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -12,20 +12,6 @@ pub enum Description { Synthetic(String), } -impl Description { - pub fn from(value: Tagged>) -> Description { - let value_tag = value.tag(); - - match value_tag { - Tag { - span: crate::data::meta::Span { start: 0, end: 0 }, - .. - } => Description::Synthetic(value.item.into()), - _ => Description::Source(Tagged::from_item(value.item.into(), value_tag)), - } - } -} - impl Description { fn into_label(self) -> Result, String> { match self { @@ -114,10 +100,6 @@ impl ShellError { .start() } - pub(crate) fn missing_property(subpath: Description, expr: Description) -> ShellError { - ProximateShellError::MissingProperty { subpath, expr }.start() - } - pub(crate) fn missing_value(tag: Option, reason: impl Into) -> ShellError { ProximateShellError::MissingValue { tag, diff --git a/src/evaluate/evaluator.rs b/src/evaluate/evaluator.rs index 8228c1035c..a111d3964d 100644 --- a/src/evaluate/evaluator.rs +++ b/src/evaluate/evaluator.rs @@ -1,5 +1,5 @@ use crate::data::base::Block; -use crate::errors::{ArgumentError, Description}; +use crate::errors::ArgumentError; use crate::parser::{ hir::{self, Expression, RawExpression}, CommandRegistry, Text, @@ -87,10 +87,20 @@ pub(crate) fn evaluate_baseline_expr( match next { None => { - return Err(ShellError::missing_property( - Description::from(item.tagged_type_name()), - Description::from(name.clone()), - )) + let possibilities = item.data_descriptors(); + + let mut possible_matches: Vec<_> = possibilities + .iter() + .map(|x| (natural::distance::levenshtein_distance(x, &name), x)) + .collect(); + + possible_matches.sort(); + + return Err(ShellError::labeled_error( + "Unknown column", + format!("did you mean '{}'?", possible_matches[0].1), + expr.tag(), + )); } Some(next) => { item = next.clone().item.tagged(expr.tag());