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
This commit is contained in:
bors 2023-11-08 16:35:53 +00:00
commit 8cf7143664
7 changed files with 19 additions and 23 deletions

View File

@ -6,7 +6,7 @@ use anyhow::format_err;
use cargo::core::{GitReference, SourceId, Workspace}; use cargo::core::{GitReference, SourceId, Workspace};
use cargo::ops; use cargo::ops;
use cargo::util::IntoUrl; use cargo::util::IntoUrl;
use cargo::util::VersionReqExt; use cargo::util::VersionExt;
use cargo::CargoResult; use cargo::CargoResult;
use itertools::Itertools; use itertools::Itertools;
use semver::VersionReq; use semver::VersionReq;
@ -263,8 +263,8 @@ fn parse_semver_flag(v: &str) -> CargoResult<VersionReq> {
), ),
} }
} else { } else {
match v.trim().parse() { match v.trim().parse::<semver::Version>() {
Ok(v) => Ok(VersionReq::exact(&v)), Ok(v) => Ok(v.to_exact_req()),
Err(e) => { Err(e) => {
let mut msg = e.to_string(); let mut msg = e.to_string();

View File

@ -153,7 +153,7 @@ impl PackageIdSpec {
/// Full `semver::Version`, if present /// Full `semver::Version`, if present
pub fn version(&self) -> Option<Version> { pub fn version(&self) -> Option<Version> {
self.version.as_ref().and_then(|v| v.version()) self.version.as_ref().and_then(|v| v.to_version())
} }
pub fn partial_version(&self) -> Option<&PartialVersion> { pub fn partial_version(&self) -> Option<&PartialVersion> {

View File

@ -493,7 +493,7 @@ pub fn create_bcx<'a, 'cfg>(
continue; continue;
}; };
let req = version.caret_req(); let req = version.to_caret_req();
if req.matches(&untagged_version) { if req.matches(&untagged_version) {
continue; continue;
} }

View File

@ -555,7 +555,7 @@ where
match deps.iter().max_by_key(|p| p.package_id()) { match deps.iter().max_by_key(|p| p.package_id()) {
Some(summary) => { Some(summary) => {
if let (Some(current), Some(msrv)) = (current_rust_version, summary.rust_version()) { if let (Some(current), Some(msrv)) = (current_rust_version, summary.rust_version()) {
let msrv_req = msrv.caret_req(); let msrv_req = msrv.to_caret_req();
if !msrv_req.matches(current) { if !msrv_req.matches(current) {
let name = summary.name(); let name = summary.name();
let ver = summary.version(); let ver = summary.version();
@ -574,7 +574,7 @@ where
.filter(|summary| { .filter(|summary| {
summary summary
.rust_version() .rust_version()
.map(|msrv| msrv.caret_req().matches(current)) .map(|msrv| msrv.to_caret_req().matches(current))
.unwrap_or(true) .unwrap_or(true)
}) })
.max_by_key(|s| s.package_id()) .max_by_key(|s| s.package_id())

View File

@ -23,7 +23,7 @@ pub use self::progress::{Progress, ProgressStyle};
pub use self::queue::Queue; pub use self::queue::Queue;
pub use self::restricted_names::validate_package_name; pub use self::restricted_names::validate_package_name;
pub use self::rustc::Rustc; pub use self::rustc::Rustc;
pub use self::semver_ext::{OptVersionReq, PartialVersion, RustVersion, VersionExt, VersionReqExt}; pub use self::semver_ext::{OptVersionReq, PartialVersion, RustVersion, VersionExt};
pub use self::vcs::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo}; pub use self::vcs::{existing_vcs_repo, FossilRepo, GitRepo, HgRepo, PijulRepo};
pub use self::workspace::{ pub use self::workspace::{
add_path_args, path_args, print_available_benches, print_available_binaries, add_path_args, path_args, print_available_benches, print_available_binaries,

View File

@ -14,27 +14,23 @@ pub enum OptVersionReq {
pub trait VersionExt { pub trait VersionExt {
fn is_prerelease(&self) -> bool; fn is_prerelease(&self) -> bool;
}
pub trait VersionReqExt { fn to_exact_req(&self) -> VersionReq;
fn exact(version: &Version) -> Self;
} }
impl VersionExt for Version { impl VersionExt for Version {
fn is_prerelease(&self) -> bool { fn is_prerelease(&self) -> bool {
!self.pre.is_empty() !self.pre.is_empty()
} }
}
impl VersionReqExt for VersionReq { fn to_exact_req(&self) -> VersionReq {
fn exact(version: &Version) -> Self {
VersionReq { VersionReq {
comparators: vec![Comparator { comparators: vec![Comparator {
op: Op::Exact, op: Op::Exact,
major: version.major, major: self.major,
minor: Some(version.minor), minor: Some(self.minor),
patch: Some(version.patch), patch: Some(self.patch),
pre: version.pre.clone(), pre: self.pre.clone(),
}], }],
} }
} }
@ -42,14 +38,14 @@ impl VersionReqExt for VersionReq {
impl OptVersionReq { impl OptVersionReq {
pub fn exact(version: &Version) -> Self { pub fn exact(version: &Version) -> Self {
OptVersionReq::Req(VersionReq::exact(version)) OptVersionReq::Req(version.to_exact_req())
} }
// Since some registries have allowed crate versions to differ only by build metadata, // Since some registries have allowed crate versions to differ only by build metadata,
// A query using OptVersionReq::exact return nondeterministic results. // A query using OptVersionReq::exact return nondeterministic results.
// So we `lock_to` the exact version were interested in. // So we `lock_to` the exact version were interested in.
pub fn lock_to_exact(version: &Version) -> Self { pub fn lock_to_exact(version: &Version) -> Self {
OptVersionReq::Locked(version.clone(), VersionReq::exact(version)) OptVersionReq::Locked(version.clone(), version.to_exact_req())
} }
pub fn is_exact(&self) -> bool { pub fn is_exact(&self) -> bool {
@ -205,7 +201,7 @@ pub struct PartialVersion {
} }
impl PartialVersion { impl PartialVersion {
pub fn version(&self) -> Option<Version> { pub fn to_version(&self) -> Option<Version> {
Some(Version { Some(Version {
major: self.major, major: self.major,
minor: self.minor?, minor: self.minor?,
@ -215,7 +211,7 @@ impl PartialVersion {
}) })
} }
pub fn caret_req(&self) -> VersionReq { pub fn to_caret_req(&self) -> VersionReq {
VersionReq { VersionReq {
comparators: vec![Comparator { comparators: vec![Comparator {
op: semver::Op::Caret, op: semver::Op::Caret,

View File

@ -568,7 +568,7 @@ impl schema::TomlManifest {
let rust_version = rust_version let rust_version = rust_version
.clone() .clone()
.resolve("rust_version", || inherit()?.rust_version())?; .resolve("rust_version", || inherit()?.rust_version())?;
let req = rust_version.caret_req(); let req = rust_version.to_caret_req();
if let Some(first_version) = edition.first_version() { if let Some(first_version) = edition.first_version() {
let unsupported = let unsupported =
semver::Version::new(first_version.major, first_version.minor - 1, 9999); semver::Version::new(first_version.major, first_version.minor - 1, 9999);