mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-04-19 22:44:38 +00:00
separate RequestBody and ResponseBody traits from ResourceType
This commit is contained in:
parent
3a03dc60fa
commit
57e4f36852
6 changed files with 60 additions and 40 deletions
|
@ -126,4 +126,4 @@ pub use routing::{DrawResources, DrawResourceRoutes};
|
|||
pub use routing::WithOpenapi;
|
||||
|
||||
mod types;
|
||||
pub use types::ResourceType;
|
||||
pub use types::*;
|
||||
|
|
|
@ -4,6 +4,7 @@ use crate::{
|
|||
routing::*,
|
||||
OpenapiSchema,
|
||||
OpenapiType,
|
||||
RequestBody,
|
||||
ResourceType
|
||||
};
|
||||
use futures::future::ok;
|
||||
|
@ -21,7 +22,7 @@ use log::error;
|
|||
use mime::{APPLICATION_JSON, TEXT_PLAIN};
|
||||
use openapiv3::{
|
||||
Components, MediaType, OpenAPI, Operation, Parameter, ParameterData, ParameterSchemaOrContent, PathItem,
|
||||
Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody, Response, Responses, Schema,
|
||||
Paths, ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody as OARequestBody, Response, Responses, Schema,
|
||||
SchemaKind, Server, StatusCode, Type
|
||||
};
|
||||
use serde::de::DeserializeOwned;
|
||||
|
@ -280,7 +281,7 @@ fn new_operation(default_status : hyper::StatusCode, schema : ReferenceOr<Schema
|
|||
links: IndexMap::new()
|
||||
}));
|
||||
|
||||
let request_body = body_schema.map(|schema| Item(RequestBody {
|
||||
let request_body = body_schema.map(|schema| Item(OARequestBody {
|
||||
description: None,
|
||||
content: schema_to_content(schema),
|
||||
required: true
|
||||
|
@ -367,7 +368,7 @@ macro_rules! implOpenapiRouter {
|
|||
|
||||
fn search<Handler, Query, Res>(&mut self)
|
||||
where
|
||||
Query : ResourceType + QueryStringExtractor<Body> + Send + Sync + 'static,
|
||||
Query : ResourceType + DeserializeOwned + QueryStringExtractor<Body> + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceSearch<Query, Res>
|
||||
{
|
||||
|
@ -383,7 +384,7 @@ macro_rules! implOpenapiRouter {
|
|||
|
||||
fn create<Handler, Body, Res>(&mut self)
|
||||
where
|
||||
Body : ResourceType,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceCreate<Body, Res>
|
||||
{
|
||||
|
@ -400,7 +401,7 @@ macro_rules! implOpenapiRouter {
|
|||
|
||||
fn update_all<Handler, Body, Res>(&mut self)
|
||||
where
|
||||
Body : ResourceType,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdateAll<Body, Res>
|
||||
{
|
||||
|
@ -418,7 +419,7 @@ macro_rules! implOpenapiRouter {
|
|||
fn update<Handler, ID, Body, Res>(&mut self)
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Body : ResourceType,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdate<ID, Body, Res>
|
||||
{
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{DrawResourceRoutes, ResourceResult, ResourceType};
|
||||
use crate::{DrawResourceRoutes, RequestBody, ResourceResult, ResourceType};
|
||||
use gotham::{
|
||||
router::response::extender::StaticResponseExtender,
|
||||
state::{State, StateData}
|
||||
|
@ -35,25 +35,25 @@ where
|
|||
/// Handle a GET request on the Resource with additional search parameters.
|
||||
pub trait ResourceSearch<Query : ResourceType, R : ResourceResult>
|
||||
where
|
||||
Query : ResourceType + StateData + StaticResponseExtender
|
||||
Query : ResourceType + DeserializeOwned + StateData + StaticResponseExtender
|
||||
{
|
||||
fn search(state : &mut State, query : Query) -> R;
|
||||
}
|
||||
|
||||
/// Handle a POST request on the Resource root.
|
||||
pub trait ResourceCreate<Body : ResourceType, R : ResourceResult>
|
||||
pub trait ResourceCreate<Body : RequestBody, R : ResourceResult>
|
||||
{
|
||||
fn create(state : &mut State, body : Body) -> R;
|
||||
}
|
||||
|
||||
/// Handle a PUT request on the Resource root.
|
||||
pub trait ResourceUpdateAll<Body : ResourceType, R : ResourceResult>
|
||||
pub trait ResourceUpdateAll<Body : RequestBody, R : ResourceResult>
|
||||
{
|
||||
fn update_all(state : &mut State, body : Body) -> R;
|
||||
}
|
||||
|
||||
/// Handle a PUT request on the Resource with an id.
|
||||
pub trait ResourceUpdate<ID, Body : ResourceType, R : ResourceResult>
|
||||
pub trait ResourceUpdate<ID, Body : RequestBody, R : ResourceResult>
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
||||
{
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{ResourceType, StatusCode};
|
||||
use crate::{ResponseBody, StatusCode};
|
||||
#[cfg(feature = "openapi")]
|
||||
use crate::{OpenapiSchema, OpenapiType};
|
||||
use hyper::Body;
|
||||
|
@ -110,7 +110,7 @@ impl<T : ToString> From<T> for ResourceError
|
|||
}
|
||||
}
|
||||
|
||||
impl<R : ResourceType, E : Error> ResourceResult for Result<R, E>
|
||||
impl<R : ResponseBody, E : Error> ResourceResult for Result<R, E>
|
||||
{
|
||||
fn into_response(self) -> Result<Response, SerdeJsonError>
|
||||
{
|
||||
|
@ -170,7 +170,7 @@ impl<T> From<T> for Success<T>
|
|||
}
|
||||
}
|
||||
|
||||
impl<T : ResourceType> ResourceResult for Success<T>
|
||||
impl<T : ResponseBody> ResourceResult for Success<T>
|
||||
{
|
||||
fn into_response(self) -> Result<Response, SerdeJsonError>
|
||||
{
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use crate::{
|
||||
resource::*,
|
||||
result::{ResourceError, ResourceResult, Response},
|
||||
RequestBody,
|
||||
ResourceType,
|
||||
StatusCode
|
||||
};
|
||||
|
@ -77,26 +78,26 @@ pub trait DrawResourceRoutes
|
|||
|
||||
fn search<Handler, Query, Res>(&mut self)
|
||||
where
|
||||
Query : ResourceType + QueryStringExtractor<Body> + Send + Sync + 'static,
|
||||
Query : ResourceType + DeserializeOwned + QueryStringExtractor<Body> + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceSearch<Query, Res>;
|
||||
|
||||
fn create<Handler, Body, Res>(&mut self)
|
||||
where
|
||||
Body : ResourceType,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceCreate<Body, Res>;
|
||||
|
||||
fn update_all<Handler, Body, Res>(&mut self)
|
||||
where
|
||||
Body : ResourceType,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdateAll<Body, Res>;
|
||||
|
||||
fn update<Handler, ID, Body, Res>(&mut self)
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Body : ResourceType,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdate<ID, Body, Res>;
|
||||
|
||||
|
@ -121,7 +122,7 @@ fn response_from(res : Response, state : &State) -> hyper::Response<Body>
|
|||
}
|
||||
if Method::borrow_from(state) != Method::HEAD
|
||||
{
|
||||
*r.body_mut() = res.body.into();
|
||||
*r.body_mut() = res.body;
|
||||
}
|
||||
r
|
||||
}
|
||||
|
@ -217,7 +218,7 @@ where
|
|||
|
||||
fn create_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
|
||||
where
|
||||
Body : ResourceType,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceCreate<Body, Res>
|
||||
{
|
||||
|
@ -226,7 +227,7 @@ where
|
|||
|
||||
fn update_all_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
|
||||
where
|
||||
Body : ResourceType,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdateAll<Body, Res>
|
||||
{
|
||||
|
@ -236,7 +237,7 @@ where
|
|||
fn update_handler<Handler, ID, Body, Res>(state : State) -> Box<HandlerFuture>
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Body : ResourceType,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdate<ID, Body, Res>
|
||||
{
|
||||
|
@ -372,7 +373,7 @@ macro_rules! implDrawResourceRoutes {
|
|||
|
||||
fn create<Handler, Body, Res>(&mut self)
|
||||
where
|
||||
Body : ResourceType,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceCreate<Body, Res>
|
||||
{
|
||||
|
@ -384,7 +385,7 @@ macro_rules! implDrawResourceRoutes {
|
|||
|
||||
fn update_all<Handler, Body, Res>(&mut self)
|
||||
where
|
||||
Body : ResourceType,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdateAll<Body, Res>
|
||||
{
|
||||
|
@ -397,7 +398,7 @@ macro_rules! implDrawResourceRoutes {
|
|||
fn update<Handler, ID, Body, Res>(&mut self)
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Body : ResourceType,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdate<ID, Body, Res>
|
||||
{
|
||||
|
|
|
@ -3,28 +3,46 @@ use crate::OpenapiType;
|
|||
|
||||
use serde::{de::DeserializeOwned, Serialize};
|
||||
|
||||
/// A type that can be used inside a request or response body. Implemented for every type
|
||||
/// that is serializable with serde, however, it is recommended to use the rest_struct!
|
||||
/// macro to create one.
|
||||
#[cfg(not(feature = "openapi"))]
|
||||
pub trait ResourceType : DeserializeOwned + Serialize
|
||||
pub trait ResourceType
|
||||
{
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "openapi"))]
|
||||
impl<T : DeserializeOwned + Serialize> ResourceType for T
|
||||
{
|
||||
}
|
||||
|
||||
/// A type that can be used inside a request or response body. Implemented for every type
|
||||
/// that is serializable with serde, however, it is recommended to use the rest_struct!
|
||||
/// macro to create one.
|
||||
#[cfg(feature = "openapi")]
|
||||
pub trait ResourceType : OpenapiType + DeserializeOwned + Serialize
|
||||
impl<T> ResourceType for T
|
||||
{
|
||||
}
|
||||
|
||||
#[cfg(feature = "openapi")]
|
||||
impl<T : OpenapiType + DeserializeOwned + Serialize> ResourceType for T
|
||||
pub trait ResourceType : OpenapiType
|
||||
{
|
||||
}
|
||||
|
||||
#[cfg(feature = "openapi")]
|
||||
impl<T : OpenapiType> ResourceType for T
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/// 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
|
||||
/// `OpenapiType`.
|
||||
pub trait ResponseBody : ResourceType + Serialize
|
||||
{
|
||||
}
|
||||
|
||||
impl<T : ResourceType + Serialize> ResponseBody for T
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/// A type that can be used inside a request body. Implemented for every type that is
|
||||
/// deserializable with serde. If the `openapi` feature is used, it must also be of type
|
||||
/// `OpenapiType`.
|
||||
pub trait RequestBody : ResourceType + DeserializeOwned
|
||||
{
|
||||
}
|
||||
|
||||
impl<T : ResourceType + DeserializeOwned> RequestBody for T
|
||||
{
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue