Skip to content

Commit

Permalink
refactor(util): Unify VersionExt, VersionReqExt
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
epage committed Nov 8, 2023
1 parent 4270265 commit a1886c6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/bin/cargo/commands/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -263,8 +263,8 @@ fn parse_semver_flag(v: &str) -> CargoResult<VersionReq> {
),
}
} else {
match v.trim().parse() {
Ok(v) => Ok(VersionReq::exact(&v)),
match v.trim().parse::<semver::Version>() {
Ok(v) => Ok(v.to_exact_req()),
Err(e) => {
let mut msg = e.to_string();

Expand Down
2 changes: 1 addition & 1 deletion src/cargo/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 8 additions & 12 deletions src/cargo/util/semver_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,42 +14,38 @@ 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(),
}],
}
}
}

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 {
Expand Down

0 comments on commit a1886c6

Please sign in to comment.