use std::convert::Infallible;
use crate::Data;
pub trait EncodeError: std::error::Error + 'static {}
impl EncodeError for Infallible {}
impl EncodeError for serde_json::Error {}
pub trait Encode {
type Error: EncodeError;
fn try_serialize(self) -> Result<Data, Self::Error>;
}
impl Encode for Vec<u8> {
type Error = Infallible;
fn try_serialize(self) -> Result<Data, Self::Error> {
Ok(self.into())
}
}
impl Encode for &[u8] {
type Error = Infallible;
fn try_serialize(self) -> Result<Data, Self::Error> {
Ok(self.to_vec().into())
}
}
impl Encode for String {
type Error = Infallible;
fn try_serialize(self) -> Result<Data, Self::Error> {
Ok(self.into_bytes().into())
}
}
impl Encode for &str {
type Error = Infallible;
fn try_serialize(self) -> Result<Data, Self::Error> {
Ok(self.as_bytes().to_vec().into())
}
}
impl Encode for Option<Vec<u8>> {
type Error = Infallible;
fn try_serialize(self) -> Result<Data, Self::Error> {
match self {
Some(v) => Ok(v.into()),
None => Ok(Vec::new().into()),
}
}
}
impl Encode for () {
type Error = Infallible;
fn try_serialize(self) -> Result<Data, Self::Error> {
Ok(Vec::new().into())
}
}
impl Encode for serde_json::Value {
type Error = serde_json::Error;
fn try_serialize(self) -> Result<Data, Self::Error> {
serde_json::to_vec(&self).map(Into::into)
}
}
pub trait ExtractError: std::error::Error + 'static {}
impl ExtractError for Infallible {}
impl ExtractError for serde_json::Error {}
pub trait Extract: Sized {
type Error: ExtractError;
fn extract(payload: Data) -> Result<Self, Self::Error>;
}
impl Extract for Vec<u8> {
type Error = Infallible;
fn extract(payload: Data) -> Result<Self, Self::Error> {
Ok(payload.into_bytes())
}
}
impl Extract for Data {
type Error = Infallible;
fn extract(payload: Data) -> Result<Self, Self::Error> {
Ok(payload)
}
}
pub struct Json<T>(pub T);
impl<T: serde::de::DeserializeOwned> Extract for Json<T> {
type Error = serde_json::Error;
fn extract(payload: Data) -> Result<Self, Self::Error> {
Ok(Json(serde_json::from_slice(&payload.into_bytes())?))
}
}
impl<T: serde::Serialize> Encode for Json<T> {
type Error = serde_json::Error;
fn try_serialize(self) -> Result<Data, Self::Error> {
serde_json::to_vec(&self.0).map(Into::into)
}
}