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