From f61cbae02234cedbc0b809f733200d934de3014a Mon Sep 17 00:00:00 2001 From: Waleed Khan Date: Sun, 4 Jun 2023 23:55:59 -0500 Subject: [PATCH] refactor(revset): move `collect_similar` from `cli_util` to `revset` --- Cargo.lock | 2 +- Cargo.toml | 1 - lib/Cargo.toml | 1 + lib/src/revset.rs | 13 ++++++++++++- src/cli_util.rs | 17 ++++------------- 5 files changed, 18 insertions(+), 16 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 60e66ba78..2615e833d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -865,7 +865,6 @@ dependencies = [ "rpassword", "serde", "slab", - "strsim", "tempfile", "testutils", "textwrap", @@ -905,6 +904,7 @@ dependencies = [ "rustix", "serde_json", "smallvec", + "strsim", "tempfile", "test-case", "testutils", diff --git a/Cargo.toml b/Cargo.toml index 16414c1f8..6b3f11955 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,6 @@ regex = "1.8.3" rpassword = "7.2.0" serde = { version = "1.0", features = ["derive"] } slab = "0.4.8" -strsim = "0.10.0" tempfile = "3.5.0" textwrap = "0.16.0" thiserror = "1.0.40" diff --git a/lib/Cargo.toml b/lib/Cargo.toml index a003fe350..14e10144f 100644 --- a/lib/Cargo.toml +++ b/lib/Cargo.toml @@ -41,6 +41,7 @@ rand_chacha = "0.3.1" regex = "1.8.3" serde_json = "1.0.96" smallvec = { version = "1.10.0", features = ["const_generics", "const_new", "union"] } +strsim = "0.10.0" tempfile = "3.5.0" thiserror = "1.0.40" tracing = "0.1.37" diff --git a/lib/src/revset.rs b/lib/src/revset.rs index ac826c536..c05695030 100644 --- a/lib/src/revset.rs +++ b/lib/src/revset.rs @@ -836,7 +836,7 @@ fn parse_function_expression( Err(RevsetParseError::with_span( RevsetParseErrorKind::NoSuchFunction { name: name.to_owned(), - candidates: collect_function_names(state.aliases_map), + candidates: collect_similar(name, &collect_function_names(state.aliases_map)), }, name_pair.as_span(), )) @@ -854,6 +854,17 @@ fn collect_function_names(aliases_map: &RevsetAliasesMap) -> Vec { names } +fn collect_similar(name: &str, candidates: &[impl AsRef]) -> Vec { + candidates + .iter() + .filter_map(|cand| { + // The parameter is borrowed from clap f5540d26 + (strsim::jaro(name, cand.as_ref()) > 0.7).then_some(cand) + }) + .map(|s| s.as_ref().to_owned()) + .collect_vec() +} + type RevsetFunction = fn(&str, Pair, ParseState) -> Result, RevsetParseError>; diff --git a/src/cli_util.rs b/src/cli_util.rs index 23e1d3d66..ccdfffbb4 100644 --- a/src/cli_util.rs +++ b/src/cli_util.rs @@ -101,16 +101,6 @@ pub fn user_error_with_hint(message: impl Into, hint: impl Into) } } -fn collect_similar<'a, S: AsRef>(name: &str, candidates: &'a [S]) -> Vec<&'a S> { - candidates - .iter() - .filter_map(|cand| { - // The parameter is borrowed from clap f5540d26 - (strsim::jaro(name, cand.as_ref()) > 0.7).then_some(cand) - }) - .collect_vec() -} - fn format_similarity_hint>(candidates: &[S]) -> Option { match candidates { [] => None, @@ -274,9 +264,10 @@ impl From for CommandError { similar_op, description, } => Some(format!("Did you mean '{similar_op}' for {description}?")), - RevsetParseErrorKind::NoSuchFunction { name, candidates } => { - format_similarity_hint(&collect_similar(name, candidates)) - } + RevsetParseErrorKind::NoSuchFunction { + name: _, + candidates, + } => format_similarity_hint(candidates), _ => None, }; CommandError::UserError {