use std::str::FromStr;
use dcbor::prelude::*;
use crate::{Error, tags};
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct UUID([u8; Self::UUID_SIZE]);
impl UUID {
pub const UUID_SIZE: usize = 16;
pub fn new() -> Self {
let mut uuid = [0u8; Self::UUID_SIZE];
bc_rand::fill_random_data(&mut uuid);
uuid[6] = (uuid[6] & 0x0f) | 0x40; uuid[8] = (uuid[8] & 0x3f) | 0x80; Self(uuid)
}
pub fn from_data(data: [u8; Self::UUID_SIZE]) -> Self { Self(data) }
pub fn from_data_ref(data: impl AsRef<[u8]>) -> Option<Self> {
let data = data.as_ref();
if data.len() != Self::UUID_SIZE {
return None;
}
let mut arr = [0u8; Self::UUID_SIZE];
arr.copy_from_slice(data);
Some(Self::from_data(arr))
}
pub fn data(&self) -> &[u8; Self::UUID_SIZE] { self.into() }
pub fn as_bytes(&self) -> &[u8] { self.as_ref() }
}
impl Default for UUID {
fn default() -> Self { Self::new() }
}
impl<'a> From<&'a UUID> for &'a [u8; UUID::UUID_SIZE] {
fn from(value: &'a UUID) -> Self { &value.0 }
}
impl AsRef<[u8]> for UUID {
fn as_ref(&self) -> &[u8] { &self.0 }
}
impl CBORTagged for UUID {
fn cbor_tags() -> Vec<Tag> { tags_for_values(&[tags::TAG_UUID]) }
}
impl From<UUID> for CBOR {
fn from(value: UUID) -> Self { value.tagged_cbor() }
}
impl CBORTaggedEncodable for UUID {
fn untagged_cbor(&self) -> CBOR { CBOR::to_byte_string(self.0) }
}
impl TryFrom<CBOR> for UUID {
type Error = dcbor::Error;
fn try_from(cbor: CBOR) -> dcbor::Result<Self> {
Self::from_tagged_cbor(cbor)
}
}
impl CBORTaggedDecodable for UUID {
fn from_untagged_cbor(cbor: CBOR) -> dcbor::Result<Self> {
let bytes = CBOR::try_into_byte_string(cbor)?;
if bytes.len() != Self::UUID_SIZE {
return Err("invalid UUID size".into());
}
let mut uuid = [0u8; Self::UUID_SIZE];
uuid.copy_from_slice(&bytes);
Ok(Self::from_data(uuid))
}
}
impl std::fmt::Display for UUID {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", String::from(self))
}
}
impl From<UUID> for String {
fn from(uuid: UUID) -> Self {
let hex = hex::encode(uuid.0);
format!(
"{}-{}-{}-{}-{}",
&hex[0..8],
&hex[8..12],
&hex[12..16],
&hex[16..20],
&hex[20..32]
)
}
}
impl From<&UUID> for String {
fn from(uuid: &UUID) -> Self { String::from(*uuid) }
}
impl FromStr for UUID {
type Err = Error;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
let s = s.trim();
let s = s.replace('-', "");
let bytes = hex::decode(s).unwrap();
let mut uuid = [0u8; Self::UUID_SIZE];
uuid.copy_from_slice(&bytes);
Ok(Self::from_data(uuid))
}
}