pub struct Purl { /* private fields */ }Expand description
A parsed Package URL. Holds the original text and never rewrites it.
Experimental: may change in a 0.x minor release.
Equality compares the original text and components exactly. To ask
whether two spellings name the same package, compare
Purl::canonical values or use same_package_url.
use libtea_core::purl::{same_package_url, Purl};
let a = Purl::parse("PKG:Maven/org.apache/commons-io@2.11.0?Type=jar&classifier=").unwrap();
let b = Purl::parse("pkg:maven/org.apache/commons-io@2.11.0?type=jar").unwrap();
assert_eq!(a.as_str(), "PKG:Maven/org.apache/commons-io@2.11.0?Type=jar&classifier=");
assert_eq!(a.canonical().as_str(), "pkg:maven/org.apache/commons-io@2.11.0?type=jar");
assert!(same_package_url(&a, &b));Implementations§
Source§impl Purl
impl Purl
Sourcepub fn parse(text: &str) -> Result<Self, PurlError>
pub fn parse(text: &str) -> Result<Self, PurlError>
Parse a Package URL as ECMA-427 describes, right to left.
Accepts what the specification says a parser should accept, such as
pkg:// and redundant slashes, and keeps text unchanged for
Self::as_str.
§Errors
Returns PurlError when a required component is missing, a type or
qualifier key has characters ECMA-427 does not allow, a qualifier key
repeats, a decoded namespace or subpath segment contains /, the
percent-encoding is invalid, or the text or its canonical form is
longer than MAX_PURL_BYTES.
Sourcepub fn package_type(&self) -> &str
pub fn package_type(&self) -> &str
The type, lowercased.
Sourcepub fn namespace(&self) -> Option<String>
pub fn namespace(&self) -> Option<String>
The decoded namespace segments joined with /, or None when there
are none. No type-specific normalization is applied (see
Self::canonical).
Sourcepub fn name(&self) -> &str
pub fn name(&self) -> &str
The decoded name, as written. No type-specific normalization is
applied (see Self::canonical).
Sourcepub fn version(&self) -> Option<&str>
pub fn version(&self) -> Option<&str>
The decoded version, as written, or None when absent or empty.
Sourcepub fn qualifiers(&self) -> impl Iterator<Item = (&str, &str)>
pub fn qualifiers(&self) -> impl Iterator<Item = (&str, &str)>
Decoded qualifiers with non-empty values, keys lowercased, in key order.
Sourcepub fn qualifier(&self, key: &str) -> Option<&str>
pub fn qualifier(&self, key: &str) -> Option<&str>
The decoded value of one qualifier; key is matched in lowercase.
Sourcepub fn subpath(&self) -> Option<String>
pub fn subpath(&self) -> Option<String>
The decoded subpath segments joined with /, without empty, . and
.. segments, or None when none remain.
Sourcepub fn canonical(&self) -> CanonicalPurl
pub fn canonical(&self) -> CanonicalPurl
The canonical form: the generic rules, plus the type’s own rules when
PurlRules has them (CanonicalPurl::rules says which).
use libtea_core::purl::{Purl, PurlRules};
let purl = Purl::parse("pkg:PyPI/Django_Allauth@0.61.1RC1").unwrap();
assert_eq!(purl.name(), "Django_Allauth");
let canonical = purl.canonical();
assert_eq!(canonical.as_str(), "pkg:pypi/django-allauth@0.61.1rc1");
assert_eq!(canonical.rules(), PurlRules::Pypi);Sourcepub fn check_type(&self) -> Result<(), PurlTypeError>
pub fn check_type(&self) -> Result<(), PurlTypeError>
Check the rules the purl-spec type definition gives as data: whether a namespace is required or prohibited, which qualifiers are required, and the permitted characters of the name and version. A type without a definition passes. Rules given only in prose are not checked.
use libtea_core::purl::{Purl, PurlTypeError};
assert_eq!(Purl::parse("pkg:github/package-url/purl-spec").unwrap().check_type(), Ok(()));
let purl = Purl::parse("pkg:github/purl-spec").unwrap();
assert_eq!(purl.check_type(), Err(PurlTypeError::NamespaceRequired));
let purl = Purl::parse("pkg:julia/Dates").unwrap();
assert_eq!(purl.check_type(), Err(PurlTypeError::QualifierRequired("uuid")));§Errors
The first rule the PURL breaks, as a PurlTypeError.