Skip to content

Commit

Permalink
hide implementation detail, version_spec::Constraint (#341)
Browse files Browse the repository at this point in the history
  • Loading branch information
YeungOnion authored Sep 16, 2023
1 parent bfe5095 commit 8070e2e
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
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

0 comments on commit 8070e2e

Please sign in to comment.