From 57e4f3685279df0b91a0261afbb5b2bf3a942398 Mon Sep 17 00:00:00 2001 From: Dominic Date: Sun, 20 Oct 2019 00:54:27 +0200 Subject: [PATCH] separate RequestBody and ResponseBody traits from ResourceType --- gotham_restful/src/lib.rs | 2 +- gotham_restful/src/openapi/router.rs | 13 ++++---- gotham_restful/src/resource.rs | 10 +++--- gotham_restful/src/result.rs | 6 ++-- gotham_restful/src/routing.rs | 23 +++++++------- gotham_restful/src/types.rs | 46 +++++++++++++++++++--------- 6 files changed, 60 insertions(+), 40 deletions(-) diff --git a/gotham_restful/src/lib.rs b/gotham_restful/src/lib.rs index 9fd913c..727eca9 100644 --- a/gotham_restful/src/lib.rs +++ b/gotham_restful/src/lib.rs @@ -126,4 +126,4 @@ pub use routing::{DrawResources, DrawResourceRoutes}; pub use routing::WithOpenapi; mod types; -pub use types::ResourceType; +pub use types::*; diff --git a/gotham_restful/src/openapi/router.rs b/gotham_restful/src/openapi/router.rs index eaae925..ae32444 100644 --- a/gotham_restful/src/openapi/router.rs +++ b/gotham_restful/src/openapi/router.rs @@ -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(&mut self) where - Query : ResourceType + QueryStringExtractor + Send + Sync + 'static, + Query : ResourceType + DeserializeOwned + QueryStringExtractor + Send + Sync + 'static, Res : ResourceResult, Handler : ResourceSearch { @@ -383,7 +384,7 @@ macro_rules! implOpenapiRouter { fn create(&mut self) where - Body : ResourceType, + Body : RequestBody, Res : ResourceResult, Handler : ResourceCreate { @@ -400,7 +401,7 @@ macro_rules! implOpenapiRouter { fn update_all(&mut self) where - Body : ResourceType, + Body : RequestBody, Res : ResourceResult, Handler : ResourceUpdateAll { @@ -418,7 +419,7 @@ macro_rules! implOpenapiRouter { fn update(&mut self) where ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, - Body : ResourceType, + Body : RequestBody, Res : ResourceResult, Handler : ResourceUpdate { diff --git a/gotham_restful/src/resource.rs b/gotham_restful/src/resource.rs index 8b04159..171f3f9 100644 --- a/gotham_restful/src/resource.rs +++ b/gotham_restful/src/resource.rs @@ -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 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 +pub trait ResourceCreate { fn create(state : &mut State, body : Body) -> R; } /// Handle a PUT request on the Resource root. -pub trait ResourceUpdateAll +pub trait ResourceUpdateAll { fn update_all(state : &mut State, body : Body) -> R; } /// Handle a PUT request on the Resource with an id. -pub trait ResourceUpdate +pub trait ResourceUpdate where ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static { diff --git a/gotham_restful/src/result.rs b/gotham_restful/src/result.rs index 6fb889b..2cdd719 100644 --- a/gotham_restful/src/result.rs +++ b/gotham_restful/src/result.rs @@ -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 From for ResourceError } } -impl ResourceResult for Result +impl ResourceResult for Result { fn into_response(self) -> Result { @@ -170,7 +170,7 @@ impl From for Success } } -impl ResourceResult for Success +impl ResourceResult for Success { fn into_response(self) -> Result { diff --git a/gotham_restful/src/routing.rs b/gotham_restful/src/routing.rs index 437c282..22ed134 100644 --- a/gotham_restful/src/routing.rs +++ b/gotham_restful/src/routing.rs @@ -1,6 +1,7 @@ use crate::{ resource::*, result::{ResourceError, ResourceResult, Response}, + RequestBody, ResourceType, StatusCode }; @@ -77,26 +78,26 @@ pub trait DrawResourceRoutes fn search(&mut self) where - Query : ResourceType + QueryStringExtractor + Send + Sync + 'static, + Query : ResourceType + DeserializeOwned + QueryStringExtractor + Send + Sync + 'static, Res : ResourceResult, Handler : ResourceSearch; fn create(&mut self) where - Body : ResourceType, + Body : RequestBody, Res : ResourceResult, Handler : ResourceCreate; fn update_all(&mut self) where - Body : ResourceType, + Body : RequestBody, Res : ResourceResult, Handler : ResourceUpdateAll; fn update(&mut self) where ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, - Body : ResourceType, + Body : RequestBody, Res : ResourceResult, Handler : ResourceUpdate; @@ -121,7 +122,7 @@ fn response_from(res : Response, state : &State) -> hyper::Response } 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(state : State) -> Box where - Body : ResourceType, + Body : RequestBody, Res : ResourceResult, Handler : ResourceCreate { @@ -226,7 +227,7 @@ where fn update_all_handler(state : State) -> Box where - Body : ResourceType, + Body : RequestBody, Res : ResourceResult, Handler : ResourceUpdateAll { @@ -236,7 +237,7 @@ where fn update_handler(state : State) -> Box where ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, - Body : ResourceType, + Body : RequestBody, Res : ResourceResult, Handler : ResourceUpdate { @@ -372,7 +373,7 @@ macro_rules! implDrawResourceRoutes { fn create(&mut self) where - Body : ResourceType, + Body : RequestBody, Res : ResourceResult, Handler : ResourceCreate { @@ -384,7 +385,7 @@ macro_rules! implDrawResourceRoutes { fn update_all(&mut self) where - Body : ResourceType, + Body : RequestBody, Res : ResourceResult, Handler : ResourceUpdateAll { @@ -397,7 +398,7 @@ macro_rules! implDrawResourceRoutes { fn update(&mut self) where ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static, - Body : ResourceType, + Body : RequestBody, Res : ResourceResult, Handler : ResourceUpdate { diff --git a/gotham_restful/src/types.rs b/gotham_restful/src/types.rs index f354e92..9e34add 100644 --- a/gotham_restful/src/types.rs +++ b/gotham_restful/src/types.rs @@ -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 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 ResourceType for T { } #[cfg(feature = "openapi")] -impl ResourceType for T +pub trait ResourceType : OpenapiType +{ +} + +#[cfg(feature = "openapi")] +impl 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 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 RequestBody for T { }