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

New StrictVersion type for VersionSpec ranges. #296

Merged
merged 7 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
13 changes: 13 additions & 0 deletions crates/rattler_conda_types/src/match_spec/matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
fmt::{Display, Formatter},
str::FromStr,
};
use std::hash::{Hash, Hasher};

/// Match a given string either by exact match, glob or regex
#[derive(Debug, Clone)]
Expand All @@ -19,6 +20,18 @@ pub enum StringMatcher {
Regex(regex::Regex),
}


impl Hash for StringMatcher {
fn hash<H: Hasher>(&self, state: &mut H) {
match self {
StringMatcher::Exact(s) => s.hash(state),
StringMatcher::Glob(pattern) => pattern.hash(state),
StringMatcher::Regex(regex) => regex.as_str().hash(state),
}
}
}


impl PartialEq for StringMatcher {
fn eq(&self, other: &Self) -> bool {
match (self, other) {
Expand Down
47 changes: 47 additions & 0 deletions crates/rattler_conda_types/src/match_spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use rattler_digest::{serde::SerializableHash, Md5Hash, Sha256Hash};
use serde::Serialize;
use serde_with::{serde_as, skip_serializing_none};
use std::fmt::{Debug, Display, Formatter};
use std::hash::{Hash, Hasher};

pub mod matcher;
pub mod parse;
Expand Down Expand Up @@ -344,13 +345,29 @@ impl MatchSpec {
}
}

impl Hash for MatchSpec {
tdejager marked this conversation as resolved.
Show resolved Hide resolved
fn hash<H: Hasher>(&self, state: &mut H) {
self.name.hash(state);
self.version.hash(state);
self.build.hash(state);
self.build_number.hash(state);
self.file_name.hash(state);
self.channel.hash(state);
self.subdir.hash(state);
self.namespace.hash(state);
self.md5.hash(state);
self.sha256.hash(state);
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use rattler_digest::{parse_digest_from_hex, Md5, Sha256};

use crate::{MatchSpec, NamelessMatchSpec, PackageRecord, Version};
use std::hash::{Hash, Hasher};

#[test]
fn test_matchspec_format_eq() {
Expand All @@ -370,6 +387,36 @@ mod tests {
assert_eq!(spec, rebuild_spec)
}

#[test]
fn test_hash_match() {
// These should not be equal as they are unequal ranges
tdejager marked this conversation as resolved.
Show resolved Hide resolved
let spec1 = MatchSpec::from_str("tensorflow 2.6.*").unwrap();
let spec2 = MatchSpec::from_str("tensorflow 2.6.*").unwrap();
assert_eq!(spec1, spec2);

let mut hasher = std::collections::hash_map::DefaultHasher::new();
let hash1 = spec1.hash(&mut hasher);
let hash2 = spec2.hash(&mut hasher);

assert_eq!(hash1, hash2);
}

#[test]
fn test_hash_no_match() {
let spec1 = MatchSpec::from_str("tensorflow 2.6.0.*").unwrap();
let spec2 = MatchSpec::from_str("tensorflow 2.6.*").unwrap();
dbg!(&spec1, &spec2);
tdejager marked this conversation as resolved.
Show resolved Hide resolved
assert_ne!(spec1, spec2);

let mut hasher = std::collections::hash_map::DefaultHasher::new();
spec1.hash(&mut hasher);
let hash1 = hasher.finish();
spec2.hash(&mut hasher);
let hash2 = hasher.finish();

assert_ne!(hash1, hash2);
}

#[test]
fn test_digest_match() {
let record = PackageRecord {
Expand Down
66 changes: 65 additions & 1 deletion crates/rattler_conda_types/src/version/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,52 @@ impl<'v> SegmentIter<'v> {
}
}

/// Version that only has equality when it is exactly the same
/// e.g for [`Version`] 1.0.0 == 1.0 while in [`StrictVersion`]
/// this is not equal. Useful in ranges where we are talking
/// about equality over version ranges instead of specific
/// version instances
#[derive(Clone, Ord, Eq, Debug)]
pub struct StrictVersion(pub Version);

impl PartialEq for StrictVersion {
fn eq(&self, other: &Self) -> bool {
// StrictVersion is only equal if the number
// of components are the same
// and the components are the same
self.0.components.len() == other.0.components.len() && self.0 == other.0
}
}

impl PartialOrd for StrictVersion {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.0.cmp(&other.0))
}
}

impl Display for StrictVersion {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}

impl Hash for StrictVersion {
fn hash<H: Hasher>(&self, state: &mut H) {
fn hash_segments<'i, I: Iterator<Item = SegmentIter<'i>>, H: Hasher>(
state: &mut H,
segments: I,
) {
for segment in segments {
segment.components().rev().for_each(|c| c.hash(state));
}
}

self.0.epoch().hash(state);
hash_segments(state, self.0.segments());
hash_segments(state, self.0.local_segments());
}
}

#[cfg(test)]
mod test {
use std::cmp::Ordering;
Expand All @@ -950,6 +996,8 @@ mod test {

use rand::seq::SliceRandom;

use crate::version::StrictVersion;

use super::Version;

// Tests are inspired by: https://github.com/conda/conda/blob/33a142c16530fcdada6c377486f1c1a385738a96/tests/models/test_version.py
Expand Down Expand Up @@ -1135,6 +1183,22 @@ mod test {
assert_eq!(random_versions, parsed_versions);
}

#[test]
fn strict_version_test() {
let v_1_0 = StrictVersion::from_str("1.0.0").unwrap();
// Should be equal to itself
assert_eq!(v_1_0, v_1_0);
let v_1_0_0 = StrictVersion::from_str("1.0").unwrap();
// Strict version should not discard zero's
assert_ne!(v_1_0, v_1_0_0);
// Ordering should stay the same as version
assert_eq!(v_1_0.cmp(&v_1_0_0), Ordering::Equal);

// Hashing should consider v_1_0 and v_1_0_0 as unequal
assert_eq!(get_hash(&v_1_0), get_hash(&v_1_0));
assert_ne!(get_hash(&v_1_0), get_hash(&v_1_0_0));
}

#[test]
fn bump() {
assert_eq!(
Expand All @@ -1158,7 +1222,7 @@ mod test {
.starts_with(&Version::from_str("1.2").unwrap()));
}

fn get_hash(spec: &Version) -> u64 {
fn get_hash(spec: &impl Hash) -> u64 {
let mut s = DefaultHasher::new();
spec.hash(&mut s);
s.finish()
Expand Down
10 changes: 9 additions & 1 deletion crates/rattler_conda_types/src/version/parse.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{Component, Version};
use super::{Component, StrictVersion, Version};
use crate::version::flags::Flags;
use crate::version::segment::Segment;
use crate::version::{ComponentVec, SegmentVec};
Expand Down Expand Up @@ -437,6 +437,14 @@ impl FromStr for Version {
}
}

impl FromStr for StrictVersion {
type Err = ParseVersionError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(StrictVersion(Version::from_str(s)?))
}
}

#[cfg(test)]
mod test {
use super::Version;
Expand Down
Loading