gotham_restful/types.rs
1use gotham::{
2 hyper::body::Bytes,
3 mime::{Mime, APPLICATION_JSON}
4};
5#[cfg(feature = "openapi")]
6use openapi_type::OpenapiType;
7use serde::{de::DeserializeOwned, Serialize};
8use std::error::Error;
9
10#[cfg(not(feature = "openapi"))]
11pub trait ResourceType {}
12
13#[cfg(not(feature = "openapi"))]
14impl<T> ResourceType for T {}
15
16#[cfg(feature = "openapi")]
17pub trait ResourceType: OpenapiType {}
18
19#[cfg(feature = "openapi")]
20impl<T: OpenapiType> ResourceType for T {}
21
22/// A type that can be used inside a response body. Implemented for every type that is
23/// serializable with serde. If the `openapi` feature is used, it must also be of type
24/// [OpenapiType].
25pub trait ResponseBody: ResourceType + Serialize {}
26
27impl<T: ResourceType + Serialize> ResponseBody for T {}
28
29/// This trait should be implemented for every type that can be built from an HTTP request body
30/// plus its media type.
31///
32/// For most use cases it is sufficient to derive this trait, you usually don't need to manually
33/// implement this. Therefore, make sure that the first variable of your struct can be built from
34/// [Bytes], and the second one can be build from [Mime]. If you have any additional variables, they
35/// need to be [Default]. This is an example of such a struct:
36///
37/// ```rust
38/// # use gotham::mime::{self, Mime};
39/// # use gotham_restful::{FromBody, RequestBody};
40/// #[derive(FromBody, RequestBody)]
41/// #[supported_types(mime::IMAGE_GIF, mime::IMAGE_JPEG, mime::IMAGE_PNG)]
42/// struct RawImage {
43/// content: Vec<u8>,
44/// content_type: Mime
45/// }
46/// ```
47pub trait FromBody: Sized {
48 /// The error type returned by the conversion if it was unsuccessfull. When using the derive
49 /// macro, there is no way to trigger an error, so [std::convert::Infallible] is used here.
50 /// However, this might change in the future.
51 type Err: Error;
52
53 /// Perform the conversion.
54 fn from_body(body: Bytes, content_type: Mime) -> Result<Self, Self::Err>;
55}
56
57impl<T: DeserializeOwned> FromBody for T {
58 type Err = serde_json::Error;
59
60 fn from_body(body: Bytes, _content_type: Mime) -> Result<Self, Self::Err> {
61 serde_json::from_slice(&body)
62 }
63}
64
65/// A type that can be used inside a request body. Implemented for every type that is deserializable
66/// with serde. If the `openapi` feature is used, it must also be of type [OpenapiType].
67///
68/// If you want a non-deserializable type to be used as a request body, e.g. because you'd like to
69/// get the raw data, you can derive it for your own type. All you need is to have a type implementing
70/// [FromBody] and optionally a list of supported media types:
71///
72/// ```rust
73/// # use gotham::mime::{self, Mime};
74/// # use gotham_restful::{FromBody, RequestBody};
75/// #[derive(FromBody, RequestBody)]
76/// #[supported_types(mime::IMAGE_GIF, mime::IMAGE_JPEG, mime::IMAGE_PNG)]
77/// struct RawImage {
78/// content: Vec<u8>,
79/// content_type: Mime
80/// }
81/// ```
82pub trait RequestBody: ResourceType + FromBody {
83 /// Return all types that are supported as content types. Use `None` if all types are supported.
84 fn supported_types() -> Option<Vec<Mime>> {
85 None
86 }
87}
88
89impl<T: ResourceType + DeserializeOwned> RequestBody for T {
90 fn supported_types() -> Option<Vec<Mime>> {
91 Some(vec![APPLICATION_JSON])
92 }
93}