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

hide implementation detail, version_spec::Constraint #341

Merged
merged 1 commit into from
Sep 16, 2023
Merged
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
2 changes: 1 addition & 1 deletion crates/rattler_conda_types/src/version_spec/constraint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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 enum Constraint {
/// Matches anything (`*`)
Any,

Expand Down
32 changes: 18 additions & 14 deletions crates/rattler_conda_types/src/version_spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub(crate) mod version_tree;

use crate::version_spec::version_tree::ParseVersionTreeError;
use crate::{ParseVersionError, Version};
pub(crate) use constraint::Constraint;
use constraint::Constraint;
use serde::{Deserialize, Serialize, Serializer};
use std::convert::TryFrom;
use std::fmt::{Display, Formatter};
Expand All @@ -17,7 +17,7 @@ use version_tree::VersionTree;

use crate::version::StrictVersion;
pub(crate) use constraint::is_start_of_version_constraint;
pub(crate) use parse::ParseConstraintError;
use parse::ParseConstraintError;

/// An operator to compare two versions.
#[allow(missing_docs)]
Expand Down Expand Up @@ -143,6 +143,19 @@ pub enum ParseVersionSpecError {
InvalidConstraint(#[source] ParseConstraintError),
}

impl From<Constraint> for VersionSpec {
fn from(constraint: Constraint) -> Self {
match constraint {
Constraint::Any => VersionSpec::Any,
Constraint::Comparison(op, ver) => VersionSpec::Range(op, ver),
Constraint::StrictComparison(op, ver) => {
VersionSpec::StrictRange(op, StrictVersion(ver))
}
Constraint::Exact(e, ver) => VersionSpec::Exact(e, ver),
}
}
}

impl FromStr for VersionSpec {
type Err = ParseVersionSpecError;

Expand All @@ -152,18 +165,9 @@ impl FromStr for VersionSpec {

fn parse_tree(tree: VersionTree) -> Result<VersionSpec, ParseVersionSpecError> {
match tree {
VersionTree::Term(str) => {
let constraint = Constraint::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) => {
VersionSpec::StrictRange(op, StrictVersion(ver))
}
Constraint::Exact(e, ver) => VersionSpec::Exact(e, ver),
})
}
VersionTree::Term(str) => Ok(Constraint::from_str(str)
.map_err(ParseVersionSpecError::InvalidConstraint)?
.into()),
VersionTree::Group(op, groups) => Ok(VersionSpec::Group(
op,
groups
Expand Down
2 changes: 1 addition & 1 deletion crates/rattler_conda_types/src/version_spec/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ fn logical_constraint_parser(input: &str) -> IResult<&str, Constraint, ParseCons
}

/// Parses a version constraint.
pub(crate) fn constraint_parser(input: &str) -> IResult<&str, Constraint, ParseConstraintError> {
pub fn constraint_parser(input: &str) -> IResult<&str, Constraint, ParseConstraintError> {
alt((
regex_constraint_parser,
any_constraint_parser,
Expand Down
Loading