Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion purl/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ package-type = ["phf", "unicase"]
hex = "0.4.3"
percent-encoding = "2.2.0"
phf = { version = "0.11.1", features = ["macros", "unicase"], optional = true }
serde = { version = "1.0.150", optional = true }
serde = { version = "1.0.150", optional = true, features = ["derive"] }
smartstring = { version = "1.0.1", optional = true }
thiserror = "1.0.37"
unicase = { version = "2.6.0", optional = true }
Expand Down
24 changes: 24 additions & 0 deletions purl/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,23 @@ impl Purl {

builder
}

/// Combine the namespace and ecosystem in the ecosystem-specific format.
pub fn combined_name(&self) -> Cow<'_, str> {
match self.package_type {
PackageType::Cargo | PackageType::Gem | PackageType::NuGet | PackageType::PyPI => {
self.name().into()
},
PackageType::Golang | PackageType::Npm => match self.namespace() {
Some(namespace) => Cow::Owned(format!("{}/{}", namespace, self.name())),
None => self.name().into(),
},
PackageType::Maven => match self.namespace() {
Some(namespace) => Cow::Owned(format!("{}:{}", namespace, self.name())),
None => self.name().into(),
},
}
}
}

/// Check whether a package type string is valid according to the rules of the
Expand Down Expand Up @@ -598,11 +615,18 @@ mod tests {
Purl::builder_with_combined_name(PackageType::Npm, "@angular/cli").build().unwrap();
assert_eq!(purl.namespace(), Some("@angular"));
assert_eq!(purl.name(), "cli");
assert_eq!(purl.combined_name(), "@angular/cli");

let purl = Purl::builder_with_combined_name(PackageType::Maven, "org.maven.plugins:pom")
.build()
.unwrap();
assert_eq!(purl.namespace(), Some("org.maven.plugins"));
assert_eq!(purl.name(), "pom");
assert_eq!(purl.combined_name(), "org.maven.plugins:pom");

let purl = Purl::builder_with_combined_name(PackageType::Cargo, "libc").build().unwrap();
assert_eq!(purl.namespace(), None);
assert_eq!(purl.name(), "libc");
assert_eq!(purl.combined_name(), "libc");
}
}
4 changes: 4 additions & 0 deletions purl/src/package_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ use std::fmt;
use std::str::FromStr;

use phf::phf_map;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use unicase::UniCase;

use crate::{
Expand Down Expand Up @@ -134,6 +136,8 @@ pub type PurlBuilder = GenericPurlBuilder<PackageType>;
///
/// This is a subset of the types described in the PURL spec repository. See
/// [`Purl`] for details.
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
#[non_exhaustive]
pub enum PackageType {
Expand Down
Loading