Skip to content

Commit

Permalink
fix: correct Eq/PartialEq/Hash for VersionReq (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucacasonato committed Dec 13, 2023
1 parent a117e8e commit 98f9174
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/jsr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use crate::package::PackageReqReferenceParseError;
///
/// This wraps PackageReqReference in order to prevent accidentally
/// mixing this with other schemes.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct JsrPackageReqReference(PackageReqReference);

impl std::fmt::Display for JsrPackageReqReference {
Expand Down
24 changes: 23 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use std::cmp::Ordering;
use std::fmt;
use std::hash::Hash;

use once_cell::sync::Lazy;
use serde::Deserialize;
Expand Down Expand Up @@ -200,12 +201,26 @@ impl RangeSetOrTag {
}

/// A version constraint.
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VersionReq {
raw_text: String,
inner: RangeSetOrTag,
}

impl PartialEq for VersionReq {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
}
}

impl Eq for VersionReq {}

impl Hash for VersionReq {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.inner.hash(state);
}
}

impl VersionReq {
/// Creates a version requirement without examining the raw text.
pub fn from_raw_text_and_inner(
Expand Down Expand Up @@ -333,4 +348,11 @@ mod test {
assert!(req_star.intersects(&req_gte_1)); // again, '*' allows any version.
}
}

#[test]
fn version_req_eq() {
let p1 = VersionReq::parse_from_specifier("1").unwrap();
let p2 = VersionReq::parse_from_specifier("1.x").unwrap();
assert_eq!(p1, p2);
}
}
2 changes: 1 addition & 1 deletion src/npm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,7 +438,7 @@ fn part(input: &str) -> ParseResult<&str> {
///
/// This wraps PackageReqReference in order to prevent accidentally
/// mixing this with other schemes.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct NpmPackageReqReference(PackageReqReference);

impl std::fmt::Display for NpmPackageReqReference {
Expand Down
2 changes: 1 addition & 1 deletion src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ pub struct PackageReqReferenceInvalidWithVersionParseError {
///
/// This contains all the information found in a package specifier other than
/// what kind of package specifier it was.
#[derive(Clone, Debug, PartialEq, Eq)]
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct PackageReqReference {
pub req: PackageReq,
pub sub_path: Option<String>,
Expand Down

0 comments on commit 98f9174

Please sign in to comment.