use gotham::{
hyper::body::Bytes,
mime::{Mime, APPLICATION_JSON}
};
#[cfg(feature = "openapi")]
use openapi_type::OpenapiType;
use serde::{de::DeserializeOwned, Serialize};
use std::error::Error;
#[cfg(not(feature = "openapi"))]
pub trait ResourceType {}
#[cfg(not(feature = "openapi"))]
impl<T> ResourceType for T {}
#[cfg(feature = "openapi")]
pub trait ResourceType: OpenapiType {}
#[cfg(feature = "openapi")]
impl<T: OpenapiType> ResourceType for T {}
pub trait ResponseBody: ResourceType + Serialize {}
impl<T: ResourceType + Serialize> ResponseBody for T {}
pub trait FromBody: Sized {
type Err: Error;
fn from_body(body: Bytes, content_type: Mime) -> Result<Self, Self::Err>;
}
impl<T: DeserializeOwned> FromBody for T {
type Err = serde_json::Error;
fn from_body(body: Bytes, _content_type: Mime) -> Result<Self, Self::Err> {
serde_json::from_slice(&body)
}
}
pub trait RequestBody: ResourceType + FromBody {
fn supported_types() -> Option<Vec<Mime>> {
None
}
}
impl<T: ResourceType + DeserializeOwned> RequestBody for T {
fn supported_types() -> Option<Vec<Mime>> {
Some(vec![APPLICATION_JSON])
}
}