Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add matches_prerelease to allow update to prerelease version if it meets Semver compatible #321

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ jobs:
env:
RUSTFLAGS: --cfg test_node_semver ${{env.RUSTFLAGS}}

mirror_node_matches_prerelease:
name: mirror_node_matches_prerelease
needs: pre_ci
if: needs.pre_ci.outputs.continue
runs-on: ubuntu-latest
timeout-minutes: 45
steps:
- uses: actions/checkout@v4
- uses: dtolnay/rust-toolchain@stable
- run: cargo test --package semver --test test_matches_prerelease --features "mirror_node_matches_prerelease"

minimal:
name: Minimal versions
needs: pre_ci
Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ rust-version = "1.31"

[features]
default = ["std"]
mirror_node_matches_prerelease = []
std = []

[dependencies]
Expand Down
18 changes: 11 additions & 7 deletions src/eval.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
use crate::{Comparator, Op, Version, VersionReq};
use crate::{eval_ext, Comparator, Op, Version, VersionReq};

pub(crate) fn matches_req(req: &VersionReq, ver: &Version) -> bool {
pub(crate) fn matches_req(req: &VersionReq, ver: &Version, prerelease_matches: bool) -> bool {
for cmp in &req.comparators {
if !matches_impl(cmp, ver) {
if prerelease_matches {
if !eval_ext::matches_prerelease_impl(cmp, ver) {
return false;
}
} else if !matches_impl(cmp, ver) {
return false;
}
}

if ver.pre.is_empty() {
if ver.pre.is_empty() || prerelease_matches {
return true;
}

Expand Down Expand Up @@ -41,7 +45,7 @@ fn matches_impl(cmp: &Comparator, ver: &Version) -> bool {
}
}

fn matches_exact(cmp: &Comparator, ver: &Version) -> bool {
pub(super) fn matches_exact(cmp: &Comparator, ver: &Version) -> bool {
if ver.major != cmp.major {
return false;
}
Expand All @@ -61,7 +65,7 @@ fn matches_exact(cmp: &Comparator, ver: &Version) -> bool {
ver.pre == cmp.pre
}

fn matches_greater(cmp: &Comparator, ver: &Version) -> bool {
pub(super) fn matches_greater(cmp: &Comparator, ver: &Version) -> bool {
if ver.major != cmp.major {
return ver.major > cmp.major;
}
Expand All @@ -87,7 +91,7 @@ fn matches_greater(cmp: &Comparator, ver: &Version) -> bool {
ver.pre > cmp.pre
}

fn matches_less(cmp: &Comparator, ver: &Version) -> bool {
pub(super) fn matches_less(cmp: &Comparator, ver: &Version) -> bool {
if ver.major != cmp.major {
return ver.major < cmp.major;
}
Expand Down
269 changes: 269 additions & 0 deletions src/eval_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
use super::eval::{matches_exact, matches_greater, matches_less};
use crate::{Comparator, Op, Prerelease, Version};

pub(super) fn matches_prerelease_impl(cmp: &Comparator, ver: &Version) -> bool {
match cmp.op {
Op::Exact | Op::Wildcard => matches_exact_prerelease(cmp, ver),
Op::Greater => matches_greater(cmp, ver),
Op::GreaterEq => {
if matches_exact_prerelease(cmp, ver) {
return true;
}
matches_greater(cmp, ver)
}
Op::Less => matches_less(&fill_partial_req(cmp), ver),
Op::LessEq => {
if matches_exact_prerelease(cmp, ver) {
return true;
}
matches_less(&fill_partial_req(cmp), ver)
}
Op::Tilde => matches_tilde_prerelease(cmp, ver),
Op::Caret => matches_caret_prerelease(cmp, ver),
#[cfg(no_non_exhaustive)]
Op::__NonExhaustive => unreachable!(),
}
}

#[cfg(not(feature = "mirror_node_matches_prerelease"))]
fn fill_partial_req(cmp: &Comparator) -> Comparator {
let mut cmp = cmp.clone();
if cmp.minor.is_none() {
cmp.minor = Some(0);
cmp.patch = Some(0);
} else if cmp.patch.is_none() {
cmp.patch = Some(0);
}
cmp
}

#[cfg(not(feature = "mirror_node_matches_prerelease"))]
fn matches_exact_prerelease(cmp: &Comparator, ver: &Version) -> bool {
if matches_exact(cmp, ver) {
return true;
}

// If the comparator has a prerelease tag like =3.0.0-alpha.24,
// then it shoud be only exactly match 3.0.0-alpha.24.
if !cmp.pre.is_empty() {
return false;
}

if !matches_greater(&fill_partial_req(cmp), ver) {
return false;
}

let mut upper = Comparator {
op: Op::Less,
pre: Prerelease::new("0").unwrap(),
..cmp.clone()
};

match (upper.minor.is_some(), upper.patch.is_some()) {
(true, true) => {
upper.patch = Some(upper.patch.unwrap() + 1);
}
(true, false) => {
// Partial Exact VersionReq eg. =0.24
upper.minor = Some(upper.minor.unwrap() + 1);
upper.patch = Some(0);
}
(false, false) => {
// Partial Exact VersionReq eg. =0
upper.major += 1;
upper.minor = Some(0);
upper.patch = Some(0);
}
_ => {}
}

matches_less(&upper, ver)
}

fn matches_tilde_prerelease(cmp: &Comparator, ver: &Version) -> bool {
if matches_exact(cmp, ver) {
return true;
}

if !matches_greater(&fill_partial_req(cmp), ver) {
return false;
}

let mut upper = Comparator {
op: Op::Less,
pre: Prerelease::new("0").unwrap(),
..cmp.clone()
};

match (upper.minor.is_some(), upper.patch.is_some()) {
(true, _) => {
upper.minor = Some(upper.minor.unwrap() + 1);
upper.patch = Some(0);
}
(false, false) => {
upper.major += 1;
upper.minor = Some(0);
upper.patch = Some(0);
}
_ => {}
}

matches_less(&upper, ver)
}

#[cfg(not(feature = "mirror_node_matches_prerelease"))]
fn matches_caret_prerelease(cmp: &Comparator, ver: &Version) -> bool {
if matches_exact(cmp, ver) {
return true;
}

if !matches_greater(&fill_partial_req(cmp), ver) {
return false;
}

let mut upper = Comparator {
op: Op::Less,
pre: Prerelease::new("0").unwrap(),
..cmp.clone()
};

match (
upper.major > 0,
upper.minor.is_some(),
upper.patch.is_some(),
) {
(true, _, _) | (_, false, false) => {
upper.major += 1;
upper.minor = Some(0);
upper.patch = Some(0);
}
(_, true, false) => {
upper.minor = Some(upper.minor.unwrap() + 1);
upper.patch = Some(0);
}
(_, true, _) if upper.minor.unwrap() > 0 => {
upper.minor = Some(upper.minor.unwrap() + 1);
upper.patch = Some(0);
}
(_, true, _) if upper.minor.unwrap() == 0 => {
if upper.patch.is_none() {
upper.patch = Some(1);
} else {
upper.patch = Some(upper.patch.unwrap() + 1);
}
}
_ => {}
}

matches_less(&upper, ver)
}

#[cfg(feature = "mirror_node_matches_prerelease")]
fn fill_partial_req(cmp: &Comparator) -> Comparator {
let mut cmp = cmp.clone();
if cmp.minor.is_none() {
cmp.minor = Some(0);
cmp.patch = Some(0);
cmp.pre = Prerelease::new("0").unwrap();
} else if cmp.patch.is_none() {
cmp.patch = Some(0);
cmp.pre = Prerelease::new("0").unwrap();
}
cmp
}

#[cfg(feature = "mirror_node_matches_prerelease")]
fn matches_exact_prerelease(cmp: &Comparator, ver: &Version) -> bool {
let lower = fill_partial_req(cmp);
if matches_exact(&lower, ver) {
return true;
}

// If the comparator has a prerelease tag like =3.0.0-alpha.24,
// then it shoud be only exactly match 3.0.0-alpha.24.
if !cmp.pre.is_empty() {
return false;
}

if !matches_greater(&lower, ver) {
return false;
}

let mut upper = Comparator {
op: Op::Less,
pre: Prerelease::new("0").unwrap(),
..cmp.clone()
};

match (upper.minor.is_some(), upper.patch.is_some()) {
(true, true) => {
upper.patch = Some(upper.patch.unwrap() + 1);
}
(true, false) => {
// Partial Exact VersionReq eg. =0.24
upper.minor = Some(upper.minor.unwrap() + 1);
upper.patch = Some(0);
}
(false, false) => {
// Partial Exact VersionReq eg. =0
upper.major += 1;
upper.minor = Some(0);
upper.patch = Some(0);
}
_ => {}
}

matches_less(&upper, ver)
}

#[cfg(feature = "mirror_node_matches_prerelease")]
fn matches_caret_prerelease(cmp: &Comparator, ver: &Version) -> bool {
let mut lower = fill_partial_req(cmp);
if lower.major == 0 && lower.pre.is_empty() {
lower.pre = Prerelease::new("0").unwrap();
}

if matches_exact(&lower, ver) {
return true;
}

if !matches_greater(&lower, ver) {
return false;
}

let mut upper = Comparator {
op: Op::Less,
pre: Prerelease::new("0").unwrap(),
..cmp.clone()
};

match (
upper.major > 0,
upper.minor.is_some(),
upper.patch.is_some(),
) {
(true, _, _) | (_, false, false) => {
upper.major += 1;
upper.minor = Some(0);
upper.patch = Some(0);
}
(_, true, false) => {
upper.minor = Some(upper.minor.unwrap() + 1);
upper.patch = Some(0);
}
(_, true, _) if upper.minor.unwrap() > 0 => {
upper.minor = Some(upper.minor.unwrap() + 1);
upper.patch = Some(0);
}
(_, true, _) if upper.minor.unwrap() == 0 => {
if upper.patch.is_none() {
upper.patch = Some(1);
} else {
upper.patch = Some(upper.patch.unwrap() + 1);
}
}
_ => {}
}

matches_less(&upper, ver)
}
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ mod backport;
mod display;
mod error;
mod eval;
mod eval_ext;
mod identifier;
mod impls;
mod parse;
Expand Down Expand Up @@ -521,7 +522,12 @@ impl VersionReq {
/// Evaluate whether the given `Version` satisfies the version requirement
/// described by `self`.
pub fn matches(&self, version: &Version) -> bool {
eval::matches_req(self, version)
eval::matches_req(self, version, false)
}

/// Simliar to [`Self::matches`], it allows to match any "Semver-compatible" pre-release version.
pub fn matches_prerelease(&self, version: &Version) -> bool {
eval::matches_req(self, version, true)
}
}

Expand Down
Loading