Skip to content

Commit 33d1d78

Browse files
committed
refactor: Move Id to Spec converter
1 parent 294c385 commit 33d1d78

File tree

5 files changed

+19
-20
lines changed

5 files changed

+19
-20
lines changed

src/cargo/core/package_id.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ use std::sync::OnceLock;
1010
use serde::de;
1111
use serde::ser;
1212

13+
use crate::core::PackageIdSpec;
1314
use crate::core::SourceId;
1415
use crate::util::interning::InternedString;
1516
use crate::util::CargoResult;
@@ -186,6 +187,15 @@ impl PackageId {
186187
pub fn tarball_name(&self) -> String {
187188
format!("{}-{}.crate", self.name(), self.version())
188189
}
190+
191+
/// Convert a `PackageId` to a `PackageIdSpec`, which will have both the `PartialVersion` and `Url`
192+
/// fields filled in.
193+
pub fn to_spec(&self) -> PackageIdSpec {
194+
PackageIdSpec::new(String::from(self.name().as_str()))
195+
.with_version(self.version().clone().into())
196+
.with_url(self.source_id().url().clone())
197+
.with_kind(self.source_id().kind().clone())
198+
}
189199
}
190200

191201
pub struct PackageIdStableHash<'a>(PackageId, &'a Path);

src/cargo/core/package_id_spec.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,6 @@ impl PackageIdSpec {
109109
})
110110
}
111111

112-
/// Convert a `PackageId` to a `PackageIdSpec`, which will have both the `PartialVersion` and `Url`
113-
/// fields filled in.
114-
pub fn from_package_id(package_id: PackageId) -> PackageIdSpec {
115-
PackageIdSpec::new(String::from(package_id.name().as_str()))
116-
.with_version(package_id.version().clone().into())
117-
.with_url(package_id.source_id().url().clone())
118-
.with_kind(package_id.source_id().kind().clone())
119-
}
120-
121112
/// Tries to convert a valid `Url` to a `PackageIdSpec`.
122113
fn from_url(mut url: Url) -> CargoResult<PackageIdSpec> {
123114
let mut kind = None;
@@ -417,7 +408,7 @@ impl PackageIdSpecQuery for PackageIdSpec {
417408
if version_cnt[id.version()] == 1 {
418409
msg.push_str(&format!("\n {}@{}", spec.name(), id.version()));
419410
} else {
420-
msg.push_str(&format!("\n {}", PackageIdSpec::from_package_id(*id)));
411+
msg.push_str(&format!("\n {}", id.to_spec()));
421412
}
422413
}
423414
}

src/cargo/ops/cargo_compile/packages.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ impl Packages {
4747
Packages::All => ws
4848
.members()
4949
.map(Package::package_id)
50-
.map(PackageIdSpec::from_package_id)
50+
.map(|id| id.to_spec())
5151
.collect(),
5252
Packages::OptOut(opt_out) => {
5353
let (mut patterns, mut names) = opt_patterns_and_names(opt_out)?;
@@ -57,15 +57,15 @@ impl Packages {
5757
!names.remove(pkg.name().as_str()) && !match_patterns(pkg, &mut patterns)
5858
})
5959
.map(Package::package_id)
60-
.map(PackageIdSpec::from_package_id)
60+
.map(|id| id.to_spec())
6161
.collect();
6262
let warn = |e| ws.config().shell().warn(e);
6363
emit_package_not_found(ws, names, true).or_else(warn)?;
6464
emit_pattern_not_found(ws, patterns, true).or_else(warn)?;
6565
specs
6666
}
6767
Packages::Packages(packages) if packages.is_empty() => {
68-
vec![PackageIdSpec::from_package_id(ws.current()?.package_id())]
68+
vec![ws.current()?.package_id().to_spec()]
6969
}
7070
Packages::Packages(opt_in) => {
7171
let (mut patterns, packages) = opt_patterns_and_names(opt_in)?;
@@ -78,7 +78,7 @@ impl Packages {
7878
.members()
7979
.filter(|pkg| match_patterns(pkg, &mut patterns))
8080
.map(Package::package_id)
81-
.map(PackageIdSpec::from_package_id);
81+
.map(|id| id.to_spec());
8282
specs.extend(matched_pkgs);
8383
}
8484
emit_pattern_not_found(ws, patterns, false)?;
@@ -87,7 +87,7 @@ impl Packages {
8787
Packages::Default => ws
8888
.default_members()
8989
.map(Package::package_id)
90-
.map(PackageIdSpec::from_package_id)
90+
.map(|id| id.to_spec())
9191
.collect(),
9292
};
9393
if specs.is_empty() {

src/cargo/ops/cargo_install.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ use std::sync::Arc;
44
use std::{env, fs};
55

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

212210
if from_cwd {

src/cargo/ops/cargo_pkgid.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ pub fn pkgid(ws: &Workspace<'_>, spec: Option<&str>) -> CargoResult<PackageIdSpe
1111
Some(spec) => PackageIdSpec::query_str(spec, resolve.iter())?,
1212
None => ws.current()?.package_id(),
1313
};
14-
Ok(PackageIdSpec::from_package_id(pkgid))
14+
Ok(pkgid.to_spec())
1515
}

0 commit comments

Comments
 (0)