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

feat(config): Add (Named)PackageIdent::matches_id helpers #5274

Merged
merged 2 commits into from
Nov 27, 2024
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
37 changes: 37 additions & 0 deletions lib/config/src/package/named_package_ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ impl NamedPackageIdent {

out
}

/// Returns true if this ident matches the given package id.
///
/// Semver constraints are matched against the package id's version.
pub fn matches_id(&self, id: &NamedPackageId) -> bool {
if self.full_name() == id.full_name {
if let Some(tag) = &self.tag {
match tag {
Tag::Named(n) => n == &id.version.to_string(),
Tag::VersionReq(v) => v.matches(&id.version),
}
} else {
true
}
} else {
false
}
}
}

impl From<NamedPackageId> for NamedPackageIdent {
Expand Down Expand Up @@ -443,4 +461,23 @@ mod tests {
let ident2 = serde_json::from_str::<NamedPackageIdent>(&raw).unwrap();
assert_eq!(ident, ident2);
}

#[test]
fn test_named_package_ident_matches_id() {
assert!(NamedPackageIdent::from_str("ns/name")
.unwrap()
.matches_id(&NamedPackageId::try_new("ns/name", "0.1.0").unwrap()));

assert!(NamedPackageIdent::from_str("ns/name")
.unwrap()
.matches_id(&NamedPackageId::try_new("ns/name", "1.0.1").unwrap()));

assert!(NamedPackageIdent::from_str("ns/name@1")
.unwrap()
.matches_id(&NamedPackageId::try_new("ns/name", "1.0.1").unwrap()));

assert!(!NamedPackageIdent::from_str("ns/name@2")
.unwrap()
.matches_id(&NamedPackageId::try_new("ns/name", "1.0.1").unwrap()));
}
}
33 changes: 32 additions & 1 deletion lib/config/src/package/package_ident.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::str::FromStr;

use super::{NamedPackageIdent, PackageHash, PackageParseError};
use super::{NamedPackageIdent, PackageHash, PackageId, PackageParseError};

#[derive(PartialEq, Eq, Clone, Debug, Hash)]
pub enum PackageIdent {
Expand All @@ -24,6 +24,17 @@ impl PackageIdent {
None
}
}

/// Returns true if this ident matches the given package id.
///
/// Semver constraints are matched against the package id's version.
pub fn matches_id(&self, id: &PackageId) -> bool {
match (self, id) {
(Self::Named(a), PackageId::Named(b)) => a.matches_id(b),
(Self::Hash(a), PackageId::Hash(b)) => a == b,
_ => false,
}
}
}

impl From<NamedPackageIdent> for PackageIdent {
Expand Down Expand Up @@ -92,3 +103,23 @@ impl schemars::JsonSchema for PackageIdent {
String::json_schema(gen)
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_package_ident_matches_id() {
assert!(PackageIdent::from_str("ns/pkg")
.unwrap()
.matches_id(&PackageId::new_named("ns/pkg", "1.0.0".parse().unwrap())));

assert!(PackageIdent::from_str("ns/pkg@2")
.unwrap()
.matches_id(&PackageId::new_named("ns/pkg", "2.3.7".parse().unwrap())));

assert!(!PackageIdent::from_str("ns/pkg@3")
.unwrap()
.matches_id(&PackageId::new_named("ns/pkg", "2.3.7".parse().unwrap())));
}
}
Loading