use std::rc::Rc;
use anyhow::bail;
use dcbor::prelude::*;
#[derive(Clone, Eq, PartialEq)]
pub struct AuthenticationTag([u8; Self::AUTHENTICATION_TAG_SIZE]);
impl AuthenticationTag {
pub const AUTHENTICATION_TAG_SIZE: usize = 16;
pub const fn from_data(data: [u8; Self::AUTHENTICATION_TAG_SIZE]) -> Self {
Self(data)
}
pub fn from_data_ref(data: impl AsRef<[u8]>) -> anyhow::Result<Self> {
let data = data.as_ref();
if data.len() != Self::AUTHENTICATION_TAG_SIZE {
bail!("Invalid authentication tag size");
}
let mut arr = [0u8; Self::AUTHENTICATION_TAG_SIZE];
arr.copy_from_slice(data.as_ref());
Ok(Self::from_data(arr))
}
pub fn data(&self) -> &[u8; Self::AUTHENTICATION_TAG_SIZE] {
self.into()
}
}
impl AsRef<AuthenticationTag> for AuthenticationTag {
fn as_ref(&self) -> &AuthenticationTag {
self
}
}
impl std::fmt::Debug for AuthenticationTag {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_tuple("AuthenticationTag")
.field(&hex::encode(self.data()))
.finish()
}
}
impl From<Rc<AuthenticationTag>> for AuthenticationTag {
fn from(value: Rc<AuthenticationTag>) -> Self {
(*value).clone()
}
}
impl<'a> From<&'a AuthenticationTag> for &'a [u8; AuthenticationTag::AUTHENTICATION_TAG_SIZE] {
fn from(value: &'a AuthenticationTag) -> Self {
&value.0
}
}
impl From<&[u8]> for AuthenticationTag {
fn from(data: &[u8]) -> Self {
Self::from_data_ref(data).unwrap()
}
}
impl From<[u8; Self::AUTHENTICATION_TAG_SIZE]> for AuthenticationTag {
fn from(data: [u8; Self::AUTHENTICATION_TAG_SIZE]) -> Self {
Self::from_data(data)
}
}
impl From<Vec<u8>> for AuthenticationTag {
fn from(data: Vec<u8>) -> Self {
Self::from_data_ref(data).unwrap()
}
}
impl CBOREncodable for AuthenticationTag {
fn cbor(&self) -> CBOR {
CBOR::byte_string(self.data())
}
}
impl From<AuthenticationTag> for CBOR {
fn from(value: AuthenticationTag) -> Self {
value.cbor()
}
}
impl CBORDecodable for AuthenticationTag {
fn from_cbor(cbor: &CBOR) -> anyhow::Result<Self> {
let data = CBOR::expect_byte_string(cbor)?;
Self::from_data_ref(&data)
}
}
impl TryFrom<CBOR> for AuthenticationTag {
type Error = anyhow::Error;
fn try_from(cbor: CBOR) -> Result<Self, Self::Error> {
Self::from_cbor(&cbor)
}
}
impl TryFrom<&CBOR> for AuthenticationTag {
type Error = anyhow::Error;
fn try_from(cbor: &CBOR) -> Result<Self, Self::Error> {
Self::from_cbor(cbor)
}
}