Skip to main content

Purl

Struct Purl 

Source
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

Source

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.

Source

pub fn as_str(&self) -> &str

Exactly the text that was parsed.

Source

pub fn package_type(&self) -> &str

The type, lowercased.

Source

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).

Source

pub fn name(&self) -> &str

The decoded name, as written. No type-specific normalization is applied (see Self::canonical).

Source

pub fn version(&self) -> Option<&str>

The decoded version, as written, or None when absent or empty.

Source

pub fn qualifiers(&self) -> impl Iterator<Item = (&str, &str)>

Decoded qualifiers with non-empty values, keys lowercased, in key order.

Source

pub fn qualifier(&self, key: &str) -> Option<&str>

The decoded value of one qualifier; key is matched in lowercase.

Source

pub fn subpath(&self) -> Option<String>

The decoded subpath segments joined with /, without empty, . and .. segments, or None when none remain.

Source

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);
Source

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.

Trait Implementations§

Source§

impl Clone for Purl

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Purl

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for Purl

Source§

impl Hash for Purl

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Purl

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for Purl

Auto Trait Implementations§

§

impl Forget for Purl

§

impl Freeze for Purl

§

impl Move for Purl

§

impl RefUnwindSafe for Purl

§

impl Send for Purl

§

impl Sync for Purl

§

impl Unpin for Purl

§

impl UnsafeUnpin for Purl

§

impl UnwindSafe for Purl

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.