use minicbor::{data::Tag, decode::Error, encode::Write, Decoder, Encoder};
use uuid::Uuid;
pub const TAG: Tag = Tag::new(37);
pub fn encode<C, W: Write>(
uuid: &Uuid,
e: &mut Encoder<W>,
_ctx: &mut C,
) -> Result<(), minicbor::encode::Error<W::Error>> {
e.tag(TAG)?.bytes(uuid.as_bytes())?;
Ok(())
}
pub fn decode<C>(d: &mut Decoder, _ctx: &mut C) -> Result<Uuid, Error> {
if d.tag()? != TAG {
return Err(Error::message("invalid UUID tag"));
};
let uuid = d.bytes()?;
if uuid.len() != 16 {
return Err(Error::message("invalid UUID size"));
}
let mut buf = [0u8; 16];
buf.copy_from_slice(uuid);
Ok(Uuid::from_bytes(buf))
}