Skip to content

Commit

Permalink
Improve ergonomics for qualifier keys (#21)
Browse files Browse the repository at this point in the history
* remove unnecessary constraint

* add impls for using QualifierKey as a string

* add without_qualifiers
  • Loading branch information
matt-phylum authored Nov 25, 2024
1 parent 3a22791 commit d4a0282
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
7 changes: 6 additions & 1 deletion purl/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,17 @@ impl<T> GenericPurlBuilder<T> {
pub fn without_qualifier<S>(mut self, k: S) -> Self
where
S: AsRef<str>,
SmallString: From<S>,
{
self.parts.qualifiers.remove(k);
self
}

/// Unset all qualifiers.
pub fn without_qualifiers(mut self) -> Self {
self.parts.qualifiers.clear();
self
}

/// Set the subpath.
///
/// Passing `""` will unset the subpath.
Expand Down
20 changes: 19 additions & 1 deletion purl/src/qualifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ impl<'a> IntoIterator for &'a mut Qualifiers {
/// A case-insensitive qualifier name.
///
/// Comparisons between this type and other types are case insensitive.
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq, PartialOrd, Ord)]
#[derive(Clone, Debug, Default, Eq, Hash, Ord)]
pub struct QualifierKey(SmallString);

impl<S> PartialEq<S> for QualifierKey
Expand Down Expand Up @@ -345,6 +345,24 @@ impl Deref for QualifierKey {
}
}

impl AsRef<str> for QualifierKey {
fn as_ref(&self) -> &str {
self.as_str()
}
}

impl From<QualifierKey> for SmallString {
fn from(value: QualifierKey) -> Self {
SmallString::from(value.0)
}
}

impl From<&QualifierKey> for SmallString {
fn from(value: &QualifierKey) -> Self {
SmallString::from(value.as_str())
}
}

impl QualifierKey {
/// Get a reference to the lower case string.
pub fn as_str(&self) -> &str {
Expand Down

0 comments on commit d4a0282

Please sign in to comment.