provenant/models/
dependency_uid.rs1use std::borrow::Borrow;
5use std::fmt;
6use std::ops::Deref;
7
8use serde::{Deserialize, Serialize};
9use uuid::Uuid;
10
11#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
12#[serde(transparent)]
13pub struct DependencyUid(String);
14
15impl DependencyUid {
16 pub fn new(purl: &str) -> Self {
18 let uuid = Uuid::new_v4();
19 if purl.contains('?') {
20 DependencyUid(format!("{}&uuid={}", purl, uuid))
21 } else {
22 DependencyUid(format!("{}?uuid={}", purl, uuid))
23 }
24 }
25
26 pub fn from_raw(s: String) -> Self {
31 DependencyUid(s)
32 }
33
34 pub fn empty() -> Self {
36 DependencyUid(String::new())
37 }
38
39 pub fn replace_base(&self, new_purl: &str) -> Self {
41 if let Some((_, suffix)) = self.0.split_once("?uuid=") {
42 return DependencyUid(format!("{}?uuid={}", new_purl, suffix));
43 }
44 if let Some((_, suffix)) = self.0.split_once("&uuid=") {
45 let separator = if new_purl.contains('?') { '&' } else { '?' };
46 return DependencyUid(format!("{}{separator}uuid={suffix}", new_purl));
47 }
48 DependencyUid(self.0.clone())
49 }
50}
51
52impl fmt::Display for DependencyUid {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 self.0.fmt(f)
55 }
56}
57
58impl AsRef<str> for DependencyUid {
59 fn as_ref(&self) -> &str {
60 &self.0
61 }
62}
63
64impl Borrow<str> for DependencyUid {
65 fn borrow(&self) -> &str {
66 &self.0
67 }
68}
69
70impl Deref for DependencyUid {
71 type Target = str;
72
73 fn deref(&self) -> &Self::Target {
74 &self.0
75 }
76}