From 4e22e350f11a19985b689c585059af5b8a953aef Mon Sep 17 00:00:00 2001 From: LuuuX Date: Wed, 21 Feb 2024 13:54:28 +0800 Subject: [PATCH] Added QueryKind::Normalized, and used it in cargo-add --- crates/resolver-tests/src/lib.rs | 1 + src/cargo/ops/cargo_add/mod.rs | 6 +++--- src/cargo/sources/directory.rs | 1 + src/cargo/sources/path.rs | 1 + src/cargo/sources/registry/mod.rs | 3 ++- src/cargo/sources/source.rs | 3 +++ 6 files changed, 11 insertions(+), 4 deletions(-) diff --git a/crates/resolver-tests/src/lib.rs b/crates/resolver-tests/src/lib.rs index efd9cebc6c68..c1cabbe555e8 100644 --- a/crates/resolver-tests/src/lib.rs +++ b/crates/resolver-tests/src/lib.rs @@ -113,6 +113,7 @@ pub fn resolve_with_config_raw( let matched = match kind { QueryKind::Exact => dep.matches(summary), QueryKind::Fuzzy => true, + QueryKind::Normalized => true, }; if matched { self.used.insert(summary.package_id()); diff --git a/src/cargo/ops/cargo_add/mod.rs b/src/cargo/ops/cargo_add/mod.rs index 609793efa0f9..9199b0e71436 100644 --- a/src/cargo/ops/cargo_add/mod.rs +++ b/src/cargo/ops/cargo_add/mod.rs @@ -588,7 +588,7 @@ fn get_latest_dependency( } MaybeWorkspace::Other(query) => { let possibilities = loop { - match registry.query_vec(&query, QueryKind::Fuzzy) { + match registry.query_vec(&query, QueryKind::Normalized) { std::task::Poll::Ready(res) => { break res?; } @@ -711,7 +711,7 @@ fn select_package( MaybeWorkspace::Other(query) => { let possibilities = loop { // Exact to avoid returning all for path/git - match registry.query_vec(&query, QueryKind::Exact) { + match registry.query_vec(&query, QueryKind::Normalized) { std::task::Poll::Ready(res) => { break res?; } @@ -938,7 +938,7 @@ fn populate_available_features( } let possibilities = loop { - match registry.query_vec(&query, QueryKind::Exact) { + match registry.query_vec(&query, QueryKind::Normalized) { std::task::Poll::Ready(res) => { break res?; } diff --git a/src/cargo/sources/directory.rs b/src/cargo/sources/directory.rs index 01c3c43302e9..12f1301003d3 100644 --- a/src/cargo/sources/directory.rs +++ b/src/cargo/sources/directory.rs @@ -109,6 +109,7 @@ impl<'cfg> Source for DirectorySource<'cfg> { let matches = packages.filter(|pkg| match kind { QueryKind::Exact => dep.matches(pkg.summary()), QueryKind::Fuzzy => true, + QueryKind::Normalized => dep.matches(pkg.summary()), }); for summary in matches.map(|pkg| pkg.summary().clone()) { f(IndexSummary::Candidate(summary)); diff --git a/src/cargo/sources/path.rs b/src/cargo/sources/path.rs index bbf6f056b16e..91f232cdc6f7 100644 --- a/src/cargo/sources/path.rs +++ b/src/cargo/sources/path.rs @@ -555,6 +555,7 @@ impl<'cfg> Source for PathSource<'cfg> { let matched = match kind { QueryKind::Exact => dep.matches(s), QueryKind::Fuzzy => true, + QueryKind::Normalized => dep.matches(s), }; if matched { f(IndexSummary::Candidate(s.clone())) diff --git a/src/cargo/sources/registry/mod.rs b/src/cargo/sources/registry/mod.rs index f2f2bb037fa6..6ce061ea5ff9 100644 --- a/src/cargo/sources/registry/mod.rs +++ b/src/cargo/sources/registry/mod.rs @@ -793,6 +793,7 @@ impl<'cfg> Source for RegistrySource<'cfg> { let matched = match kind { QueryKind::Exact => dep.matches(s.as_summary()), QueryKind::Fuzzy => true, + QueryKind::Normalized => true, }; if !matched { return; @@ -831,7 +832,7 @@ impl<'cfg> Source for RegistrySource<'cfg> { return Poll::Ready(Ok(())); } let mut any_pending = false; - if kind == QueryKind::Fuzzy { + if kind == QueryKind::Fuzzy || kind == QueryKind::Normalized { // Attempt to handle misspellings by searching for a chain of related // names to the original name. The resolver will later // reject any candidates that have the wrong name, and with this it'll diff --git a/src/cargo/sources/source.rs b/src/cargo/sources/source.rs index dd6619e59a75..016be1423e5b 100644 --- a/src/cargo/sources/source.rs +++ b/src/cargo/sources/source.rs @@ -180,6 +180,9 @@ pub enum QueryKind { /// whereas an `Registry` source may return dependencies that have the same /// canonicalization. Fuzzy, + /// Match a denpendency in all ways and will normalize the package name. + /// Each source defines what normalizing means. + Normalized, } /// A download status that represents if a [`Package`] has already been