#[cfg(any(feature = "bincode", feature = "cbor", feature = "json"))]
use serde::{de::DeserializeOwned, ser::Serialize};
#[cfg(any(feature = "bincode", feature = "cbor", feature = "json"))]
use crate::error::Error;
use crate::error::Result;
pub trait Encoding<T> {
fn encode(t: &T) -> Result<Vec<u8>>;
fn decode(slice: &[u8]) -> Result<T>;
}
#[derive(Clone, Debug, Default)]
pub struct PlainEncoding;
#[cfg(feature = "bincode")]
#[derive(Clone, Debug, Default)]
pub struct BincodeEncoding;
#[cfg(feature = "cbor")]
#[derive(Clone, Debug, Default)]
pub struct CborEncoding;
#[cfg(feature = "json")]
#[derive(Clone, Debug, Default)]
pub struct JsonEncoding;
impl<T> Encoding<T> for PlainEncoding
where
T: AsRef<[u8]>,
for<'a> T: From<&'a [u8]>,
{
fn encode(t: &T) -> Result<Vec<u8>> {
Ok(t.as_ref().to_vec())
}
fn decode(slice: &[u8]) -> Result<T> {
Ok(slice.into())
}
}
#[cfg(feature = "bincode")]
impl<T> Encoding<T> for BincodeEncoding
where
T: DeserializeOwned + Serialize + 'static,
{
fn encode(t: &T) -> Result<Vec<u8>> {
bincode::serialize(t).map_err(Error::BincodeSerialize)
}
fn decode(slice: &[u8]) -> Result<T> {
bincode::deserialize(slice).map_err(Error::BincodeDeserialize)
}
}
#[cfg(feature = "cbor")]
impl<T> Encoding<T> for CborEncoding
where
T: DeserializeOwned + Serialize + 'static,
{
fn encode(t: &T) -> Result<Vec<u8>> {
serde_cbor::to_vec(t).map_err(Error::CborSerialize)
}
fn decode(slice: &[u8]) -> Result<T> {
serde_cbor::from_slice(slice).map_err(Error::CborDeserialize)
}
}
#[cfg(feature = "json")]
impl<T> Encoding<T> for JsonEncoding
where
T: DeserializeOwned + Serialize + 'static,
{
fn encode(t: &T) -> Result<Vec<u8>> {
serde_json::to_vec(t).map_err(Error::JsonSerialize)
}
fn decode(slice: &[u8]) -> Result<T> {
serde_json::from_slice(slice).map_err(Error::JsonDeserialize)
}
}