From a1886c620e1d71ca66a3a14a083369fe59c60312 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 6 Nov 2023 15:48:37 -0600 Subject: [PATCH 1/2] refactor(util): Unify VersionExt, VersionReqExt For myself, I find the trait-as-a-constructor approach of `VersionReqExt::exact` awkward and find merging it into `VersionExt` as `VersionExt::to_exact_req` is a bit cleaner. For example, this would make it easier to integrate with `PartialVersion` if we want. --- src/bin/cargo/commands/install.rs | 6 +++--- src/cargo/util/mod.rs | 2 +- src/cargo/util/semver_ext.rs | 20 ++++++++------------ 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/src/bin/cargo/commands/install.rs b/src/bin/cargo/commands/install.rs index 016ba5d5ce5..143ae517cff 100644 --- a/src/bin/cargo/commands/install.rs +++ b/src/bin/cargo/commands/install.rs @@ -6,7 +6,7 @@ use anyhow::format_err; use cargo::core::{GitReference, SourceId, Workspace}; use cargo::ops; use cargo::util::IntoUrl; -use cargo::util::VersionReqExt; +use cargo::util::VersionExt; use cargo::CargoResult; use itertools::Itertools; use semver::VersionReq; @@ -263,8 +263,8 @@ fn parse_semver_flag(v: &str) -> CargoResult { ), } } else { - match v.trim().parse() { - Ok(v) => Ok(VersionReq::exact(&v)), + match v.trim().parse::() { + Ok(v) => Ok(v.to_exact_req()), Err(e) => { let mut msg = e.to_string(); diff --git a/src/cargo/util/mod.rs b/src/cargo/util/mod.rs index 3a30bcb39c2..e72dde14e91 100644 --- a/src/cargo/util/mod.rs +++ b/src/cargo/util/mod.rs @@ -22,7 +22,7 @@ pub use self::progress::{Progress, ProgressStyle}; pub use self::queue::Queue; pub use self::restricted_names::validate_package_name; 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::workspace::{ add_path_args, path_args, print_available_benches, print_available_binaries, diff --git a/src/cargo/util/semver_ext.rs b/src/cargo/util/semver_ext.rs index 7863ba15d1f..86f0b19245e 100644 --- a/src/cargo/util/semver_ext.rs +++ b/src/cargo/util/semver_ext.rs @@ -14,27 +14,23 @@ pub enum OptVersionReq { pub trait VersionExt { fn is_prerelease(&self) -> bool; -} -pub trait VersionReqExt { - fn exact(version: &Version) -> Self; + fn to_exact_req(&self) -> VersionReq; } impl VersionExt for Version { fn is_prerelease(&self) -> bool { !self.pre.is_empty() } -} -impl VersionReqExt for VersionReq { - fn exact(version: &Version) -> Self { + fn to_exact_req(&self) -> VersionReq { VersionReq { comparators: vec![Comparator { op: Op::Exact, - major: version.major, - minor: Some(version.minor), - patch: Some(version.patch), - pre: version.pre.clone(), + major: self.major, + minor: Some(self.minor), + patch: Some(self.patch), + pre: self.pre.clone(), }], } } @@ -42,14 +38,14 @@ impl VersionReqExt for VersionReq { impl OptVersionReq { 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, // A query using OptVersionReq::exact return nondeterministic results. // So we `lock_to` the exact version were interested in. 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 { From cf6d5b32f72ff7cefd14a0fa83a10f1ff1f61abc Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 6 Nov 2023 15:52:43 -0600 Subject: [PATCH 2/2] refactor(util): Align VersionExt, PartialVersion naming --- src/cargo/core/package_id_spec.rs | 2 +- src/cargo/ops/cargo_compile/mod.rs | 2 +- src/cargo/ops/common_for_install_and_uninstall.rs | 4 ++-- src/cargo/util/semver_ext.rs | 4 ++-- src/cargo/util/toml/mod.rs | 2 +- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/cargo/core/package_id_spec.rs b/src/cargo/core/package_id_spec.rs index 51e026f51cf..457166b3710 100644 --- a/src/cargo/core/package_id_spec.rs +++ b/src/cargo/core/package_id_spec.rs @@ -153,7 +153,7 @@ impl PackageIdSpec { /// Full `semver::Version`, if present pub fn version(&self) -> Option { - 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> { diff --git a/src/cargo/ops/cargo_compile/mod.rs b/src/cargo/ops/cargo_compile/mod.rs index 9cf8599c48c..94c6cf9de04 100644 --- a/src/cargo/ops/cargo_compile/mod.rs +++ b/src/cargo/ops/cargo_compile/mod.rs @@ -493,7 +493,7 @@ pub fn create_bcx<'a, 'cfg>( continue; }; - let req = version.caret_req(); + let req = version.to_caret_req(); if req.matches(&untagged_version) { continue; } diff --git a/src/cargo/ops/common_for_install_and_uninstall.rs b/src/cargo/ops/common_for_install_and_uninstall.rs index 803f3488d38..d1f9152be40 100644 --- a/src/cargo/ops/common_for_install_and_uninstall.rs +++ b/src/cargo/ops/common_for_install_and_uninstall.rs @@ -555,7 +555,7 @@ where match deps.iter().max_by_key(|p| p.package_id()) { Some(summary) => { 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) { let name = summary.name(); let ver = summary.version(); @@ -574,7 +574,7 @@ where .filter(|summary| { summary .rust_version() - .map(|msrv| msrv.caret_req().matches(current)) + .map(|msrv| msrv.to_caret_req().matches(current)) .unwrap_or(true) }) .max_by_key(|s| s.package_id()) diff --git a/src/cargo/util/semver_ext.rs b/src/cargo/util/semver_ext.rs index 86f0b19245e..475abacc578 100644 --- a/src/cargo/util/semver_ext.rs +++ b/src/cargo/util/semver_ext.rs @@ -201,7 +201,7 @@ pub struct PartialVersion { } impl PartialVersion { - pub fn version(&self) -> Option { + pub fn to_version(&self) -> Option { Some(Version { major: self.major, minor: self.minor?, @@ -211,7 +211,7 @@ impl PartialVersion { }) } - pub fn caret_req(&self) -> VersionReq { + pub fn to_caret_req(&self) -> VersionReq { VersionReq { comparators: vec![Comparator { op: semver::Op::Caret, diff --git a/src/cargo/util/toml/mod.rs b/src/cargo/util/toml/mod.rs index 35aa3c50f17..3c1dc48f3ee 100644 --- a/src/cargo/util/toml/mod.rs +++ b/src/cargo/util/toml/mod.rs @@ -568,7 +568,7 @@ impl schema::TomlManifest { let rust_version = rust_version .clone() .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() { let unsupported = semver::Version::new(first_version.major, first_version.minor - 1, 9999);