use crate::{
codec::{Patch, Post, Put},
ContentType, Decodes, Encodes, Format, FormatType,
};
use bytes::Bytes;
use serde::{de::DeserializeOwned, Serialize};
pub struct PostcardEncoding;
impl ContentType for PostcardEncoding {
const CONTENT_TYPE: &'static str = "application/x-postcard";
}
impl FormatType for PostcardEncoding {
const FORMAT_TYPE: Format = Format::Binary;
}
impl<T> Encodes<T> for PostcardEncoding
where
T: Serialize,
{
type Error = postcard::Error;
fn encode(value: &T) -> Result<Bytes, Self::Error> {
postcard::to_allocvec(value).map(Bytes::from)
}
}
impl<T> Decodes<T> for PostcardEncoding
where
T: DeserializeOwned,
{
type Error = postcard::Error;
fn decode(bytes: Bytes) -> Result<T, Self::Error> {
postcard::from_bytes(&bytes)
}
}
pub type Postcard = Post<PostcardEncoding>;
pub type PatchPostcard = Patch<PostcardEncoding>;
pub type PutPostcard = Put<PostcardEncoding>;