mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-22 20:52:27 +00:00
Less generic type arguments
This commit is contained in:
parent
e0a1505d13
commit
744f56acf9
7 changed files with 200 additions and 272 deletions
|
@ -158,6 +158,7 @@ pub use openapi::{
|
|||
mod resource;
|
||||
pub use resource::{
|
||||
Resource,
|
||||
ResourceMethod,
|
||||
ResourceReadAll,
|
||||
ResourceRead,
|
||||
ResourceSearch,
|
||||
|
|
|
@ -4,19 +4,16 @@ use crate::{
|
|||
routing::*,
|
||||
OpenapiSchema,
|
||||
OpenapiType,
|
||||
RequestBody,
|
||||
ResourceType
|
||||
RequestBody
|
||||
};
|
||||
use futures::future::ok;
|
||||
use gotham::{
|
||||
extractor::QueryStringExtractor,
|
||||
handler::{Handler, HandlerFuture, NewHandler},
|
||||
helpers::http::response::create_response,
|
||||
pipeline::chain::PipelineHandleChain,
|
||||
router::builder::*,
|
||||
state::State
|
||||
};
|
||||
use hyper::Body;
|
||||
use indexmap::IndexMap;
|
||||
use log::error;
|
||||
use mime::{Mime, APPLICATION_JSON, TEXT_PLAIN};
|
||||
|
@ -25,7 +22,6 @@ use openapiv3::{
|
|||
ReferenceOr, ReferenceOr::Item, ReferenceOr::Reference, RequestBody as OARequestBody, Response, Responses, Schema,
|
||||
SchemaKind, SecurityScheme, Server, StatusCode, Type
|
||||
};
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::panic::RefUnwindSafe;
|
||||
|
||||
/**
|
||||
|
@ -381,134 +377,103 @@ macro_rules! implOpenapiRouter {
|
|||
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||
P : RefUnwindSafe + Send + Sync + 'static
|
||||
{
|
||||
fn read_all<Handler, Res>(&mut self)
|
||||
where
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceReadAll<Res>
|
||||
fn read_all<Handler : ResourceReadAll>(&mut self)
|
||||
{
|
||||
let schema = (self.0).1.add_schema::<Res>();
|
||||
let schema = (self.0).1.add_schema::<Handler::Res>();
|
||||
|
||||
let path = format!("/{}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), None, None, Res::requires_auth()));
|
||||
item.get = Some(new_operation(Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::default(), None, None, Handler::Res::requires_auth()));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1).read_all::<Handler, Res>()
|
||||
(&mut *(self.0).0, self.1).read_all::<Handler>()
|
||||
}
|
||||
|
||||
fn read<Handler, ID, Res>(&mut self)
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceRead<ID, Res>
|
||||
fn read<Handler : ResourceRead>(&mut self)
|
||||
{
|
||||
let schema = (self.0).1.add_schema::<Res>();
|
||||
let schema = (self.0).1.add_schema::<Handler::Res>();
|
||||
|
||||
let path = format!("/{}/{{id}}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None, Res::requires_auth()));
|
||||
item.get = Some(new_operation(Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None, Handler::Res::requires_auth()));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1).read::<Handler, ID, Res>()
|
||||
(&mut *(self.0).0, self.1).read::<Handler>()
|
||||
}
|
||||
|
||||
fn search<Handler, Query, Res>(&mut self)
|
||||
where
|
||||
Query : ResourceType + DeserializeOwned + QueryStringExtractor<Body> + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceSearch<Query, Res>
|
||||
fn search<Handler : ResourceSearch>(&mut self)
|
||||
{
|
||||
let schema = (self.0).1.add_schema::<Res>();
|
||||
let schema = (self.0).1.add_schema::<Handler::Res>();
|
||||
|
||||
let path = format!("/{}/search", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&self.1);
|
||||
item.get = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_query_params(Query::schema()), None, None, Res::requires_auth()));
|
||||
item.get = Some(new_operation(Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::from_query_params(Handler::Query::schema()), None, None, Handler::Res::requires_auth()));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1).search::<Handler, Query, Res>()
|
||||
(&mut *(self.0).0, self.1).search::<Handler>()
|
||||
}
|
||||
|
||||
fn create<Handler, Body, Res>(&mut self)
|
||||
where
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceCreate<Body, Res>
|
||||
fn create<Handler : ResourceCreate>(&mut self)
|
||||
{
|
||||
let schema = (self.0).1.add_schema::<Res>();
|
||||
let body_schema = (self.0).1.add_schema::<Body>();
|
||||
let schema = (self.0).1.add_schema::<Handler::Res>();
|
||||
let body_schema = (self.0).1.add_schema::<Handler::Body>();
|
||||
|
||||
let path = format!("/{}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.post = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Body::supported_types(), Res::requires_auth()));
|
||||
item.post = Some(new_operation(Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Handler::Body::supported_types(), Handler::Res::requires_auth()));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1).create::<Handler, Body, Res>()
|
||||
(&mut *(self.0).0, self.1).create::<Handler>()
|
||||
}
|
||||
|
||||
fn update_all<Handler, Body, Res>(&mut self)
|
||||
where
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdateAll<Body, Res>
|
||||
fn update_all<Handler : ResourceUpdateAll>(&mut self)
|
||||
{
|
||||
let schema = (self.0).1.add_schema::<Res>();
|
||||
let body_schema = (self.0).1.add_schema::<Body>();
|
||||
let schema = (self.0).1.add_schema::<Handler::Res>();
|
||||
let body_schema = (self.0).1.add_schema::<Handler::Body>();
|
||||
|
||||
let path = format!("/{}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.put = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Body::supported_types(), Res::requires_auth()));
|
||||
item.put = Some(new_operation(Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::default(), Some(body_schema), Handler::Body::supported_types(), Handler::Res::requires_auth()));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1).update_all::<Handler, Body, Res>()
|
||||
(&mut *(self.0).0, self.1).update_all::<Handler>()
|
||||
}
|
||||
|
||||
fn update<Handler, ID, Body, Res>(&mut self)
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdate<ID, Body, Res>
|
||||
fn update<Handler : ResourceUpdate>(&mut self)
|
||||
{
|
||||
let schema = (self.0).1.add_schema::<Res>();
|
||||
let body_schema = (self.0).1.add_schema::<Body>();
|
||||
let schema = (self.0).1.add_schema::<Handler::Res>();
|
||||
let body_schema = (self.0).1.add_schema::<Handler::Body>();
|
||||
|
||||
let path = format!("/{}/{{id}}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.put = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), Some(body_schema), Body::supported_types(), Res::requires_auth()));
|
||||
item.put = Some(new_operation(Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), Some(body_schema), Handler::Body::supported_types(), Handler::Res::requires_auth()));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1).update::<Handler, ID, Body, Res>()
|
||||
(&mut *(self.0).0, self.1).update::<Handler>()
|
||||
}
|
||||
|
||||
fn delete_all<Handler, Res>(&mut self)
|
||||
where
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceDeleteAll<Res>
|
||||
fn delete_all<Handler : ResourceDeleteAll>(&mut self)
|
||||
{
|
||||
let schema = (self.0).1.add_schema::<Res>();
|
||||
let schema = (self.0).1.add_schema::<Handler::Res>();
|
||||
|
||||
let path = format!("/{}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.delete = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::default(), None, None, Res::requires_auth()));
|
||||
item.delete = Some(new_operation(Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::default(), None, None, Handler::Res::requires_auth()));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1).delete_all::<Handler, Res>()
|
||||
(&mut *(self.0).0, self.1).delete_all::<Handler>()
|
||||
}
|
||||
|
||||
fn delete<Handler, ID, Res>(&mut self)
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceDelete<ID, Res>
|
||||
fn delete<Handler : ResourceDelete>(&mut self)
|
||||
{
|
||||
let schema = (self.0).1.add_schema::<Res>();
|
||||
let schema = (self.0).1.add_schema::<Handler::Res>();
|
||||
|
||||
let path = format!("/{}/{{id}}", &self.1);
|
||||
let mut item = (self.0).1.remove_path(&path);
|
||||
item.delete = Some(new_operation(Res::default_status(), Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None, Res::requires_auth()));
|
||||
item.delete = Some(new_operation(Handler::Res::default_status(), Handler::Res::accepted_types(), schema, OperationParams::from_path_params(vec!["id"]), None, None, Handler::Res::requires_auth()));
|
||||
(self.0).1.add_path(path, item);
|
||||
|
||||
(&mut *(self.0).0, self.1).delete::<Handler, ID, Res>()
|
||||
(&mut *(self.0).0, self.1).delete::<Handler>()
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
use crate::{DrawResourceRoutes, RequestBody, ResourceResult, ResourceType};
|
||||
use gotham::{
|
||||
router::response::extender::StaticResponseExtender,
|
||||
state::{State, StateData}
|
||||
extractor::QueryStringExtractor,
|
||||
state::State
|
||||
};
|
||||
use hyper::Body;
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::panic::RefUnwindSafe;
|
||||
|
||||
|
@ -18,58 +19,68 @@ pub trait Resource
|
|||
fn setup<D : DrawResourceRoutes>(route : D);
|
||||
}
|
||||
|
||||
/// Handle a GET request on the Resource root.
|
||||
pub trait ResourceReadAll<R : ResourceResult>
|
||||
pub trait ResourceMethod
|
||||
{
|
||||
fn read_all(state : &mut State) -> R;
|
||||
type Res : ResourceResult;
|
||||
}
|
||||
|
||||
/// Handle a GET request on the Resource root.
|
||||
pub trait ResourceReadAll : ResourceMethod
|
||||
{
|
||||
fn read_all(state : &mut State) -> Self::Res;
|
||||
}
|
||||
|
||||
/// Handle a GET request on the Resource with an id.
|
||||
pub trait ResourceRead<ID, R : ResourceResult>
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
||||
pub trait ResourceRead : ResourceMethod
|
||||
{
|
||||
fn read(state : &mut State, id : ID) -> R;
|
||||
type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static;
|
||||
|
||||
fn read(state : &mut State, id : Self::ID) -> Self::Res;
|
||||
}
|
||||
|
||||
/// Handle a GET request on the Resource with additional search parameters.
|
||||
pub trait ResourceSearch<Query : ResourceType, R : ResourceResult>
|
||||
where
|
||||
Query : ResourceType + DeserializeOwned + StateData + StaticResponseExtender
|
||||
pub trait ResourceSearch : ResourceMethod
|
||||
{
|
||||
fn search(state : &mut State, query : Query) -> R;
|
||||
type Query : ResourceType + QueryStringExtractor<Body> + Sync;
|
||||
|
||||
fn search(state : &mut State, query : Self::Query) -> Self::Res;
|
||||
}
|
||||
|
||||
/// Handle a POST request on the Resource root.
|
||||
pub trait ResourceCreate<Body : RequestBody, R : ResourceResult>
|
||||
pub trait ResourceCreate : ResourceMethod
|
||||
{
|
||||
fn create(state : &mut State, body : Body) -> R;
|
||||
type Body : RequestBody;
|
||||
|
||||
fn create(state : &mut State, body : Self::Body) -> Self::Res;
|
||||
}
|
||||
|
||||
/// Handle a PUT request on the Resource root.
|
||||
pub trait ResourceUpdateAll<Body : RequestBody, R : ResourceResult>
|
||||
pub trait ResourceUpdateAll : ResourceMethod
|
||||
{
|
||||
fn update_all(state : &mut State, body : Body) -> R;
|
||||
type Body : RequestBody;
|
||||
|
||||
fn update_all(state : &mut State, body : Self::Body) -> Self::Res;
|
||||
}
|
||||
|
||||
/// Handle a PUT request on the Resource with an id.
|
||||
pub trait ResourceUpdate<ID, Body : RequestBody, R : ResourceResult>
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
||||
pub trait ResourceUpdate : ResourceMethod
|
||||
{
|
||||
fn update(state : &mut State, id : ID, body : Body) -> R;
|
||||
type Body : RequestBody;
|
||||
type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static;
|
||||
|
||||
fn update(state : &mut State, id : Self::ID, body : Self::Body) -> Self::Res;
|
||||
}
|
||||
|
||||
/// Handle a DELETE request on the Resource root.
|
||||
pub trait ResourceDeleteAll<R : ResourceResult>
|
||||
pub trait ResourceDeleteAll : ResourceMethod
|
||||
{
|
||||
fn delete_all(state : &mut State) -> R;
|
||||
fn delete_all(state : &mut State) -> Self::Res;
|
||||
}
|
||||
|
||||
/// Handle a DELETE request on the Resource with an id.
|
||||
pub trait ResourceDelete<ID, R : ResourceResult>
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
||||
pub trait ResourceDelete : ResourceMethod
|
||||
{
|
||||
fn delete(state : &mut State, id : ID) -> R;
|
||||
type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static;
|
||||
|
||||
fn delete(state : &mut State, id : Self::ID) -> Self::Res;
|
||||
}
|
||||
|
|
|
@ -171,6 +171,7 @@ This can be returned from a resource when there is no cause of an error. For exa
|
|||
|
||||
```
|
||||
# #[macro_use] extern crate gotham_restful_derive;
|
||||
# mod doc_tests_are_broken {
|
||||
# use gotham::state::State;
|
||||
# use gotham_restful::*;
|
||||
# use serde::{Deserialize, Serialize};
|
||||
|
@ -189,6 +190,7 @@ fn read_all(_state: &mut State) -> Success<MyResponse> {
|
|||
let res = MyResponse { message: "I'm always happy".to_string() };
|
||||
res.into()
|
||||
}
|
||||
# }
|
||||
```
|
||||
*/
|
||||
pub struct Success<T>(T);
|
||||
|
@ -242,6 +244,7 @@ look something like this (assuming the `auth` feature is enabled):
|
|||
|
||||
```
|
||||
# #[macro_use] extern crate gotham_restful_derive;
|
||||
# mod doc_tests_are_broken {
|
||||
# use gotham::state::State;
|
||||
# use gotham_restful::*;
|
||||
# use serde::Deserialize;
|
||||
|
@ -261,6 +264,7 @@ fn read_all(auth : AuthStatus<MyAuthData>) -> AuthResult<NoContent> {
|
|||
// do something
|
||||
NoContent::default().into()
|
||||
}
|
||||
# }
|
||||
```
|
||||
*/
|
||||
pub enum AuthResult<T>
|
||||
|
@ -359,6 +363,7 @@ the function attributes:
|
|||
|
||||
```
|
||||
# #[macro_use] extern crate gotham_restful_derive;
|
||||
# mod doc_tests_are_broken {
|
||||
# use gotham::state::State;
|
||||
# use gotham_restful::*;
|
||||
#
|
||||
|
@ -369,6 +374,7 @@ the function attributes:
|
|||
fn read_all(_state: &mut State) {
|
||||
// do something
|
||||
}
|
||||
# }
|
||||
```
|
||||
*/
|
||||
#[derive(Default)]
|
||||
|
|
|
@ -2,7 +2,6 @@ use crate::{
|
|||
resource::*,
|
||||
result::{ResourceError, ResourceResult, Response},
|
||||
RequestBody,
|
||||
ResourceType,
|
||||
StatusCode
|
||||
};
|
||||
#[cfg(feature = "openapi")]
|
||||
|
@ -13,7 +12,6 @@ use futures::{
|
|||
stream::Stream
|
||||
};
|
||||
use gotham::{
|
||||
extractor::QueryStringExtractor,
|
||||
handler::{HandlerFuture, IntoHandlerError},
|
||||
helpers::http::response::{create_empty_response, create_response},
|
||||
pipeline::chain::PipelineHandleChain,
|
||||
|
@ -35,7 +33,6 @@ use hyper::{
|
|||
Method
|
||||
};
|
||||
use mime::{Mime, APPLICATION_JSON};
|
||||
use serde::de::DeserializeOwned;
|
||||
use std::panic::RefUnwindSafe;
|
||||
|
||||
/// Allow us to extract an id from a path.
|
||||
|
@ -67,52 +64,14 @@ pub trait DrawResources
|
|||
/// `Resource::setup` method.
|
||||
pub trait DrawResourceRoutes
|
||||
{
|
||||
fn read_all<Handler, Res>(&mut self)
|
||||
where
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceReadAll<Res>;
|
||||
|
||||
fn read<Handler, ID, Res>(&mut self)
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceRead<ID, Res>;
|
||||
|
||||
fn search<Handler, Query, Res>(&mut self)
|
||||
where
|
||||
Query : ResourceType + DeserializeOwned + QueryStringExtractor<Body> + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceSearch<Query, Res>;
|
||||
|
||||
fn create<Handler, Body, Res>(&mut self)
|
||||
where
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceCreate<Body, Res>;
|
||||
|
||||
fn update_all<Handler, Body, Res>(&mut self)
|
||||
where
|
||||
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 : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdate<ID, Body, Res>;
|
||||
|
||||
fn delete_all<Handler, Res>(&mut self)
|
||||
where
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceDeleteAll<Res>;
|
||||
|
||||
fn delete<Handler, ID, Res>(&mut self)
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceDelete<ID, Res>;
|
||||
fn read_all<Handler : ResourceReadAll>(&mut self);
|
||||
fn read<Handler : ResourceRead>(&mut self);
|
||||
fn search<Handler : ResourceSearch>(&mut self);
|
||||
fn create<Handler : ResourceCreate>(&mut self);
|
||||
fn update_all<Handler : ResourceUpdateAll>(&mut self);
|
||||
fn update<Handler : ResourceUpdate>(&mut self);
|
||||
fn delete_all<Handler : ResourceDeleteAll>(&mut self);
|
||||
fn delete<Handler : ResourceDelete>(&mut self);
|
||||
}
|
||||
|
||||
fn response_from(res : Response, state : &State) -> hyper::Response<Body>
|
||||
|
@ -195,85 +154,54 @@ where
|
|||
Box::new(f)
|
||||
}
|
||||
|
||||
fn read_all_handler<Handler, Res>(state : State) -> Box<HandlerFuture>
|
||||
where
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceReadAll<Res>
|
||||
fn read_all_handler<Handler : ResourceReadAll>(state : State) -> Box<HandlerFuture>
|
||||
{
|
||||
to_handler_future(state, |state| Handler::read_all(state))
|
||||
}
|
||||
|
||||
fn read_handler<Handler, ID, Res>(state : State) -> Box<HandlerFuture>
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceRead<ID, Res>
|
||||
fn read_handler<Handler : ResourceRead>(state : State) -> Box<HandlerFuture>
|
||||
{
|
||||
let id = {
|
||||
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
|
||||
let path : &PathExtractor<Handler::ID> = PathExtractor::borrow_from(&state);
|
||||
path.id.clone()
|
||||
};
|
||||
to_handler_future(state, |state| Handler::read(state, id))
|
||||
}
|
||||
|
||||
fn search_handler<Handler, Query, Res>(mut state : State) -> Box<HandlerFuture>
|
||||
where
|
||||
Query : ResourceType + QueryStringExtractor<Body> + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceSearch<Query, Res>
|
||||
fn search_handler<Handler : ResourceSearch>(mut state : State) -> Box<HandlerFuture>
|
||||
{
|
||||
let query = Query::take_from(&mut state);
|
||||
let query = Handler::Query::take_from(&mut state);
|
||||
to_handler_future(state, |state| Handler::search(state, query))
|
||||
}
|
||||
|
||||
fn create_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
|
||||
where
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceCreate<Body, Res>
|
||||
fn create_handler<Handler : ResourceCreate>(state : State) -> Box<HandlerFuture>
|
||||
{
|
||||
handle_with_body::<Body, _, _>(state, |state, body| Handler::create(state, body))
|
||||
handle_with_body::<Handler::Body, _, _>(state, |state, body| Handler::create(state, body))
|
||||
}
|
||||
|
||||
fn update_all_handler<Handler, Body, Res>(state : State) -> Box<HandlerFuture>
|
||||
where
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdateAll<Body, Res>
|
||||
fn update_all_handler<Handler : ResourceUpdateAll>(state : State) -> Box<HandlerFuture>
|
||||
{
|
||||
handle_with_body::<Body, _, _>(state, |state, body| Handler::update_all(state, body))
|
||||
handle_with_body::<Handler::Body, _, _>(state, |state, body| Handler::update_all(state, body))
|
||||
}
|
||||
|
||||
fn update_handler<Handler, ID, Body, Res>(state : State) -> Box<HandlerFuture>
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdate<ID, Body, Res>
|
||||
fn update_handler<Handler : ResourceUpdate>(state : State) -> Box<HandlerFuture>
|
||||
{
|
||||
let id = {
|
||||
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
|
||||
let path : &PathExtractor<Handler::ID> = PathExtractor::borrow_from(&state);
|
||||
path.id.clone()
|
||||
};
|
||||
handle_with_body::<Body, _, _>(state, |state, body| Handler::update(state, id, body))
|
||||
handle_with_body::<Handler::Body, _, _>(state, |state, body| Handler::update(state, id, body))
|
||||
}
|
||||
|
||||
fn delete_all_handler<Handler, Res>(state : State) -> Box<HandlerFuture>
|
||||
where
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceDeleteAll<Res>
|
||||
fn delete_all_handler<Handler : ResourceDeleteAll>(state : State) -> Box<HandlerFuture>
|
||||
{
|
||||
to_handler_future(state, |state| Handler::delete_all(state))
|
||||
}
|
||||
|
||||
fn delete_handler<Handler, ID, Res>(state : State) -> Box<HandlerFuture>
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceDelete<ID, Res>
|
||||
fn delete_handler<Handler : ResourceDelete>(state : State) -> Box<HandlerFuture>
|
||||
{
|
||||
let id = {
|
||||
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
|
||||
let path : &PathExtractor<Handler::ID> = PathExtractor::borrow_from(&state);
|
||||
path.id.clone()
|
||||
};
|
||||
to_handler_future(state, |state| Handler::delete(state, id))
|
||||
|
@ -368,109 +296,78 @@ macro_rules! implDrawResourceRoutes {
|
|||
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||
P : RefUnwindSafe + Send + Sync + 'static
|
||||
{
|
||||
fn read_all<Handler, Res>(&mut self)
|
||||
where
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceReadAll<Res>
|
||||
fn read_all<Handler : ResourceReadAll>(&mut self)
|
||||
{
|
||||
let matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
||||
self.0.get(&self.1)
|
||||
.extend_route_matcher(matcher)
|
||||
.to(|state| read_all_handler::<Handler, Res>(state));
|
||||
.to(|state| read_all_handler::<Handler>(state));
|
||||
}
|
||||
|
||||
fn read<Handler, ID, Res>(&mut self)
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceRead<ID, Res>
|
||||
fn read<Handler : ResourceRead>(&mut self)
|
||||
{
|
||||
let matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
||||
self.0.get(&format!("{}/:id", self.1))
|
||||
.extend_route_matcher(matcher)
|
||||
.with_path_extractor::<PathExtractor<ID>>()
|
||||
.to(|state| read_handler::<Handler, ID, Res>(state));
|
||||
.with_path_extractor::<PathExtractor<Handler::ID>>()
|
||||
.to(|state| read_handler::<Handler>(state));
|
||||
}
|
||||
|
||||
fn search<Handler, Query, Res>(&mut self)
|
||||
where
|
||||
Query : ResourceType + QueryStringExtractor<Body> + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceSearch<Query, Res>
|
||||
fn search<Handler : ResourceSearch>(&mut self)
|
||||
{
|
||||
let matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
||||
self.0.get(&format!("{}/search", self.1))
|
||||
.extend_route_matcher(matcher)
|
||||
.with_query_string_extractor::<Query>()
|
||||
.to(|state| search_handler::<Handler, Query, Res>(state));
|
||||
.with_query_string_extractor::<Handler::Query>()
|
||||
.to(|state| search_handler::<Handler>(state));
|
||||
}
|
||||
|
||||
fn create<Handler, Body, Res>(&mut self)
|
||||
where
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceCreate<Body, Res>
|
||||
fn create<Handler : ResourceCreate>(&mut self)
|
||||
{
|
||||
let accept_matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
let content_matcher : MaybeMatchContentTypeHeader = Body::supported_types().into();
|
||||
let accept_matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
||||
let content_matcher : MaybeMatchContentTypeHeader = Handler::Body::supported_types().into();
|
||||
self.0.post(&self.1)
|
||||
.extend_route_matcher(accept_matcher)
|
||||
.extend_route_matcher(content_matcher)
|
||||
.to(|state| create_handler::<Handler, Body, Res>(state));
|
||||
.to(|state| create_handler::<Handler>(state));
|
||||
}
|
||||
|
||||
fn update_all<Handler, Body, Res>(&mut self)
|
||||
where
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdateAll<Body, Res>
|
||||
fn update_all<Handler : ResourceUpdateAll>(&mut self)
|
||||
{
|
||||
let accept_matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
let content_matcher : MaybeMatchContentTypeHeader = Body::supported_types().into();
|
||||
let accept_matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
||||
let content_matcher : MaybeMatchContentTypeHeader = Handler::Body::supported_types().into();
|
||||
self.0.put(&self.1)
|
||||
.extend_route_matcher(accept_matcher)
|
||||
.extend_route_matcher(content_matcher)
|
||||
.to(|state| update_all_handler::<Handler, Body, Res>(state));
|
||||
.to(|state| update_all_handler::<Handler>(state));
|
||||
}
|
||||
|
||||
fn update<Handler, ID, Body, Res>(&mut self)
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Body : RequestBody,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceUpdate<ID, Body, Res>
|
||||
fn update<Handler : ResourceUpdate>(&mut self)
|
||||
{
|
||||
let accept_matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
let content_matcher : MaybeMatchContentTypeHeader = Body::supported_types().into();
|
||||
let accept_matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
||||
let content_matcher : MaybeMatchContentTypeHeader = Handler::Body::supported_types().into();
|
||||
self.0.put(&format!("{}/:id", self.1))
|
||||
.extend_route_matcher(accept_matcher)
|
||||
.extend_route_matcher(content_matcher)
|
||||
.with_path_extractor::<PathExtractor<ID>>()
|
||||
.to(|state| update_handler::<Handler, ID, Body, Res>(state));
|
||||
.with_path_extractor::<PathExtractor<Handler::ID>>()
|
||||
.to(|state| update_handler::<Handler>(state));
|
||||
}
|
||||
|
||||
fn delete_all<Handler, Res>(&mut self)
|
||||
where
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceDeleteAll<Res>
|
||||
fn delete_all<Handler : ResourceDeleteAll>(&mut self)
|
||||
{
|
||||
let matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
||||
self.0.delete(&self.1)
|
||||
.extend_route_matcher(matcher)
|
||||
.to(|state| delete_all_handler::<Handler, Res>(state));
|
||||
.to(|state| delete_all_handler::<Handler>(state));
|
||||
}
|
||||
|
||||
fn delete<Handler, ID, Res>(&mut self)
|
||||
where
|
||||
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
||||
Res : ResourceResult,
|
||||
Handler : ResourceDelete<ID, Res>
|
||||
fn delete<Handler : ResourceDelete>(&mut self)
|
||||
{
|
||||
let matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
let matcher : MaybeMatchAcceptHeader = Handler::Res::accepted_types().into();
|
||||
self.0.delete(&format!("{}/:id", self.1))
|
||||
.extend_route_matcher(matcher)
|
||||
.with_path_extractor::<PathExtractor<ID>>()
|
||||
.to(|state| delete_handler::<Handler, ID, Res>(state));
|
||||
.with_path_extractor::<PathExtractor<Handler::ID>>()
|
||||
.to(|state| delete_handler::<Handler>(state));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
use heck::SnakeCase;
|
||||
use heck::{CamelCase, SnakeCase};
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::{Ident, TokenStream as TokenStream2};
|
||||
use quote::{format_ident, quote};
|
||||
|
@ -46,6 +46,22 @@ impl FromStr for Method
|
|||
|
||||
impl Method
|
||||
{
|
||||
pub fn type_names(&self) -> Vec<&'static str>
|
||||
{
|
||||
use Method::*;
|
||||
|
||||
match self {
|
||||
ReadAll => vec![],
|
||||
Read => vec!["ID"],
|
||||
Search => vec!["Query"],
|
||||
Create => vec!["Body"],
|
||||
UpdateAll => vec!["Body"],
|
||||
Update => vec!["ID", "Body"],
|
||||
DeleteAll => vec![],
|
||||
Delete => vec!["ID"]
|
||||
}
|
||||
}
|
||||
|
||||
pub fn trait_ident(&self) -> Ident
|
||||
{
|
||||
use Method::*;
|
||||
|
@ -80,6 +96,16 @@ impl Method
|
|||
format_ident!("{}", name)
|
||||
}
|
||||
|
||||
pub fn mod_ident(&self, resource : String) -> Ident
|
||||
{
|
||||
format_ident!("_gotham_restful_resource_{}_method_{}", resource.to_snake_case(), self.fn_ident())
|
||||
}
|
||||
|
||||
pub fn handler_struct_ident(&self, resource : String) -> Ident
|
||||
{
|
||||
format_ident!("{}{}Handler", resource.to_camel_case(), self.trait_ident())
|
||||
}
|
||||
|
||||
pub fn setup_ident(&self, resource : String) -> Ident
|
||||
{
|
||||
format_ident!("{}_{}_setup_impl", resource.to_snake_case(), self.fn_ident())
|
||||
|
@ -194,6 +220,8 @@ pub fn expand_method(method : Method, attrs : TokenStream, item : TokenStream) -
|
|||
|
||||
let trait_ident = method.trait_ident();
|
||||
let method_ident = method.fn_ident();
|
||||
let mod_ident = method.mod_ident(resource_ident.to_string());
|
||||
let handler_ident = method.handler_struct_ident(resource_ident.to_string());
|
||||
let setup_ident = method.setup_ident(resource_ident.to_string());
|
||||
|
||||
let (ret, is_no_content) = match &fun.sig.output {
|
||||
|
@ -214,11 +242,15 @@ pub fn expand_method(method : Method, attrs : TokenStream, item : TokenStream) -
|
|||
}).collect();
|
||||
|
||||
// extract the generic parameters to use
|
||||
let mut generics : Vec<TokenStream2> = args.iter()
|
||||
let generics : Vec<TokenStream2> = args.iter()
|
||||
.filter(|arg| (*arg).ty.is_method_arg())
|
||||
.map(|arg| arg.ty.quote_ty().unwrap())
|
||||
.zip(method.type_names())
|
||||
.map(|(arg, name)| {
|
||||
let ident = format_ident!("{}", name);
|
||||
quote!(type #ident = #arg;)
|
||||
})
|
||||
.collect();
|
||||
generics.push(quote!(#ret));
|
||||
|
||||
// extract the definition of our method
|
||||
let mut args_def : Vec<TokenStream2> = args.iter()
|
||||
|
@ -277,22 +309,37 @@ pub fn expand_method(method : Method, attrs : TokenStream, item : TokenStream) -
|
|||
let output = quote! {
|
||||
#fun
|
||||
|
||||
impl #krate::#trait_ident<#(#generics),*> for #resource_ident
|
||||
where #where_clause
|
||||
#fun_vis mod #mod_ident
|
||||
{
|
||||
fn #method_ident(#(#args_def),*) -> #ret
|
||||
use super::*;
|
||||
|
||||
struct #handler_ident;
|
||||
|
||||
impl #krate::ResourceMethod for #handler_ident
|
||||
{
|
||||
#[allow(unused_imports)]
|
||||
use #krate::export::{Future, FromState};
|
||||
|
||||
#block
|
||||
type Res = #ret;
|
||||
}
|
||||
}
|
||||
|
||||
#[deny(dead_code)]
|
||||
#fun_vis fn #setup_ident<D : #krate::DrawResourceRoutes>(route : &mut D)
|
||||
{
|
||||
route.#method_ident::<#resource_ident, #(#generics),*>();
|
||||
|
||||
impl #krate::#trait_ident for #handler_ident
|
||||
where #where_clause
|
||||
{
|
||||
#(#generics)*
|
||||
|
||||
fn #method_ident(#(#args_def),*) -> #ret
|
||||
{
|
||||
#[allow(unused_imports)]
|
||||
use #krate::export::{Future, FromState};
|
||||
|
||||
#block
|
||||
}
|
||||
}
|
||||
|
||||
#[deny(dead_code)]
|
||||
pub fn #setup_ident<D : #krate::DrawResourceRoutes>(route : &mut D)
|
||||
{
|
||||
route.#method_ident::<#handler_ident>();
|
||||
}
|
||||
|
||||
}
|
||||
};
|
||||
output.into()
|
||||
|
|
|
@ -39,8 +39,9 @@ pub fn expand_resource(tokens : TokenStream) -> TokenStream
|
|||
m.0.into_iter()
|
||||
}).map(|method| {
|
||||
let method = Method::from_str(&method.to_string()).expect("unknown method");
|
||||
let mod_ident = method.mod_ident(ident.to_string());
|
||||
let ident = method.setup_ident(ident.to_string());
|
||||
quote!(#ident(&mut route);)
|
||||
quote!(#mod_ident::#ident(&mut route);)
|
||||
}).collect();
|
||||
|
||||
let output = quote! {
|
||||
|
|
Loading…
Add table
Reference in a new issue