use std::borrow::Borrow;
use std::fmt;
use std::ops::Deref;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
#[serde(transparent)]
pub struct PackageUid(String);
impl PackageUid {
pub fn new(purl: &str) -> Self {
let uuid = Uuid::new_v4();
Self::with_uuid_suffix(purl, uuid)
}
pub fn new_opaque(base: &str) -> Self {
let uuid = Uuid::new_v4();
Self::with_uuid_suffix(base, uuid)
}
fn with_uuid_suffix(base: &str, uuid: Uuid) -> Self {
PackageUid(crate::models::purl::append_uuid_qualifier(
base,
&uuid.to_string(),
))
}
pub fn from_raw(s: String) -> Self {
PackageUid(s)
}
pub fn empty() -> Self {
PackageUid(String::new())
}
pub fn stable_key(&self) -> std::borrow::Cow<'_, str> {
crate::models::purl::strip_uuid_qualifier(&self.0)
}
pub fn replace_base(&self, new_purl: &str) -> Self {
let Some(uuid) = crate::models::purl::uuid_qualifier_value(&self.0) else {
return PackageUid(self.0.clone());
};
PackageUid(crate::models::purl::append_uuid_qualifier(new_purl, uuid))
}
pub fn as_str(&self) -> &str {
&self.0
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl fmt::Display for PackageUid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}
impl AsRef<str> for PackageUid {
fn as_ref(&self) -> &str {
&self.0
}
}
impl Borrow<str> for PackageUid {
fn borrow(&self) -> &str {
&self.0
}
}
impl Deref for PackageUid {
type Target = str;
fn deref(&self) -> &Self::Target {
&self.0
}
}