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