From 619238e86bd59a48c3176961b134dc82d1143749 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 7 Mar 2024 16:50:38 -0600 Subject: [PATCH] refactor(lockfile): Pull out dep change detection --- src/cargo/ops/cargo_generate_lockfile.rs | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/cargo/ops/cargo_generate_lockfile.rs b/src/cargo/ops/cargo_generate_lockfile.rs index ff1c85e75c9..394dde6c90f 100644 --- a/src/cargo/ops/cargo_generate_lockfile.rs +++ b/src/cargo/ops/cargo_generate_lockfile.rs @@ -202,10 +202,7 @@ fn print_lockfile_update( vec![] }; - if diff.removed.len() == 1 && diff.added.len() == 1 { - let added = diff.added.into_iter().next().unwrap(); - let removed = diff.removed.into_iter().next().unwrap(); - + if let Some((removed, added)) = diff.change() { let latest = if !possibilities.is_empty() { possibilities .iter() @@ -400,6 +397,19 @@ impl PackageDiff { changes.into_iter().map(|(_, v)| v).collect() } + /// Guess if a package upgraded/downgraded + /// + /// All `PackageDiff` knows is that entries were added/removed within [`Resolve`]. + /// A package could be added or removed because of dependencies from other packages + /// which makes it hard to definitively say "X was upgrade to N". + pub fn change(&self) -> Option<(&PackageId, &PackageId)> { + if self.removed.len() == 1 && self.added.len() == 1 { + Some((&self.removed[0], &self.added[0])) + } else { + None + } + } + /// For querying [`PackageRegistry`] for alternative versions to report to the user pub fn alternatives_query(&self) -> Option { let package_id = [