use super::Post;
use crate::{ContentType, Decodes, Encodes};
use bytes::Bytes;
use serde::{de::DeserializeOwned, Serialize};
pub struct CborEncoding;
impl ContentType for CborEncoding {
const CONTENT_TYPE: &'static str = "application/cbor";
}
impl<T> Encodes<T> for CborEncoding
where
T: Serialize,
{
type Error = ciborium::ser::Error<std::io::Error>;
fn encode(value: T) -> Result<Bytes, Self::Error> {
let mut buffer: Vec<u8> = Vec::new();
ciborium::ser::into_writer(&value, &mut buffer)?;
Ok(Bytes::from(buffer))
}
}
impl<T> Decodes<T> for CborEncoding
where
T: DeserializeOwned,
{
type Error = ciborium::de::Error<std::io::Error>;
fn decode(bytes: Bytes) -> Result<T, Self::Error> {
ciborium::de::from_reader(bytes.as_ref())
}
}
pub type Cbor = Post<CborEncoding>;