use crate::serde_base64::SerdeBase64;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
macro_rules! newtypes {
(
$(
$(#[$meta:meta])*
$vis:vis struct $name:ident(..);
)*
) => {
$(
$(#[$meta])*
#[derive(Serialize, Deserialize, Clone, PartialEq, Eq, Debug)]
#[serde(transparent)]
$vis struct $name<'a>(Cow<'a, [u8]>);
#[allow(unused)]
impl<'a> $name<'a> {
$vis const fn borrowed(data: &'a [u8]) -> Self{
Self(Cow::Borrowed(data))
}
$vis fn owned(data: Vec<u8>) -> Self {
Self(Cow::Owned(data))
}
$vis fn as_bytes(&self) -> &[u8] {
&self.0
}
}
impl SerdeBase64 for $name<'static> {
fn from_bytes(bytes: Vec<u8>) -> Result<Self, String> {
Ok(Self(Cow::Owned(bytes)))
}
fn to_bytes(&self) -> &[u8] {
&self.0
}
}
)*
}
}
newtypes! {
pub struct PublicKeyBytes(..);
pub struct PayloadBytes(..);
pub struct SignatureBytes(..);
pub(crate) struct PrivateKeyBytes(..);
}