Skip to content

Commit

Permalink
feat: add JsrDepPackageReq (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret authored Jan 21, 2024
1 parent f3cd7de commit 795694e
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/jsr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,35 @@ impl<'de> Deserialize<'de> for JsrPackageNvReference {
}
}

/// A package constraint for a JSR dependency which could be from npm or JSR.
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct JsrDepPackageReq {
pub kind: PackageKind,
pub req: PackageReq,
}

impl std::fmt::Display for JsrDepPackageReq {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", self.kind.scheme_with_colon(), self.req)
}
}

impl JsrDepPackageReq {
pub fn jsr(req: PackageReq) -> Self {
Self {
kind: PackageKind::Jsr,
req,
}
}

pub fn npm(req: PackageReq) -> Self {
Self {
kind: PackageKind::Npm,
req,
}
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -189,4 +218,16 @@ mod test {
);
}
}

#[test]
fn jsr_dep_package_req_display() {
assert_eq!(
JsrDepPackageReq::jsr(PackageReq::from_str("b@1").unwrap()).to_string(),
"jsr:b@1"
);
assert_eq!(
JsrDepPackageReq::npm(PackageReq::from_str("c@1").unwrap()).to_string(),
"npm:c@1"
);
}
}

0 comments on commit 795694e

Please sign in to comment.