Skip to content

Commit 8cf7143

Browse files
committed
Auto merge of #12926 - epage:exact, r=Eh2406
refactor(util): Prepare for splitting out semver logic ### What does this PR try to resolve? Like #12924, this was cleanup I noticed as I was looking to pull out our reusable semver code for #12801 ### How should we test and review this PR? ### Additional information
2 parents b065008 + cf6d5b3 commit 8cf7143

File tree

7 files changed

+19
-23
lines changed

7 files changed

+19
-23
lines changed

src/bin/cargo/commands/install.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use anyhow::format_err;
66
use cargo::core::{GitReference, SourceId, Workspace};
77
use cargo::ops;
88
use cargo::util::IntoUrl;
9-
use cargo::util::VersionReqExt;
9+
use cargo::util::VersionExt;
1010
use cargo::CargoResult;
1111
use itertools::Itertools;
1212
use semver::VersionReq;
@@ -263,8 +263,8 @@ fn parse_semver_flag(v: &str) -> CargoResult<VersionReq> {
263263
),
264264
}
265265
} else {
266-
match v.trim().parse() {
267-
Ok(v) => Ok(VersionReq::exact(&v)),
266+
match v.trim().parse::<semver::Version>() {
267+
Ok(v) => Ok(v.to_exact_req()),
268268
Err(e) => {
269269
let mut msg = e.to_string();
270270

src/cargo/core/package_id_spec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ impl PackageIdSpec {
153153

154154
/// Full `semver::Version`, if present
155155
pub fn version(&self) -> Option<Version> {
156-
self.version.as_ref().and_then(|v| v.version())
156+
self.version.as_ref().and_then(|v| v.to_version())
157157
}
158158

159159
pub fn partial_version(&self) -> Option<&PartialVersion> {

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ pub fn create_bcx<'a, 'cfg>(
493493
continue;
494494
};
495495

496-
let req = version.caret_req();
496+
let req = version.to_caret_req();
497497
if req.matches(&untagged_version) {
498498
continue;
499499
}

src/cargo/ops/common_for_install_and_uninstall.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ where
555555
match deps.iter().max_by_key(|p| p.package_id()) {
556556
Some(summary) => {
557557
if let (Some(current), Some(msrv)) = (current_rust_version, summary.rust_version()) {
558-
let msrv_req = msrv.caret_req();
558+
let msrv_req = msrv.to_caret_req();
559559
if !msrv_req.matches(current) {
560560
let name = summary.name();
561561
let ver = summary.version();
@@ -574,7 +574,7 @@ where
574574
.filter(|summary| {
575575
summary
576576
.rust_version()
577-
.map(|msrv| msrv.caret_req().matches(current))
577+
.map(|msrv| msrv.to_caret_req().matches(current))
578578
.unwrap_or(true)
579579
})
580580
.max_by_key(|s| s.package_id())

src/cargo/util/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub use self::progress::{Progress, ProgressStyle};
2323
pub use self::queue::Queue;
2424
pub use self::restricted_names::validate_package_name;
2525
pub use self::rustc::Rustc;
26-
pub use self::semver_ext::{OptVersionReq, PartialVersion, RustVersion, VersionExt, VersionReqExt};
26+
pub use self::semver_ext::{OptVersionReq, PartialVersion, RustVersion, VersionExt};
2727
pub use self::vcs::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
2828
pub use self::workspace::{
2929
add_path_args, path_args, print_available_benches, print_available_binaries,

src/cargo/util/semver_ext.rs

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,38 @@ pub enum OptVersionReq {
1414

1515
pub trait VersionExt {
1616
fn is_prerelease(&self) -> bool;
17-
}
1817

19-
pub trait VersionReqExt {
20-
fn exact(version: &Version) -> Self;
18+
fn to_exact_req(&self) -> VersionReq;
2119
}
2220

2321
impl VersionExt for Version {
2422
fn is_prerelease(&self) -> bool {
2523
!self.pre.is_empty()
2624
}
27-
}
2825

29-
impl VersionReqExt for VersionReq {
30-
fn exact(version: &Version) -> Self {
26+
fn to_exact_req(&self) -> VersionReq {
3127
VersionReq {
3228
comparators: vec![Comparator {
3329
op: Op::Exact,
34-
major: version.major,
35-
minor: Some(version.minor),
36-
patch: Some(version.patch),
37-
pre: version.pre.clone(),
30+
major: self.major,
31+
minor: Some(self.minor),
32+
patch: Some(self.patch),
33+
pre: self.pre.clone(),
3834
}],
3935
}
4036
}
4137
}
4238

4339
impl OptVersionReq {
4440
pub fn exact(version: &Version) -> Self {
45-
OptVersionReq::Req(VersionReq::exact(version))
41+
OptVersionReq::Req(version.to_exact_req())
4642
}
4743

4844
// Since some registries have allowed crate versions to differ only by build metadata,
4945
// A query using OptVersionReq::exact return nondeterministic results.
5046
// So we `lock_to` the exact version were interested in.
5147
pub fn lock_to_exact(version: &Version) -> Self {
52-
OptVersionReq::Locked(version.clone(), VersionReq::exact(version))
48+
OptVersionReq::Locked(version.clone(), version.to_exact_req())
5349
}
5450

5551
pub fn is_exact(&self) -> bool {
@@ -205,7 +201,7 @@ pub struct PartialVersion {
205201
}
206202

207203
impl PartialVersion {
208-
pub fn version(&self) -> Option<Version> {
204+
pub fn to_version(&self) -> Option<Version> {
209205
Some(Version {
210206
major: self.major,
211207
minor: self.minor?,
@@ -215,7 +211,7 @@ impl PartialVersion {
215211
})
216212
}
217213

218-
pub fn caret_req(&self) -> VersionReq {
214+
pub fn to_caret_req(&self) -> VersionReq {
219215
VersionReq {
220216
comparators: vec![Comparator {
221217
op: semver::Op::Caret,

src/cargo/util/toml/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -568,7 +568,7 @@ impl schema::TomlManifest {
568568
let rust_version = rust_version
569569
.clone()
570570
.resolve("rust_version", || inherit()?.rust_version())?;
571-
let req = rust_version.caret_req();
571+
let req = rust_version.to_caret_req();
572572
if let Some(first_version) = edition.first_version() {
573573
let unsupported =
574574
semver::Version::new(first_version.major, first_version.minor - 1, 9999);

0 commit comments

Comments
 (0)