1use std::str::FromStr;
2
3use dcbor::prelude::*;
4
5use crate::{Error, tags};
6
7#[derive(Clone, Copy, Debug, Eq, PartialEq)]
26pub struct UUID([u8; Self::UUID_SIZE]);
27
28impl UUID {
29 pub const UUID_SIZE: usize = 16;
30
31 pub fn new() -> Self {
33 let mut uuid = [0u8; Self::UUID_SIZE];
34 bc_rand::fill_random_data(&mut uuid);
35 uuid[6] = (uuid[6] & 0x0f) | 0x40; uuid[8] = (uuid[8] & 0x3f) | 0x80; Self(uuid)
38 }
39
40 pub fn from_data(data: [u8; Self::UUID_SIZE]) -> Self { Self(data) }
42
43 pub fn from_data_ref(data: impl AsRef<[u8]>) -> Option<Self> {
45 let data = data.as_ref();
46 if data.len() != Self::UUID_SIZE {
47 return None;
48 }
49 let mut arr = [0u8; Self::UUID_SIZE];
50 arr.copy_from_slice(data);
51 Some(Self::from_data(arr))
52 }
53
54 pub fn data(&self) -> &[u8; Self::UUID_SIZE] { self.into() }
56
57 pub fn as_bytes(&self) -> &[u8] { self.as_ref() }
59}
60
61impl Default for UUID {
63 fn default() -> Self { Self::new() }
64}
65
66impl<'a> From<&'a UUID> for &'a [u8; UUID::UUID_SIZE] {
68 fn from(value: &'a UUID) -> Self { &value.0 }
69}
70
71impl AsRef<[u8]> for UUID {
73 fn as_ref(&self) -> &[u8] { &self.0 }
74}
75
76impl CBORTagged for UUID {
78 fn cbor_tags() -> Vec<Tag> { tags_for_values(&[tags::TAG_UUID]) }
79}
80
81impl From<UUID> for CBOR {
83 fn from(value: UUID) -> Self { value.tagged_cbor() }
84}
85
86impl CBORTaggedEncodable for UUID {
88 fn untagged_cbor(&self) -> CBOR { CBOR::to_byte_string(self.0) }
89}
90
91impl TryFrom<CBOR> for UUID {
93 type Error = dcbor::Error;
94
95 fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
96 Self::from_tagged_cbor(cbor)
97 }
98}
99
100impl CBORTaggedDecodable for UUID {
102 fn from_untagged_cbor(cbor: CBOR) -> dcbor::Result<Self> {
103 let bytes = CBOR::try_into_byte_string(cbor)?;
104 if bytes.len() != Self::UUID_SIZE {
105 return Err("invalid UUID size".into());
106 }
107 let mut uuid = [0u8; Self::UUID_SIZE];
108 uuid.copy_from_slice(&bytes);
109 Ok(Self::from_data(uuid))
110 }
111}
112
113impl std::fmt::Display for UUID {
115 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
116 write!(f, "{}", String::from(self))
117 }
118}
119
120impl From<UUID> for String {
123 fn from(uuid: UUID) -> Self {
124 let hex = hex::encode(uuid.0);
125 format!(
126 "{}-{}-{}-{}-{}",
127 &hex[0..8],
128 &hex[8..12],
129 &hex[12..16],
130 &hex[16..20],
131 &hex[20..32]
132 )
133 }
134}
135
136impl From<&UUID> for String {
138 fn from(uuid: &UUID) -> Self { String::from(*uuid) }
139}
140
141impl FromStr for UUID {
143 type Err = Error;
144
145 fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
146 let s = s.trim();
147 let s = s.replace('-', "");
148 let bytes = hex::decode(s).unwrap();
149 let mut uuid = [0u8; Self::UUID_SIZE];
150 uuid.copy_from_slice(&bytes);
151 Ok(Self::from_data(uuid))
152 }
153}