2019-10-14 00:59:02 +02:00
|
|
|
#[cfg(feature = "openapi")]
|
2019-10-20 17:42:33 +02:00
|
|
|
use crate::OpenapiType;
|
2019-10-14 00:59:02 +02:00
|
|
|
|
2020-04-14 17:44:07 +02:00
|
|
|
use gotham::hyper::body::Bytes;
|
2019-10-20 14:49:53 +00:00
|
|
|
use mime::{Mime, APPLICATION_JSON};
|
2019-10-14 00:59:02 +02:00
|
|
|
use serde::{de::DeserializeOwned, Serialize};
|
2021-01-18 00:05:30 +00:00
|
|
|
use std::error::Error;
|
2019-10-14 00:59:02 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "openapi"))]
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait ResourceType {}
|
2019-10-14 00:59:02 +02:00
|
|
|
|
|
|
|
#[cfg(not(feature = "openapi"))]
|
2020-09-15 15:10:41 +02:00
|
|
|
impl<T> ResourceType for T {}
|
2019-10-14 00:59:02 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "openapi")]
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait ResourceType: OpenapiType {}
|
2019-10-14 00:59:02 +02:00
|
|
|
|
|
|
|
#[cfg(feature = "openapi")]
|
2020-09-15 15:10:41 +02:00
|
|
|
impl<T: OpenapiType> ResourceType for T {}
|
2019-10-20 14:49:53 +00:00
|
|
|
|
|
|
|
/// A type that can be used inside a response body. Implemented for every type that is
|
|
|
|
/// serializable with serde. If the `openapi` feature is used, it must also be of type
|
2020-11-22 23:55:52 +01:00
|
|
|
/// [OpenapiType].
|
|
|
|
///
|
|
|
|
/// [OpenapiType]: trait.OpenapiType.html
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait ResponseBody: ResourceType + Serialize {}
|
2019-10-20 14:49:53 +00:00
|
|
|
|
2020-09-15 15:10:41 +02:00
|
|
|
impl<T: ResourceType + Serialize> ResponseBody for T {}
|
2019-10-20 14:49:53 +00:00
|
|
|
|
2020-05-05 19:31:02 +02:00
|
|
|
/**
|
|
|
|
This trait should be implemented for every type that can be built from an HTTP request body
|
2020-11-22 23:55:52 +01:00
|
|
|
plus its media type.
|
|
|
|
|
|
|
|
For most use cases it is sufficient to derive this trait, you usually don't need to manually
|
|
|
|
implement this. Therefore, make sure that the first variable of your struct can be built from
|
|
|
|
[Bytes], and the second one can be build from [Mime]. If you have any additional variables, they
|
|
|
|
need to be [Default]. This is an example of such a struct:
|
2020-05-05 19:31:02 +02:00
|
|
|
|
|
|
|
```rust
|
|
|
|
# #[macro_use] extern crate gotham_restful;
|
|
|
|
# use gotham_restful::*;
|
|
|
|
#[derive(FromBody, RequestBody)]
|
|
|
|
#[supported_types(mime::IMAGE_GIF, mime::IMAGE_JPEG, mime::IMAGE_PNG)]
|
|
|
|
struct RawImage {
|
|
|
|
content: Vec<u8>,
|
|
|
|
content_type: Mime
|
|
|
|
}
|
|
|
|
```
|
|
|
|
*/
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait FromBody: Sized {
|
2020-05-05 19:31:02 +02:00
|
|
|
/// The error type returned by the conversion if it was unsuccessfull. When using the derive
|
2020-11-22 23:55:52 +01:00
|
|
|
/// macro, there is no way to trigger an error, so [std::convert::Infallible] is used here.
|
|
|
|
/// However, this might change in the future.
|
2020-09-15 15:10:41 +02:00
|
|
|
type Err: Error;
|
|
|
|
|
2020-05-05 19:31:02 +02:00
|
|
|
/// Perform the conversion.
|
2020-09-15 15:10:41 +02:00
|
|
|
fn from_body(body: Bytes, content_type: Mime) -> Result<Self, Self::Err>;
|
2019-10-20 14:49:53 +00:00
|
|
|
}
|
|
|
|
|
2020-09-15 15:10:41 +02:00
|
|
|
impl<T: DeserializeOwned> FromBody for T {
|
2019-10-20 14:49:53 +00:00
|
|
|
type Err = serde_json::Error;
|
2020-09-15 15:10:41 +02:00
|
|
|
|
|
|
|
fn from_body(body: Bytes, _content_type: Mime) -> Result<Self, Self::Err> {
|
2019-10-20 14:49:53 +00:00
|
|
|
serde_json::from_slice(&body)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-05 19:50:23 +02:00
|
|
|
/**
|
|
|
|
A type that can be used inside a request body. Implemented for every type that is deserializable
|
2020-11-22 23:55:52 +01:00
|
|
|
with serde. If the `openapi` feature is used, it must also be of type [OpenapiType].
|
2020-05-05 19:50:23 +02:00
|
|
|
|
|
|
|
If you want a non-deserializable type to be used as a request body, e.g. because you'd like to
|
|
|
|
get the raw data, you can derive it for your own type. All you need is to have a type implementing
|
2020-11-22 23:55:52 +01:00
|
|
|
[FromBody] and optionally a list of supported media types:
|
2020-05-05 19:50:23 +02:00
|
|
|
|
|
|
|
```rust
|
|
|
|
# #[macro_use] extern crate gotham_restful;
|
|
|
|
# use gotham_restful::*;
|
|
|
|
#[derive(FromBody, RequestBody)]
|
|
|
|
#[supported_types(mime::IMAGE_GIF, mime::IMAGE_JPEG, mime::IMAGE_PNG)]
|
|
|
|
struct RawImage {
|
|
|
|
content: Vec<u8>,
|
|
|
|
content_type: Mime
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
2020-11-22 23:55:52 +01:00
|
|
|
[OpenapiType]: trait.OpenapiType.html
|
2020-05-05 19:50:23 +02:00
|
|
|
*/
|
2020-09-15 15:10:41 +02:00
|
|
|
pub trait RequestBody: ResourceType + FromBody {
|
2020-05-05 19:50:23 +02:00
|
|
|
/// Return all types that are supported as content types. Use `None` if all types are supported.
|
2020-09-15 15:10:41 +02:00
|
|
|
fn supported_types() -> Option<Vec<Mime>> {
|
2019-10-20 14:49:53 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-15 15:10:41 +02:00
|
|
|
impl<T: ResourceType + DeserializeOwned> RequestBody for T {
|
|
|
|
fn supported_types() -> Option<Vec<Mime>> {
|
2019-10-20 14:49:53 +00:00
|
|
|
Some(vec![APPLICATION_JSON])
|
|
|
|
}
|
|
|
|
}
|