Skip to content

Commit

Permalink
implement build number spec for match spec, addresses #96
Browse files Browse the repository at this point in the history
  • Loading branch information
YeungOnion committed Sep 20, 2023
1 parent 51e959b commit 0c487af
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 16 deletions.
6 changes: 2 additions & 4 deletions crates/rattler_conda_types/src/build_spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pub mod parse;

pub use parse::ParseBuildNumberSpecError;
use serde::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};

Expand All @@ -26,7 +27,7 @@ pub enum OrdOperator {
/// Ideally we could have some kind of type constraint to guarantee that
/// there's function relating the operator and element into a function that returns bool
/// possible TODO: create `Operator<Element>` trait
#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Deserialize)]
#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
pub struct OperatorConstraint<Operator, Element> {
op: Operator,
rhs: Element,
Expand Down Expand Up @@ -62,9 +63,6 @@ impl Display for BuildNumberSpec {
}

impl BuildNumberSpec {
// for PR #340 will allow this pass CI
// expect following PR where `matches` will be provided with an impl of publicly declared trait
#[allow(unused)]
/// Returns whether the number matches the specification.
/// Expected use is within match_spec::MatchSpec::matches
pub fn matches(&self, build_num: &BuildNumber) -> bool {
Expand Down
1 change: 1 addition & 0 deletions crates/rattler_conda_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub mod package;
mod package_name;
pub mod prefix_record;

pub use build_spec::{BuildNumber, BuildNumberSpec, ParseBuildNumberSpecError};
pub use channel::{Channel, ChannelConfig, ParseChannelError};
pub use channel_data::{ChannelData, ChannelDataPackage};
pub use explicit_environment_spec::{
Expand Down
12 changes: 9 additions & 3 deletions crates/rattler_conda_types/src/match_spec/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::{PackageName, PackageRecord, VersionSpec};
use crate::{build_spec::BuildNumberSpec, PackageName, PackageRecord, VersionSpec};
use rattler_digest::{serde::SerializableHash, Md5Hash, Sha256Hash};
use serde::{Deserialize, Serialize};
use serde_with::{serde_as, skip_serializing_none, DisplayFromStr};
Expand Down Expand Up @@ -121,7 +121,7 @@ pub struct MatchSpec {
/// The build string of the package (e.g. `py37_0`, `py37h6de7cb9_0`, `py*`)
pub build: Option<StringMatcher>,
/// The build number of the package
pub build_number: Option<u64>,
pub build_number: Option<BuildNumberSpec>,
/// Match the specific filename of the package
pub file_name: Option<String>,
/// The channel of the package
Expand Down Expand Up @@ -207,6 +207,12 @@ impl MatchSpec {
}
}

if let Some(build_number) = self.build_number.as_ref() {
if !build_number.matches(&record.build_number) {
return false;
}
}

if let Some(md5_spec) = self.md5.as_ref() {
if Some(md5_spec) != record.md5.as_ref() {
return false;
Expand Down Expand Up @@ -254,7 +260,7 @@ pub struct NamelessMatchSpec {
#[serde_as(as = "Option<DisplayFromStr>")]
pub build: Option<StringMatcher>,
/// The build number of the package
pub build_number: Option<u64>,
pub build_number: Option<BuildNumberSpec>,
/// Match the specific filename of the package
pub file_name: Option<String>,
/// The channel of the package
Expand Down
22 changes: 16 additions & 6 deletions crates/rattler_conda_types/src/match_spec/parse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use super::matcher::{StringMatcher, StringMatcherParseError};
use super::MatchSpec;
use crate::build_spec::{BuildNumberSpec, ParseBuildNumberSpecError};
use crate::package::ArchiveType;
use crate::version_spec::version_tree::{recognize_constraint, recognize_version};
use crate::version_spec::{is_start_of_version_constraint, ParseVersionSpecError};
Expand All @@ -17,7 +18,6 @@ use nom::{Finish, IResult};
use rattler_digest::{parse_digest_from_hex, Md5, Sha256};
use smallvec::SmallVec;
use std::borrow::Cow;
use std::num::ParseIntError;
use std::path::PathBuf;
use std::str::FromStr;
use thiserror::Error;
Expand Down Expand Up @@ -66,9 +66,9 @@ pub enum ParseMatchSpecError {
#[error(transparent)]
InvalidStringMatcher(#[from] StringMatcherParseError),

/// Invalid build number
#[error("invalid build number: {0}")]
InvalidBuildNumber(#[from] ParseIntError),
/// Invalid build number spec
#[error("invalid build number spec: {0}")]
InvalidBuildNumber(#[from] ParseBuildNumberSpecError),

/// Unable to parse hash digest from hex
#[error("Unable to parse hash digest from hex")]
Expand Down Expand Up @@ -205,7 +205,7 @@ fn parse_bracket_vec_into_components(
match key {
"version" => match_spec.version = Some(VersionSpec::from_str(value)?),
"build" => match_spec.build = Some(StringMatcher::from_str(value)?),
"build_number" => match_spec.build_number = Some(value.parse()?),
"build_number" => match_spec.build_number = Some(BuildNumberSpec::from_str(value)?),
"sha256" => {
match_spec.sha256 = Some(
parse_digest_from_hex::<Sha256>(value)
Expand Down Expand Up @@ -473,7 +473,7 @@ mod tests {
split_version_and_build, strip_brackets, BracketVec, MatchSpec, ParseMatchSpecError,
};
use crate::match_spec::parse::parse_bracket_list;
use crate::{NamelessMatchSpec, VersionSpec};
use crate::{BuildNumberSpec, NamelessMatchSpec, VersionSpec};
use smallvec::smallvec;

#[test]
Expand Down Expand Up @@ -579,6 +579,16 @@ mod tests {
assert_eq!(spec.name, Some("foo".parse().unwrap()));
assert_eq!(spec.version, Some(VersionSpec::from_str("1.0.*").unwrap()));
assert_eq!(spec.channel, Some("conda-forge".to_string()));

let spec =
MatchSpec::from_str(r#"conda-forge::foo[version=1.0.*, build_number=">6"]"#).unwrap();
assert_eq!(spec.name, Some("foo".parse().unwrap()));
assert_eq!(spec.version, Some(VersionSpec::from_str("1.0.*").unwrap()));
assert_eq!(spec.channel, Some("conda-forge".to_string()));
assert_eq!(
spec.build_number,
Some(BuildNumberSpec::from_str(">6").unwrap())
);
}

#[test]
Expand Down
6 changes: 3 additions & 3 deletions crates/rattler_conda_types/src/repo_data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ use url::Url;
use rattler_macros::sorted;

use crate::{
package::IndexJson, utils::serde::DeserializeFromStrUnchecked, Channel, NoArchType,
PackageName, Platform, RepoDataRecord, VersionWithSource,
build_spec::BuildNumber, package::IndexJson, utils::serde::DeserializeFromStrUnchecked,
Channel, NoArchType, PackageName, Platform, RepoDataRecord, VersionWithSource,
};

/// [`RepoData`] is an index of package binaries available on in a subdirectory of a Conda channel.
Expand Down Expand Up @@ -79,7 +79,7 @@ pub struct PackageRecord {
pub build: String,

/// The build number of the package
pub build_number: u64,
pub build_number: BuildNumber,

/// Additional constraints on packages. `constrains` are different from `depends` in that packages
/// specified in `depends` must be installed next to this package, whereas packages specified in
Expand Down

0 comments on commit 0c487af

Please sign in to comment.