Skip to content

Commit

Permalink
deprecated Equality and Range variants for single variant OrdRange ca…
Browse files Browse the repository at this point in the history
…pturing both

motive is that this is already implemented for build spec
this couples eq and ord since VersionOperator already couples those together
  • Loading branch information
YeungOnion committed Sep 6, 2023
1 parent 07f95f1 commit 8bb1cd8
Show file tree
Hide file tree
Showing 4 changed files with 221 additions and 185 deletions.
148 changes: 81 additions & 67 deletions crates/rattler_conda_types/src/version_spec/constraint.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,35 @@
use super::ParseConstraintError;
use super::RangeOperator;
use super::{EqualityOperator, RangeOperator, StrictRangeOperator};
use crate::constraint::operators::OrdOperator;
use crate::version_spec::parse::constraint_parser;
use crate::version_spec::{EqualityOperator, StrictRangeOperator};
use crate::Version;

use std::str::FromStr;

/// A single version constraint (e.g. `>3.4.5` or `1.2.*`)
#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
pub(crate) enum Constraint {
pub(crate) enum VersionConstraint {
/// Matches anything (`*`)
Any,

/// Version comparison (e.g `>1.2.3`)
OrdComparison(OrdOperator, Version),

#[deprecated(
since = "0.8.1",
note = "joined this with Exact variant into OrdComparison"
)]
/// Version comparison (e.g `>1.2.3`)
Comparison(RangeOperator, Version),

Check failure on line 24 in crates/rattler_conda_types/src/version_spec/constraint.rs

View workflow job for this annotation

GitHub Actions / Format, Lint and Test the Python bindings

variants `Comparison` and `Exact` are never constructed

Check failure on line 24 in crates/rattler_conda_types/src/version_spec/constraint.rs

View workflow job for this annotation

GitHub Actions / Format and Lint

variants `Comparison` and `Exact` are never constructed

Check failure on line 24 in crates/rattler_conda_types/src/version_spec/constraint.rs

View workflow job for this annotation

GitHub Actions / Check intra-doc links

variants `Comparison` and `Exact` are never constructed

Check failure on line 24 in crates/rattler_conda_types/src/version_spec/constraint.rs

View workflow job for this annotation

GitHub Actions / Format, Lint and Test the Python bindings

variants `Comparison` and `Exact` are never constructed

/// Strict comparison (e.g `~=1.2.3`)
StrictComparison(StrictRangeOperator, Version),

#[deprecated(
since = "0.8.1",
note = "joined this with Comparison variant into OrdComparison"
)]
/// Exact Version
Exact(EqualityOperator, Version),
}
Expand All @@ -27,7 +39,7 @@ pub(crate) fn is_start_of_version_constraint(c: char) -> bool {
matches!(c, '>' | '<' | '=' | '!' | '~')
}

impl FromStr for Constraint {
impl FromStr for VersionConstraint {
type Err = ParseConstraintError;

fn from_str(input: &str) -> Result<Self, Self::Err> {
Expand All @@ -42,119 +54,121 @@ impl FromStr for Constraint {

#[cfg(test)]
mod test {
use super::Constraint;
use super::VersionConstraint;
use crate::version_spec::constraint::ParseConstraintError;
use crate::version_spec::OrdOperator;
use crate::version_spec::{EqualityOperator, RangeOperator, StrictRangeOperator};
use crate::Version;

use std::str::FromStr;

#[test]
fn test_empty() {
assert!(matches!(
Constraint::from_str(""),
VersionConstraint::from_str(""),
Err(ParseConstraintError::InvalidVersion(_))
));
}

#[test]
fn test_any() {
assert_eq!(Constraint::from_str("*"), Ok(Constraint::Any));
assert_eq!(VersionConstraint::from_str("*"), Ok(VersionConstraint::Any));
}

#[test]
fn test_invalid_op() {
assert_eq!(
Constraint::from_str("<>1.2.3"),
VersionConstraint::from_str("<>1.2.3"),
Err(ParseConstraintError::InvalidOperator(String::from("<>")))
);
assert_eq!(
Constraint::from_str("=!1.2.3"),
VersionConstraint::from_str("=!1.2.3"),
Err(ParseConstraintError::InvalidOperator(String::from("=!")))
);
assert_eq!(
Constraint::from_str("<!=1.2.3"),
VersionConstraint::from_str("<!=1.2.3"),
Err(ParseConstraintError::InvalidOperator(String::from("<!=")))
);
assert_eq!(
Constraint::from_str("<!>1.2.3"),
VersionConstraint::from_str("<!>1.2.3"),
Err(ParseConstraintError::InvalidOperator(String::from("<!>")))
);
assert_eq!(
Constraint::from_str("!=!1.2.3"),
VersionConstraint::from_str("!=!1.2.3"),
Err(ParseConstraintError::InvalidOperator(String::from("!=!")))
);
assert_eq!(
Constraint::from_str("<=>1.2.3"),
VersionConstraint::from_str("<=>1.2.3"),
Err(ParseConstraintError::InvalidOperator(String::from("<=>")))
);
assert_eq!(
Constraint::from_str("=>1.2.3"),
VersionConstraint::from_str("=>1.2.3"),
Err(ParseConstraintError::InvalidOperator(String::from("=>")))
);
}

#[test]
fn test_op() {
assert_eq!(
Constraint::from_str(">1.2.3"),
Ok(Constraint::Comparison(
RangeOperator::Greater,
VersionConstraint::from_str(">1.2.3"),
Ok(VersionConstraint::OrdComparison(
OrdOperator::Gt,
Version::from_str("1.2.3").unwrap()
))
);
assert_eq!(
Constraint::from_str("<1.2.3"),
Ok(Constraint::Comparison(
RangeOperator::Less,
VersionConstraint::from_str("<1.2.3"),
Ok(VersionConstraint::OrdComparison(
OrdOperator::Lt,
Version::from_str("1.2.3").unwrap()
))
);
assert_eq!(
Constraint::from_str("=1.2.3"),
Ok(Constraint::StrictComparison(
VersionConstraint::from_str("=1.2.3"),
Ok(VersionConstraint::StrictComparison(
StrictRangeOperator::StartsWith,
Version::from_str("1.2.3").unwrap()
))
);
assert_eq!(
Constraint::from_str("==1.2.3"),
Ok(Constraint::Exact(
EqualityOperator::Equals,
VersionConstraint::from_str("==1.2.3"),
Ok(VersionConstraint::OrdComparison(
OrdOperator::Eq,
Version::from_str("1.2.3").unwrap()
))
);
assert_eq!(
Constraint::from_str("!=1.2.3"),
Ok(Constraint::Exact(
EqualityOperator::NotEquals,
VersionConstraint::from_str("!=1.2.3"),
Ok(VersionConstraint::OrdComparison(
OrdOperator::Ne,
Version::from_str("1.2.3").unwrap()
))
);
assert_eq!(
Constraint::from_str("~=1.2.3"),
Ok(Constraint::StrictComparison(
VersionConstraint::from_str("~=1.2.3"),
Ok(VersionConstraint::StrictComparison(
StrictRangeOperator::Compatible,
Version::from_str("1.2.3").unwrap()
))
);
assert_eq!(
Constraint::from_str(">=1.2.3"),
Ok(Constraint::Comparison(
RangeOperator::GreaterEquals,
VersionConstraint::from_str(">=1.2.3"),
Ok(VersionConstraint::OrdComparison(
OrdOperator::Ge,
Version::from_str("1.2.3").unwrap()
))
);
assert_eq!(
Constraint::from_str("<=1.2.3"),
Ok(Constraint::Comparison(
RangeOperator::LessEquals,
VersionConstraint::from_str("<=1.2.3"),
Ok(VersionConstraint::OrdComparison(
OrdOperator::Le,
Version::from_str("1.2.3").unwrap()
))
);
assert_eq!(
Constraint::from_str(">=1!1.2"),
Ok(Constraint::Comparison(
RangeOperator::GreaterEquals,
VersionConstraint::from_str(">=1!1.2"),
Ok(VersionConstraint::OrdComparison(
OrdOperator::Ge,
Version::from_str("1!1.2").unwrap()
))
);
Expand All @@ -163,51 +177,51 @@ mod test {
#[test]
fn test_glob_op() {
assert_eq!(
Constraint::from_str("=1.2.*"),
Ok(Constraint::StrictComparison(
VersionConstraint::from_str("=1.2.*"),
Ok(VersionConstraint::StrictComparison(
StrictRangeOperator::StartsWith,
Version::from_str("1.2").unwrap()
))
);
assert_eq!(
Constraint::from_str("!=1.2.*"),
Ok(Constraint::StrictComparison(
VersionConstraint::from_str("!=1.2.*"),
Ok(VersionConstraint::StrictComparison(
StrictRangeOperator::NotStartsWith,
Version::from_str("1.2").unwrap()
))
);
assert_eq!(
Constraint::from_str(">=1.2.*"),
Ok(Constraint::Comparison(
RangeOperator::GreaterEquals,
VersionConstraint::from_str(">=1.2.*"),
Ok(VersionConstraint::OrdComparison(
OrdOperator::Ge,
Version::from_str("1.2").unwrap()
))
);
assert_eq!(
Constraint::from_str("==1.2.*"),
Ok(Constraint::Exact(
EqualityOperator::Equals,
VersionConstraint::from_str("==1.2.*"),
Ok(VersionConstraint::OrdComparison(
OrdOperator::Eq,
Version::from_str("1.2").unwrap()
))
);
assert_eq!(
Constraint::from_str(">1.2.*"),
Ok(Constraint::Comparison(
RangeOperator::GreaterEquals,
VersionConstraint::from_str(">1.2.*"),
Ok(VersionConstraint::OrdComparison(
OrdOperator::Ge,
Version::from_str("1.2").unwrap()
))
);
assert_eq!(
Constraint::from_str("<=1.2.*"),
Ok(Constraint::Comparison(
RangeOperator::LessEquals,
VersionConstraint::from_str("<=1.2.*"),
Ok(VersionConstraint::OrdComparison(
OrdOperator::Le,
Version::from_str("1.2").unwrap()
))
);
assert_eq!(
Constraint::from_str("<1.2.*"),
Ok(Constraint::Comparison(
RangeOperator::Less,
VersionConstraint::from_str("<1.2.*"),
Ok(VersionConstraint::OrdComparison(
OrdOperator::Lt,
Version::from_str("1.2").unwrap()
))
);
Expand All @@ -216,24 +230,24 @@ mod test {
#[test]
fn test_starts_with() {
assert_eq!(
Constraint::from_str("1.2.*"),
Ok(Constraint::StrictComparison(
VersionConstraint::from_str("1.2.*"),
Ok(VersionConstraint::StrictComparison(
StrictRangeOperator::StartsWith,
Version::from_str("1.2").unwrap()
))
);
assert_eq!(
Constraint::from_str("1.2.*.*"),
VersionConstraint::from_str("1.2.*.*"),
Err(ParseConstraintError::RegexConstraintsNotSupported)
);
}

#[test]
fn test_exact() {
assert_eq!(
Constraint::from_str("1.2.3"),
Ok(Constraint::Exact(
EqualityOperator::Equals,
VersionConstraint::from_str("1.2.3"),
Ok(VersionConstraint::OrdComparison(
OrdOperator::Eq,
Version::from_str("1.2.3").unwrap()
))
);
Expand All @@ -242,15 +256,15 @@ mod test {
#[test]
fn test_regex() {
assert_eq!(
Constraint::from_str("^1.2.3"),
VersionConstraint::from_str("^1.2.3"),
Err(ParseConstraintError::UnterminatedRegex)
);
assert_eq!(
Constraint::from_str("1.2.3$"),
VersionConstraint::from_str("1.2.3$"),
Err(ParseConstraintError::RegexConstraintsNotSupported)
);
assert_eq!(
Constraint::from_str("1.*.3"),
VersionConstraint::from_str("1.*.3"),
Err(ParseConstraintError::RegexConstraintsNotSupported)
);
}
Expand Down
Loading

0 comments on commit 8bb1cd8

Please sign in to comment.