From 795694e25c6f91c56a5fcd04693d07645909763f Mon Sep 17 00:00:00 2001 From: David Sherret Date: Sun, 21 Jan 2024 14:11:56 -0500 Subject: [PATCH] feat: add JsrDepPackageReq (#17) --- src/jsr.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/src/jsr.rs b/src/jsr.rs index 7508171..6d5e21d 100644 --- a/src/jsr.rs +++ b/src/jsr.rs @@ -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::*; @@ -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" + ); + } }