Skip to content

Commit

Permalink
refactor: Move Id to Spec converter
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Dec 6, 2023
1 parent 1ecb3e8 commit 69c7463
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 20 deletions.
10 changes: 10 additions & 0 deletions src/cargo/core/package_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use std::sync::OnceLock;
use serde::de;
use serde::ser;

use crate::core::PackageIdSpec;
use crate::core::SourceId;
use crate::util::interning::InternedString;
use crate::util::CargoResult;
Expand Down Expand Up @@ -186,6 +187,15 @@ impl PackageId {
pub fn tarball_name(&self) -> String {
format!("{}-{}.crate", self.name(), self.version())
}

/// Convert a `PackageId` to a `PackageIdSpec`, which will have both the `PartialVersion` and `Url`
/// fields filled in.
pub fn to_spec(&self) -> PackageIdSpec {
PackageIdSpec::new(String::from(self.name().as_str()))
.with_version(self.version().clone().into())
.with_url(self.source_id().url().clone())
.with_kind(self.source_id().kind().clone())
}
}

pub struct PackageIdStableHash<'a>(PackageId, &'a Path);
Expand Down
11 changes: 1 addition & 10 deletions src/cargo/core/package_id_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,6 @@ impl PackageIdSpec {
})
}

/// Convert a `PackageId` to a `PackageIdSpec`, which will have both the `PartialVersion` and `Url`
/// fields filled in.
pub fn from_package_id(package_id: PackageId) -> PackageIdSpec {
PackageIdSpec::new(String::from(package_id.name().as_str()))
.with_version(package_id.version().clone().into())
.with_url(package_id.source_id().url().clone())
.with_kind(package_id.source_id().kind().clone())
}

/// Tries to convert a valid `Url` to a `PackageIdSpec`.
fn from_url(mut url: Url) -> CargoResult<PackageIdSpec> {
let mut kind = None;
Expand Down Expand Up @@ -417,7 +408,7 @@ impl PackageIdSpecQuery for PackageIdSpec {
if version_cnt[id.version()] == 1 {
msg.push_str(&format!("\n {}@{}", spec.name(), id.version()));
} else {
msg.push_str(&format!("\n {}", PackageIdSpec::from_package_id(*id)));
msg.push_str(&format!("\n {}", id.to_spec()));
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions src/cargo/ops/cargo_compile/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl Packages {
Packages::All => ws
.members()
.map(Package::package_id)
.map(PackageIdSpec::from_package_id)
.map(|id| id.to_spec())
.collect(),
Packages::OptOut(opt_out) => {
let (mut patterns, mut names) = opt_patterns_and_names(opt_out)?;
Expand All @@ -57,15 +57,15 @@ impl Packages {
!names.remove(pkg.name().as_str()) && !match_patterns(pkg, &mut patterns)
})
.map(Package::package_id)
.map(PackageIdSpec::from_package_id)
.map(|id| id.to_spec())
.collect();
let warn = |e| ws.config().shell().warn(e);
emit_package_not_found(ws, names, true).or_else(warn)?;
emit_pattern_not_found(ws, patterns, true).or_else(warn)?;
specs
}
Packages::Packages(packages) if packages.is_empty() => {
vec![PackageIdSpec::from_package_id(ws.current()?.package_id())]
vec![ws.current()?.package_id().to_spec()]
}
Packages::Packages(opt_in) => {
let (mut patterns, packages) = opt_patterns_and_names(opt_in)?;
Expand All @@ -78,7 +78,7 @@ impl Packages {
.members()
.filter(|pkg| match_patterns(pkg, &mut patterns))
.map(Package::package_id)
.map(PackageIdSpec::from_package_id);
.map(|id| id.to_spec());
specs.extend(matched_pkgs);
}
emit_pattern_not_found(ws, patterns, false)?;
Expand All @@ -87,7 +87,7 @@ impl Packages {
Packages::Default => ws
.default_members()
.map(Package::package_id)
.map(PackageIdSpec::from_package_id)
.map(|id| id.to_spec())
.collect(),
};
if specs.is_empty() {
Expand Down
6 changes: 2 additions & 4 deletions src/cargo/ops/cargo_install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ use std::sync::Arc;
use std::{env, fs};

use crate::core::compiler::{CompileKind, DefaultExecutor, Executor, UnitOutput};
use crate::core::{
Dependency, Edition, Package, PackageId, PackageIdSpec, SourceId, Target, Workspace,
};
use crate::core::{Dependency, Edition, Package, PackageId, SourceId, Target, Workspace};
use crate::ops::{common_for_install_and_uninstall::*, FilterRule};
use crate::ops::{CompileFilter, Packages};
use crate::sources::source::Source;
Expand Down Expand Up @@ -206,7 +204,7 @@ impl<'cfg> InstallablePackage<'cfg> {
// For cargo install tracking, we retain the source git url in `pkg`, but for the build spec
// we need to unconditionally use `ws.current()` to correctly address the path where we
// locally cloned that repo.
let pkgidspec = PackageIdSpec::from_package_id(ws.current()?.package_id());
let pkgidspec = ws.current()?.package_id().to_spec();
opts.spec = Packages::Packages(vec![pkgidspec.to_string()]);

if from_cwd {
Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/cargo_pkgid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ pub fn pkgid(ws: &Workspace<'_>, spec: Option<&str>) -> CargoResult<PackageIdSpe
Some(spec) => PackageIdSpec::query_str(spec, resolve.iter())?,
None => ws.current()?.package_id(),
};
Ok(PackageIdSpec::from_package_id(pkgid))
Ok(pkgid.to_spec())
}

0 comments on commit 69c7463

Please sign in to comment.