use core::fmt;
use uuid::Uuid;
#[derive(Clone, Copy, PartialEq, Eq, Hash)]
pub struct Tag(Uuid);
impl Tag {
pub fn new() -> Self {
Self(Uuid::new_v4())
}
pub const fn from_uuid(uuid: Uuid) -> Self {
Self(uuid)
}
pub fn encode_lower<'a>(&self, buf: &'a mut [u8]) -> &'a str {
self.0.as_hyphenated().encode_lower(buf)
}
pub fn try_from_ascii_bytes(bytes: &[u8]) -> Result<Self, uuid::Error> {
Uuid::try_parse_ascii(bytes).map(Self)
}
}
impl Default for Tag {
fn default() -> Self {
Self::new()
}
}
impl fmt::Debug for Tag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Tag({})", self.0.as_hyphenated())
}
}
impl fmt::Display for Tag {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0.as_hyphenated())
}
}
impl From<Uuid> for Tag {
fn from(uuid: Uuid) -> Self {
Self(uuid)
}
}
impl From<Tag> for Uuid {
fn from(tag: Tag) -> Self {
tag.0
}
}
impl core::str::FromStr for Tag {
type Err = uuid::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
s.parse::<Uuid>().map(Self)
}
}