use std::str::FromStr;
use hex::{FromHex, ToHex};
use serde::{Deserialize, Serialize};
pub type CUIDInner = [u8; 32];
#[derive(Copy, Clone, Hash, PartialEq, Eq, Serialize, Deserialize, Default, PartialOrd, Ord)]
#[serde(transparent)]
#[repr(transparent)]
pub struct CUID(CUIDInner);
impl CUID {
pub fn new(inner: CUIDInner) -> Self {
Self(inner)
}
}
impl AsRef<CUIDInner> for CUID {
fn as_ref(&self) -> &CUIDInner {
&self.0
}
}
impl FromHex for CUID {
type Error = <CUIDInner as FromHex>::Error;
fn from_hex<T: AsRef<[u8]>>(hex: T) -> Result<Self, Self::Error> {
CUIDInner::from_hex(hex).map(Self)
}
}
impl ToHex for CUID {
fn encode_hex<T: std::iter::FromIterator<char>>(&self) -> T {
ToHex::encode_hex(&self.0)
}
fn encode_hex_upper<T: std::iter::FromIterator<char>>(&self) -> T {
ToHex::encode_hex_upper(&self.0)
}
}
impl FromStr for CUID {
type Err = hex::FromHexError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
FromHex::from_hex(s)
}
}
use std::fmt;
impl fmt::Display for CUID {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.encode_hex::<String>())
}
}
impl fmt::Debug for CUID {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_tuple("CUID").field(&self.to_string()).finish()
}
}