refactor: move out into_resolve from EncodableResolve

This commit is contained in:
Vito Secona 2025-09-13 14:09:17 +07:00
parent 97333185f6
commit 598cfab7e9
2 changed files with 279 additions and 276 deletions

View File

@ -144,20 +144,23 @@ struct Patch {
pub type Metadata = BTreeMap<String, String>;
impl EncodableResolve {
/// Convert a `Cargo.lock` to a Resolve.
///
/// Note that this `Resolve` is not "complete". For example, the
/// dependencies do not know the difference between regular/dev/build
/// dependencies, so they are not filled in. It also does not include
/// `features`. Care should be taken when using this Resolve. One of the
/// primary uses is to be used with `resolve_with_previous` to guide the
/// resolver to create a complete Resolve.
pub fn into_resolve(self, original: &str, ws: &Workspace<'_>) -> CargoResult<Resolve> {
/// Convert a `Cargo.lock` to a Resolve.
///
/// Note that this `Resolve` is not "complete". For example, the
/// dependencies do not know the difference between regular/dev/build
/// dependencies, so they are not filled in. It also does not include
/// `features`. Care should be taken when using this Resolve. One of the
/// primary uses is to be used with `resolve_with_previous` to guide the
/// resolver to create a complete Resolve.
pub fn into_resolve(
resolve: EncodableResolve,
original: &str,
ws: &Workspace<'_>,
) -> CargoResult<Resolve> {
let path_deps: HashMap<String, HashMap<semver::Version, SourceId>> = build_path_deps(ws)?;
let mut checksums = HashMap::new();
let mut version = match self.version {
let mut version = match resolve.version {
Some(n @ 5) if ws.gctx().nightly_features_allowed => {
if ws.gctx().cli_unstable().next_lockfile_bump {
ResolveVersion::V5
@ -180,8 +183,8 @@ impl EncodableResolve {
};
let packages = {
let mut packages = self.package.unwrap_or_default();
if let Some(root) = self.root {
let mut packages = resolve.package.unwrap_or_default();
if let Some(root) = resolve.root {
packages.insert(0, root);
}
packages
@ -332,7 +335,7 @@ impl EncodableResolve {
replacements
};
let mut metadata = self.metadata.unwrap_or_default();
let mut metadata = resolve.metadata.unwrap_or_default();
// In the V1 serialization formats all checksums were listed in the lock
// file in the `[metadata]` section, so if we're still V1 then look for
@ -367,7 +370,7 @@ impl EncodableResolve {
}
let mut unused_patches = Vec::new();
for pkg in self.patch.unused {
for pkg in resolve.patch.unused {
let id = match pkg
.source
.as_deref()
@ -435,7 +438,6 @@ impl EncodableResolve {
None
})
}
}
}
fn build_path_deps(

View File

@ -1,5 +1,6 @@
use std::io::prelude::*;
use crate::core::resolver::encode::into_resolve;
use crate::core::{Resolve, ResolveVersion, Workspace, resolver};
use crate::util::Filesystem;
use crate::util::errors::CargoResult;
@ -23,7 +24,7 @@ pub fn load_pkg_lockfile(ws: &Workspace<'_>) -> CargoResult<Option<Resolve>> {
let resolve = (|| -> CargoResult<Option<Resolve>> {
let v: resolver::EncodableResolve = toml::from_str(&s)?;
Ok(Some(v.into_resolve(&s, ws)?))
Ok(Some(into_resolve(v, &s, ws)?))
})()
.with_context(|| format!("failed to parse lock file at: {}", f.path().display()))?;
Ok(resolve)
@ -208,7 +209,7 @@ fn are_equal_lockfiles(orig: &str, current: &str, ws: &Workspace<'_>) -> bool {
let res: CargoResult<bool> = (|| {
let old: resolver::EncodableResolve = toml::from_str(orig)?;
let new: resolver::EncodableResolve = toml::from_str(current)?;
Ok(old.into_resolve(orig, ws)? == new.into_resolve(current, ws)?)
Ok(into_resolve(old, orig, ws)? == into_resolve(new, current, ws)?)
})();
if let Ok(true) = res {
return true;