use std::fmt;
use crate::errors::Error;
#[derive(Clone, Debug)]
pub struct Uuid {
pub data: [u8; 16],
pub uuid: String,
}
impl Uuid {
pub fn new(data: [u8; 16]) -> Self {
let mut uuid = hex::encode(data);
uuid.insert(20, '-');
uuid.insert(16, '-');
uuid.insert(12, '-');
uuid.insert(8, '-');
Self { data, uuid }
}
pub fn parse(uuid: String) -> Result<Self, Error> {
let hex = uuid.replace("-", "");
let vec = hex::decode(hex)?;
let mut data = [0u8; 16];
(0..16).for_each(|i| data[i] = vec[i]);
Ok(Self { data, uuid })
}
}
impl fmt::Display for Uuid {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.uuid)
}
}