diff --git a/crates/rattler_conda_types/src/version_spec/constraint.rs b/crates/rattler_conda_types/src/version_spec/constraint.rs index a98137089..b22d220d6 100644 --- a/crates/rattler_conda_types/src/version_spec/constraint.rs +++ b/crates/rattler_conda_types/src/version_spec/constraint.rs @@ -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), /// 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), } @@ -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 { @@ -42,53 +54,55 @@ 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("=>"))) ); } @@ -96,65 +110,65 @@ mod test { #[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() )) ); @@ -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() )) ); @@ -216,14 +230,14 @@ 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) ); } @@ -231,9 +245,9 @@ mod test { #[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() )) ); @@ -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) ); } diff --git a/crates/rattler_conda_types/src/version_spec/mod.rs b/crates/rattler_conda_types/src/version_spec/mod.rs index 350cf1e23..dec95a27c 100644 --- a/crates/rattler_conda_types/src/version_spec/mod.rs +++ b/crates/rattler_conda_types/src/version_spec/mod.rs @@ -5,9 +5,10 @@ mod constraint; pub(crate) mod parse; pub(crate) mod version_tree; +use crate::constraint::operators::OrdOperator; use crate::version_spec::version_tree::ParseVersionTreeError; use crate::{ParseVersionError, Version}; -pub(crate) use constraint::Constraint; +pub(crate) use constraint::VersionConstraint; use serde::{Deserialize, Serialize, Serializer}; use std::convert::TryFrom; use std::fmt::{Display, Formatter}; @@ -82,13 +83,23 @@ impl EqualityOperator { /// Range and equality operators combined #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize)] -pub enum VersionOperators { +pub enum VersionOperator { + #[deprecated( + since = "0.8.1", + note = "OrdRange was created as transparent enum union for Range and Exact" + )] /// Specifies a range of versions Range(RangeOperator), /// Specifies a range of versions using the strict operator StrictRange(StrictRangeOperator), + #[deprecated( + since = "0.8.1", + note = "OrdRange was created as transparent enum union for Range and Exact" + )] /// Specifies an exact version Exact(EqualityOperator), + /// Uses methods from impl Ord to specify a version + OrdRange(OrdOperator), } /// Logical operator used two compare groups of version comparisions. E.g. `>=3.4,<4.0` or @@ -103,6 +114,8 @@ pub enum LogicalOperator { } impl LogicalOperator { + // what kind of complement is this? + // shouldn't this return (in a haskell-ish syntax) `logicalOp . map not`? /// Returns the complement of the operator. pub fn complement(self) -> Self { match self { @@ -121,13 +134,18 @@ pub enum VersionSpec { /// Any version Any, /// A version range - Range(RangeOperator, Version), + OrdRange(OrdOperator, Version), /// A version range using the strict operator StrictRange(StrictRangeOperator, StrictVersion), - /// A exact version - Exact(EqualityOperator, Version), /// A group of version specifications Group(LogicalOperator, Vec), + #[deprecated( + since = "0.8.1", + note = "VersionOperator::OrdRange as transparent enum union for Range + and Exact removes need for Exact using EqualityOperator" + )] + /// A specific version + Exact(EqualityOperator, Version), } #[allow(clippy::enum_variant_names, missing_docs)] @@ -153,15 +171,17 @@ impl FromStr for VersionSpec { fn parse_tree(tree: VersionTree) -> Result { match tree { VersionTree::Term(str) => { - let constraint = Constraint::from_str(str) + let constraint = VersionConstraint::from_str(str) .map_err(ParseVersionSpecError::InvalidConstraint)?; Ok(match constraint { - Constraint::Any => VersionSpec::Any, - Constraint::Comparison(op, ver) => VersionSpec::Range(op, ver), - Constraint::StrictComparison(op, ver) => { + VersionConstraint::Any => VersionSpec::Any, + VersionConstraint::OrdComparison(op, ver) => VersionSpec::OrdRange(op, ver), + VersionConstraint::StrictComparison(op, ver) => { VersionSpec::StrictRange(op, StrictVersion(ver)) } - Constraint::Exact(e, ver) => VersionSpec::Exact(e, ver), + VersionConstraint::Exact(_, _) | VersionConstraint::Comparison(_, _) => { + panic!("actively removing this variant") + } }) } VersionTree::Group(op, groups) => Ok(VersionSpec::Group( @@ -228,10 +248,8 @@ impl Display for VersionSpec { StrictRangeOperator::NotStartsWith => write!(f, "!={}.*", version), op => write!(f, "{}{}", op, version), }, - VersionSpec::Range(op, version) => { - write!(f, "{}{}", op, version) - } - VersionSpec::Exact(op, version) => { + VersionSpec::Exact(_, _) => panic!("Actively removing this variant"), + VersionSpec::OrdRange(op, version) => { write!(f, "{}{}", op, version) } VersionSpec::Group(op, group) => { @@ -273,12 +291,13 @@ impl VersionSpec { match self { VersionSpec::None => false, VersionSpec::Any => true, - VersionSpec::Exact(EqualityOperator::Equals, limit) => limit == version, - VersionSpec::Exact(EqualityOperator::NotEquals, limit) => limit != version, - VersionSpec::Range(RangeOperator::Greater, limit) => version > limit, - VersionSpec::Range(RangeOperator::GreaterEquals, limit) => version >= limit, - VersionSpec::Range(RangeOperator::Less, limit) => version < limit, - VersionSpec::Range(RangeOperator::LessEquals, limit) => version <= limit, + VersionSpec::OrdRange(OrdOperator::Eq, limit) => limit == version, + VersionSpec::OrdRange(OrdOperator::Ne, limit) => limit != version, + VersionSpec::OrdRange(OrdOperator::Gt, limit) => version > limit, + VersionSpec::OrdRange(OrdOperator::Ge, limit) => version >= limit, + VersionSpec::OrdRange(OrdOperator::Lt, limit) => version < limit, + VersionSpec::OrdRange(OrdOperator::Le, limit) => version <= limit, + VersionSpec::Exact(_, _) => panic!("actively removing this variant"), VersionSpec::StrictRange(StrictRangeOperator::StartsWith, limit) => { version.starts_with(&limit.0) } @@ -303,6 +322,7 @@ impl VersionSpec { #[cfg(test)] mod tests { + use crate::version_spec::OrdOperator; use crate::version_spec::{EqualityOperator, LogicalOperator, RangeOperator}; use crate::{Version, VersionSpec}; use std::str::FromStr; @@ -311,15 +331,15 @@ mod tests { fn test_simple() { assert_eq!( VersionSpec::from_str("1.2.3"), - Ok(VersionSpec::Exact( - EqualityOperator::Equals, + Ok(VersionSpec::OrdRange( + OrdOperator::Eq, Version::from_str("1.2.3").unwrap() )) ); assert_eq!( VersionSpec::from_str(">=1.2.3"), - Ok(VersionSpec::Range( - RangeOperator::GreaterEquals, + Ok(VersionSpec::OrdRange( + OrdOperator::Ge, Version::from_str("1.2.3").unwrap() )) ); @@ -332,11 +352,8 @@ mod tests { Ok(VersionSpec::Group( LogicalOperator::And, vec![ - VersionSpec::Range( - RangeOperator::GreaterEquals, - Version::from_str("1.2.3").unwrap() - ), - VersionSpec::Range(RangeOperator::Less, Version::from_str("2.0.0").unwrap()), + VersionSpec::OrdRange(OrdOperator::Ge, Version::from_str("1.2.3").unwrap()), + VersionSpec::OrdRange(OrdOperator::Lt, Version::from_str("2.0.0").unwrap()), ] )) ); @@ -345,11 +362,8 @@ mod tests { Ok(VersionSpec::Group( LogicalOperator::Or, vec![ - VersionSpec::Range( - RangeOperator::GreaterEquals, - Version::from_str("1.2.3").unwrap() - ), - VersionSpec::Range(RangeOperator::Less, Version::from_str("1.0.0").unwrap()), + VersionSpec::OrdRange(OrdOperator::Ge, Version::from_str("1.2.3").unwrap()), + VersionSpec::OrdRange(OrdOperator::Lt, Version::from_str("1.0.0").unwrap()), ] )) ); @@ -358,11 +372,8 @@ mod tests { Ok(VersionSpec::Group( LogicalOperator::Or, vec![ - VersionSpec::Range( - RangeOperator::GreaterEquals, - Version::from_str("1.2.3").unwrap() - ), - VersionSpec::Range(RangeOperator::Less, Version::from_str("1.0.0").unwrap()), + VersionSpec::OrdRange(OrdOperator::Ge, Version::from_str("1.2.3").unwrap()), + VersionSpec::OrdRange(OrdOperator::Lt, Version::from_str("1.0.0").unwrap()), ] )) ); diff --git a/crates/rattler_conda_types/src/version_spec/parse.rs b/crates/rattler_conda_types/src/version_spec/parse.rs index f3eef2f20..7a01578af 100644 --- a/crates/rattler_conda_types/src/version_spec/parse.rs +++ b/crates/rattler_conda_types/src/version_spec/parse.rs @@ -1,7 +1,8 @@ -use crate::version::parse::version_parser; -use crate::version_spec::constraint::{is_start_of_version_constraint, Constraint}; -use crate::version_spec::{EqualityOperator, RangeOperator, StrictRangeOperator, VersionOperators}; -use crate::{ParseVersionError, ParseVersionErrorKind}; +use super::constraint::{is_start_of_version_constraint, VersionConstraint}; +use super::{RangeOperator, StrictRangeOperator, VersionOperator}; +use crate::constraint::operators::OrdOperator; +use crate::version::parse::{version_parser, ParseVersionError, ParseVersionErrorKind}; + use nom::{ branch::alt, bytes::complete::{tag, take_while, take_while1}, @@ -22,7 +23,7 @@ enum ParseVersionOperatorError<'i> { } /// Parses a version operator, returns an error if the operator is not recognized or not found. -fn operator_parser(input: &str) -> IResult<&str, VersionOperators, ParseVersionOperatorError> { +fn operator_parser(input: &str) -> IResult<&str, VersionOperator, ParseVersionOperatorError> { // Take anything that looks like an operator. let (rest, operator_str) = take_while1(is_start_of_version_constraint)(input).map_err( |_: nom::Err>| { @@ -31,14 +32,14 @@ fn operator_parser(input: &str) -> IResult<&str, VersionOperators, ParseVersionO )?; let op = match operator_str { - "==" => VersionOperators::Exact(EqualityOperator::Equals), - "!=" => VersionOperators::Exact(EqualityOperator::NotEquals), - "<=" => VersionOperators::Range(RangeOperator::LessEquals), - ">=" => VersionOperators::Range(RangeOperator::GreaterEquals), - "<" => VersionOperators::Range(RangeOperator::Less), - ">" => VersionOperators::Range(RangeOperator::Greater), - "=" => VersionOperators::StrictRange(StrictRangeOperator::StartsWith), - "~=" => VersionOperators::StrictRange(StrictRangeOperator::Compatible), + "==" => VersionOperator::OrdRange(OrdOperator::Eq), + "!=" => VersionOperator::OrdRange(OrdOperator::Ne), + "<=" => VersionOperator::OrdRange(OrdOperator::Le), + ">=" => VersionOperator::OrdRange(OrdOperator::Ge), + "<" => VersionOperator::OrdRange(OrdOperator::Lt), + ">" => VersionOperator::OrdRange(OrdOperator::Gt), + "=" => VersionOperator::StrictRange(StrictRangeOperator::StartsWith), + "~=" => VersionOperator::StrictRange(StrictRangeOperator::Compatible), _ => { return Err(nom::Err::Failure( ParseVersionOperatorError::InvalidOperator(operator_str), @@ -83,7 +84,7 @@ impl<'i> ParseError<&'i str> for ParseConstraintError { } /// Parses a regex constraint. Returns an error if no terminating `$` is found. -fn regex_constraint_parser(input: &str) -> IResult<&str, Constraint, ParseConstraintError> { +fn regex_constraint_parser(input: &str) -> IResult<&str, VersionConstraint, ParseConstraintError> { let (_rest, (_, _, terminator)) = tuple((char('^'), take_while(|c| c != '$'), opt(char('$'))))(input)?; match terminator { @@ -95,12 +96,14 @@ fn regex_constraint_parser(input: &str) -> IResult<&str, Constraint, ParseConstr } /// Parses the any constraint. This matches "*" and ".*" -fn any_constraint_parser(input: &str) -> IResult<&str, Constraint, ParseConstraintError> { - value(Constraint::Any, terminated(tag("*"), opt(tag(".*"))))(input) +fn any_constraint_parser(input: &str) -> IResult<&str, VersionConstraint, ParseConstraintError> { + value(VersionConstraint::Any, terminated(tag("*"), opt(tag(".*"))))(input) } /// Parses a constraint with an operator in front of it. -fn logical_constraint_parser(input: &str) -> IResult<&str, Constraint, ParseConstraintError> { +fn logical_constraint_parser( + input: &str, +) -> IResult<&str, VersionConstraint, ParseConstraintError> { // Parse the optional preceding operator let (input, op) = match operator_parser(input) { Err( @@ -142,26 +145,26 @@ fn logical_constraint_parser(input: &str) -> IResult<&str, Constraint, ParseCons let op = match (version_rest, op) { // The version was successfully parsed ("", Some(op)) => op, - ("", None) => VersionOperators::Exact(EqualityOperator::Equals), + ("", None) => VersionOperator::OrdRange(OrdOperator::Eq), // The version ends in a wildcard pattern - ("*" | ".*", Some(VersionOperators::StrictRange(StrictRangeOperator::StartsWith))) => { - VersionOperators::StrictRange(StrictRangeOperator::StartsWith) + ("*" | ".*", Some(VersionOperator::StrictRange(StrictRangeOperator::StartsWith))) => { + VersionOperator::StrictRange(StrictRangeOperator::StartsWith) } - ("*" | ".*", Some(VersionOperators::Range(RangeOperator::GreaterEquals))) => { - VersionOperators::Range(RangeOperator::GreaterEquals) + ("*" | ".*", Some(VersionOperator::OrdRange(OrdOperator::Ge))) => { + VersionOperator::OrdRange(OrdOperator::Ge) } - ("*" | ".*", Some(VersionOperators::Range(RangeOperator::Greater))) => { - VersionOperators::Range(RangeOperator::GreaterEquals) + ("*" | ".*", Some(VersionOperator::OrdRange(OrdOperator::Gt))) => { + VersionOperator::OrdRange(OrdOperator::Ge) } - ("*" | ".*", Some(VersionOperators::Exact(EqualityOperator::NotEquals))) => { - VersionOperators::StrictRange(StrictRangeOperator::NotStartsWith) + ("*" | ".*", Some(VersionOperator::OrdRange(OrdOperator::Ne))) => { + VersionOperator::StrictRange(StrictRangeOperator::NotStartsWith) } (glob @ "*" | glob @ ".*", Some(op)) => { tracing::warn!("Using {glob} with relational operator is superfluous and deprecated and will be removed in a future version of conda."); op } - ("*" | ".*", None) => VersionOperators::StrictRange(StrictRangeOperator::StartsWith), + ("*" | ".*", None) => VersionOperator::StrictRange(StrictRangeOperator::StartsWith), // The version string kinda looks like a regular expression. (version_remainder, _) if version_str.contains('*') || version_remainder.ends_with('$') => { @@ -182,14 +185,18 @@ fn logical_constraint_parser(input: &str) -> IResult<&str, Constraint, ParseCons }; match op { - VersionOperators::Range(r) => Ok((rest, Constraint::Comparison(r, version))), - VersionOperators::Exact(e) => Ok((rest, Constraint::Exact(e, version))), - VersionOperators::StrictRange(s) => Ok((rest, Constraint::StrictComparison(s, version))), + VersionOperator::OrdRange(r) => Ok((rest, VersionConstraint::OrdComparison(r, version))), + VersionOperator::StrictRange(s) => { + Ok((rest, VersionConstraint::StrictComparison(s, version))) + } + _ => panic!("actively removing Range and Exact variants"), } } /// Parses a version constraint. -pub(crate) fn constraint_parser(input: &str) -> IResult<&str, Constraint, ParseConstraintError> { +pub(crate) fn constraint_parser( + input: &str, +) -> IResult<&str, VersionConstraint, ParseConstraintError> { alt(( regex_constraint_parser, any_constraint_parser, @@ -200,6 +207,7 @@ pub(crate) fn constraint_parser(input: &str) -> IResult<&str, Constraint, ParseC #[cfg(test)] mod test { use super::*; + use crate::constraint::operators::OrdOperator; use crate::{Version, VersionSpec}; use std::str::FromStr; @@ -207,40 +215,40 @@ mod test { fn test_operator_parser() { assert_eq!( operator_parser(">3.1"), - Ok(("3.1", VersionOperators::Range(RangeOperator::Greater))) + Ok(("3.1", VersionOperator::OrdRange(OrdOperator::Gt))) ); assert_eq!( operator_parser(">=3.1"), - Ok(("3.1", VersionOperators::Range(RangeOperator::GreaterEquals))) + Ok(("3.1", VersionOperator::OrdRange(OrdOperator::Ge))) ); assert_eq!( operator_parser("<3.1"), - Ok(("3.1", VersionOperators::Range(RangeOperator::Less))) + Ok(("3.1", VersionOperator::OrdRange(OrdOperator::Lt))) ); assert_eq!( operator_parser("<=3.1"), - Ok(("3.1", VersionOperators::Range(RangeOperator::LessEquals))) + Ok(("3.1", VersionOperator::OrdRange(OrdOperator::Le))) ); assert_eq!( operator_parser("==3.1"), - Ok(("3.1", VersionOperators::Exact(EqualityOperator::Equals))) + Ok(("3.1", VersionOperator::OrdRange(OrdOperator::Eq))) ); assert_eq!( operator_parser("!=3.1"), - Ok(("3.1", VersionOperators::Exact(EqualityOperator::NotEquals))) + Ok(("3.1", VersionOperator::OrdRange(OrdOperator::Ne))) ); assert_eq!( operator_parser("=3.1"), Ok(( "3.1", - VersionOperators::StrictRange(StrictRangeOperator::StartsWith) + VersionOperator::StrictRange(StrictRangeOperator::StartsWith) )) ); assert_eq!( operator_parser("~=3.1"), Ok(( "3.1", - VersionOperators::StrictRange(StrictRangeOperator::Compatible) + VersionOperator::StrictRange(StrictRangeOperator::Compatible) )) ); @@ -286,7 +294,10 @@ mod test { logical_constraint_parser("3.1"), Ok(( "", - Constraint::Exact(EqualityOperator::Equals, Version::from_str("3.1").unwrap()) + VersionConstraint::OrdComparison( + OrdOperator::Eq, + Version::from_str("3.1").unwrap() + ) )) ); @@ -294,7 +305,10 @@ mod test { logical_constraint_parser(">3.1"), Ok(( "", - Constraint::Comparison(RangeOperator::Greater, Version::from_str("3.1").unwrap()) + VersionConstraint::OrdComparison( + OrdOperator::Gt, + Version::from_str("3.1").unwrap() + ) )) ); @@ -302,7 +316,7 @@ mod test { logical_constraint_parser("3.1*"), Ok(( "", - Constraint::StrictComparison( + VersionConstraint::StrictComparison( StrictRangeOperator::StartsWith, Version::from_str("3.1").unwrap() ) @@ -313,7 +327,7 @@ mod test { logical_constraint_parser("3.1.*"), Ok(( "", - Constraint::StrictComparison( + VersionConstraint::StrictComparison( StrictRangeOperator::StartsWith, Version::from_str("3.1").unwrap() ) @@ -324,7 +338,7 @@ mod test { logical_constraint_parser("~=3.1"), Ok(( "", - Constraint::StrictComparison( + VersionConstraint::StrictComparison( StrictRangeOperator::Compatible, Version::from_str("3.1").unwrap() ) @@ -335,8 +349,8 @@ mod test { logical_constraint_parser(">=3.1*"), Ok(( "", - Constraint::Comparison( - RangeOperator::GreaterEquals, + VersionConstraint::OrdComparison( + OrdOperator::Ge, Version::from_str("3.1").unwrap() ) )) @@ -358,12 +372,13 @@ mod test { ); // Any constraints - assert_eq!(constraint_parser("*"), Ok(("", Constraint::Any))); - assert_eq!(constraint_parser("*.*"), Ok(("", Constraint::Any))); + assert_eq!(constraint_parser("*"), Ok(("", VersionConstraint::Any))); + assert_eq!(constraint_parser("*.*"), Ok(("", VersionConstraint::Any))); } #[test] fn pixi_issue_278() { + // TODO, move something so that from_str is in same submodule as this test assert!(VersionSpec::from_str("1.8.1+g6b29558").is_ok()); } } diff --git a/crates/rattler_conda_types/src/version_spec/version_tree.rs b/crates/rattler_conda_types/src/version_spec/version_tree.rs index c7bd933ba..871527fb1 100644 --- a/crates/rattler_conda_types/src/version_spec/version_tree.rs +++ b/crates/rattler_conda_types/src/version_spec/version_tree.rs @@ -1,6 +1,6 @@ -use crate::version_spec::{ - EqualityOperator, LogicalOperator, RangeOperator, StrictRangeOperator, VersionOperators, -}; +use super::OrdOperator; +use crate::version_spec::{LogicalOperator, StrictRangeOperator, VersionOperator}; + use nom::{ branch::alt, bytes::complete::{tag, take_while}, @@ -29,29 +29,20 @@ pub enum ParseVersionTreeError { /// A parser that parses version operators. fn parse_operator<'a, E: ParseError<&'a str>>( input: &'a str, -) -> Result<(&'a str, VersionOperators), nom::Err> { +) -> Result<(&'a str, VersionOperator), nom::Err> { alt(( - value(VersionOperators::Exact(EqualityOperator::Equals), tag("==")), - value( - VersionOperators::Exact(EqualityOperator::NotEquals), - tag("!="), - ), + value(VersionOperator::OrdRange(OrdOperator::Eq), tag("==")), + value(VersionOperator::OrdRange(OrdOperator::Ne), tag("!=")), value( - VersionOperators::StrictRange(StrictRangeOperator::StartsWith), + VersionOperator::StrictRange(StrictRangeOperator::StartsWith), tag("="), ), + value(VersionOperator::OrdRange(OrdOperator::Ge), tag(">=")), + value(VersionOperator::OrdRange(OrdOperator::Gt), tag(">")), + value(VersionOperator::OrdRange(OrdOperator::Le), tag("<=")), + value(VersionOperator::OrdRange(OrdOperator::Lt), tag("<")), value( - VersionOperators::Range(RangeOperator::GreaterEquals), - tag(">="), - ), - value(VersionOperators::Range(RangeOperator::Greater), tag(">")), - value( - VersionOperators::Range(RangeOperator::LessEquals), - tag("<="), - ), - value(VersionOperators::Range(RangeOperator::Less), tag("<")), - value( - VersionOperators::StrictRange(StrictRangeOperator::Compatible), + VersionOperator::StrictRange(StrictRangeOperator::Compatible), tag("~="), ), ))(input) @@ -201,11 +192,13 @@ impl<'a> TryFrom<&'a str> for VersionTree<'a> { #[cfg(test)] mod tests { + use super::OrdOperator; use super::{parse_operator, recognize_version, LogicalOperator, VersionTree}; use crate::version_spec::version_tree::{parse_version_epoch, recognize_constraint}; use crate::version_spec::{ - EqualityOperator, RangeOperator, StrictRangeOperator, VersionOperators, + EqualityOperator, RangeOperator, StrictRangeOperator, VersionOperator, }; + use std::convert::TryFrom; #[test] @@ -264,40 +257,40 @@ mod tests { assert_eq!( parse_operator::("=="), - Ok(("", VersionOperators::Exact(EqualityOperator::Equals))) + Ok(("", VersionOperator::OrdRange(OrdOperator::Eq))) ); assert_eq!( parse_operator::("!="), - Ok(("", VersionOperators::Exact(EqualityOperator::NotEquals))) + Ok(("", VersionOperator::OrdRange(OrdOperator::Ne))) ); assert_eq!( parse_operator::(">"), - Ok(("", VersionOperators::Range(RangeOperator::Greater))) + Ok(("", VersionOperator::OrdRange(OrdOperator::Gt))) ); assert_eq!( parse_operator::(">="), - Ok(("", VersionOperators::Range(RangeOperator::GreaterEquals))) + Ok(("", VersionOperator::OrdRange(OrdOperator::Ge))) ); assert_eq!( parse_operator::("<"), - Ok(("", VersionOperators::Range(RangeOperator::Less))) + Ok(("", VersionOperator::OrdRange(OrdOperator::Lt))) ); assert_eq!( parse_operator::("<="), - Ok(("", VersionOperators::Range(RangeOperator::LessEquals))) + Ok(("", VersionOperator::OrdRange(OrdOperator::Le))) ); assert_eq!( parse_operator::("="), Ok(( "", - VersionOperators::StrictRange(StrictRangeOperator::StartsWith) + VersionOperator::StrictRange(StrictRangeOperator::StartsWith) )) ); assert_eq!( parse_operator::("~="), Ok(( "", - VersionOperators::StrictRange(StrictRangeOperator::Compatible) + VersionOperator::StrictRange(StrictRangeOperator::Compatible) )) ); @@ -308,7 +301,10 @@ mod tests { // Only the operator is parsed assert_eq!( parse_operator::(">=3.8"), - Ok(("3.8", VersionOperators::Range(RangeOperator::GreaterEquals))) + Ok(( + "3.8", + VersionOperator::OrdRange(crate::constraint::operators::OrdOperator::Ge) + )) ); }