Skip to content

Commit

Permalink
refactor(util): Move VersionExt back into main cargo crate
Browse files Browse the repository at this point in the history
This is a partial revert of 2b2502f

These were pulled out with the idea of being a place to house
`PartialVersion` for `util_schemas` to use.
Since then, we went with a `util_schemas::core` in rust-lang#13128,
meaning we can hold off on creating yet another crate for us to manage.
  • Loading branch information
epage committed Dec 15, 2023
1 parent 594e2be commit 7cac565
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 31 deletions.
2 changes: 1 addition & 1 deletion 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_semver::VersionExt;
use cargo::util::VersionExt;
use cargo::CargoResult;
use itertools::Itertools;
use semver::VersionReq;
Expand Down
3 changes: 1 addition & 2 deletions src/cargo/core/resolver/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use std::task::Poll;
use crate::core::{Dependency, PackageId, Registry, Summary};
use crate::sources::source::QueryKind;
use crate::util::edit_distance::edit_distance;
use crate::util::{Config, OptVersionReq};
use crate::util_semver::VersionExt;
use crate::util::{Config, OptVersionReq, VersionExt};
use anyhow::Error;

use super::context::Context;
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::lockserver::{LockServer, LockServerClient, LockServerStarted};
pub use self::progress::{Progress, ProgressStyle};
pub use self::queue::Queue;
pub use self::rustc::Rustc;
pub use self::semver_ext::OptVersionReq;
pub use self::semver_ext::{OptVersionReq, 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
26 changes: 24 additions & 2 deletions src/cargo/util/semver_ext.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,30 @@
use std::fmt::{self, Display};

use semver::{Op, Version, VersionReq};
use semver::{Comparator, Op, Version, VersionReq};

use crate::util_semver::VersionExt as _;
pub trait VersionExt {
fn is_prerelease(&self) -> bool;

fn to_exact_req(&self) -> VersionReq;
}

impl VersionExt for Version {
fn is_prerelease(&self) -> bool {
!self.pre.is_empty()
}

fn to_exact_req(&self) -> VersionReq {
VersionReq {
comparators: vec![Comparator {
op: Op::Exact,
major: self.major,
minor: Some(self.minor),
patch: Some(self.patch),
pre: self.pre.clone(),
}],
}
}
}

#[derive(PartialEq, Eq, Hash, Clone, Debug)]
pub enum OptVersionReq {
Expand Down
26 changes: 1 addition & 25 deletions src/cargo/util_semver.rs
Original file line number Diff line number Diff line change
@@ -1,32 +1,8 @@
use std::fmt::{self, Display};

use semver::{Comparator, Op, Version, VersionReq};
use semver::{Comparator, Version, VersionReq};
use serde_untagged::UntaggedEnumVisitor;

pub trait VersionExt {
fn is_prerelease(&self) -> bool;

fn to_exact_req(&self) -> VersionReq;
}

impl VersionExt for Version {
fn is_prerelease(&self) -> bool {
!self.pre.is_empty()
}

fn to_exact_req(&self) -> VersionReq {
VersionReq {
comparators: vec![Comparator {
op: Op::Exact,
major: self.major,
minor: Some(self.minor),
patch: Some(self.patch),
pre: self.pre.clone(),
}],
}
}
}

#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Debug)]
pub struct PartialVersion {
pub major: u64,
Expand Down

0 comments on commit 7cac565

Please sign in to comment.