use alloc::{string::String, vec::Vec};
use serde::{Deserialize, Serialize, de::DeserializeOwned};
use crate::cbor::canonical;
#[cfg(feature = "serde")]
use serde_bytes::ByteBuf;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct CborBytes(pub Vec<u8>);
impl CborBytes {
pub fn new(bytes: Vec<u8>) -> Self {
Self(bytes)
}
pub fn as_slice(&self) -> &[u8] {
&self.0
}
pub fn into_vec(self) -> Vec<u8> {
self.0
}
pub fn ensure_canonical(&self) -> canonical::Result<()> {
canonical::ensure_canonical(&self.0)
}
pub fn canonicalize(self) -> canonical::Result<Self> {
canonical::canonicalize(&self.0).map(Self)
}
pub fn decode<T: DeserializeOwned>(&self) -> canonical::Result<T> {
canonical::from_cbor(&self.0)
}
}
impl From<Vec<u8>> for CborBytes {
fn from(bytes: Vec<u8>) -> Self {
Self(bytes)
}
}
impl From<CborBytes> for Vec<u8> {
fn from(cbor: CborBytes) -> Self {
cbor.0
}
}
#[cfg(feature = "serde")]
impl Serialize for CborBytes {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_bytes(&self.0)
}
}
#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for CborBytes {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
let bytes = ByteBuf::deserialize(deserializer)?;
Ok(CborBytes(bytes.into_vec()))
}
}
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Blob {
pub content_type: String,
pub bytes: Vec<u8>,
}
impl Blob {
pub fn new(content_type: impl Into<String>, bytes: Vec<u8>) -> Self {
Self {
content_type: content_type.into(),
bytes,
}
}
}