mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-23 13:02:28 +00:00
asyncify method proc macro
This commit is contained in:
parent
427c836f52
commit
89f6494b51
3 changed files with 23 additions and 25 deletions
|
@ -42,7 +42,7 @@ pub trait ResourceMethod
|
||||||
/// Handle a GET request on the Resource root.
|
/// Handle a GET request on the Resource root.
|
||||||
pub trait ResourceReadAll : ResourceMethod
|
pub trait ResourceReadAll : ResourceMethod
|
||||||
{
|
{
|
||||||
fn read_all(state : &mut State) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
|
fn read_all(state : State) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a GET request on the Resource with an id.
|
/// Handle a GET request on the Resource with an id.
|
||||||
|
@ -50,7 +50,7 @@ pub trait ResourceRead : ResourceMethod
|
||||||
{
|
{
|
||||||
type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static;
|
type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static;
|
||||||
|
|
||||||
fn read(state : &mut State, id : Self::ID) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
|
fn read(state : State, id : Self::ID) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a GET request on the Resource with additional search parameters.
|
/// Handle a GET request on the Resource with additional search parameters.
|
||||||
|
@ -58,7 +58,7 @@ pub trait ResourceSearch : ResourceMethod
|
||||||
{
|
{
|
||||||
type Query : ResourceType + QueryStringExtractor<Body> + Sync;
|
type Query : ResourceType + QueryStringExtractor<Body> + Sync;
|
||||||
|
|
||||||
fn search(state : &mut State, query : Self::Query) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
|
fn search(state : State, query : Self::Query) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a POST request on the Resource root.
|
/// Handle a POST request on the Resource root.
|
||||||
|
@ -66,7 +66,7 @@ pub trait ResourceCreate : ResourceMethod
|
||||||
{
|
{
|
||||||
type Body : RequestBody;
|
type Body : RequestBody;
|
||||||
|
|
||||||
fn create(state : &mut State, body : Self::Body) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
|
fn create(state : State, body : Self::Body) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a PUT request on the Resource root.
|
/// Handle a PUT request on the Resource root.
|
||||||
|
@ -74,7 +74,7 @@ pub trait ResourceUpdateAll : ResourceMethod
|
||||||
{
|
{
|
||||||
type Body : RequestBody;
|
type Body : RequestBody;
|
||||||
|
|
||||||
fn update_all(state : &mut State, body : Self::Body) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
|
fn update_all(state : State, body : Self::Body) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a PUT request on the Resource with an id.
|
/// Handle a PUT request on the Resource with an id.
|
||||||
|
@ -83,13 +83,13 @@ pub trait ResourceUpdate : ResourceMethod
|
||||||
type Body : RequestBody;
|
type Body : RequestBody;
|
||||||
type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static;
|
type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static;
|
||||||
|
|
||||||
fn update(state : &mut State, id : Self::ID, body : Self::Body) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
|
fn update(state : State, id : Self::ID, body : Self::Body) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a DELETE request on the Resource root.
|
/// Handle a DELETE request on the Resource root.
|
||||||
pub trait ResourceDeleteAll : ResourceMethod
|
pub trait ResourceDeleteAll : ResourceMethod
|
||||||
{
|
{
|
||||||
fn delete_all(state : &mut State) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
|
fn delete_all(state : State) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Handle a DELETE request on the Resource with an id.
|
/// Handle a DELETE request on the Resource with an id.
|
||||||
|
@ -97,5 +97,5 @@ pub trait ResourceDelete : ResourceMethod
|
||||||
{
|
{
|
||||||
type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static;
|
type ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static;
|
||||||
|
|
||||||
fn delete(state : &mut State, id : Self::ID) -> Pin<Box<dyn Future<Output = Self::Res> + Send>>;
|
fn delete(state : State, id : Self::ID) -> Pin<Box<dyn Future<Output = (State, Self::Res)> + Send>>;
|
||||||
}
|
}
|
||||||
|
|
|
@ -106,12 +106,12 @@ fn response_from(res : Response, state : &State) -> hyper::Response<Body>
|
||||||
r
|
r
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn to_handler_future<F, R>(mut state : State, get_result : F) -> Result<(State, gotham::hyper::Response<Body>), (State, HandlerError)>
|
async fn to_handler_future<F, R>(state : State, get_result : F) -> Result<(State, gotham::hyper::Response<Body>), (State, HandlerError)>
|
||||||
where
|
where
|
||||||
F : FnOnce(&mut State) -> Pin<Box<dyn Future<Output = R> + Send>>,
|
F : FnOnce(State) -> Pin<Box<dyn Future<Output = (State, R)> + Send>>,
|
||||||
R : ResourceResult
|
R : ResourceResult
|
||||||
{
|
{
|
||||||
let res = get_result(&mut state).await;
|
let (state, res) = get_result(state).await;
|
||||||
let res = res.into_response().await;
|
let res = res.into_response().await;
|
||||||
match res {
|
match res {
|
||||||
Ok(res) => {
|
Ok(res) => {
|
||||||
|
@ -125,7 +125,7 @@ where
|
||||||
async fn body_to_res<B, F, R>(mut state : State, get_result : F) -> (State, Result<gotham::hyper::Response<Body>, HandlerError>)
|
async fn body_to_res<B, F, R>(mut state : State, get_result : F) -> (State, Result<gotham::hyper::Response<Body>, HandlerError>)
|
||||||
where
|
where
|
||||||
B : RequestBody,
|
B : RequestBody,
|
||||||
F : FnOnce(&mut State, B) -> Pin<Box<dyn Future<Output = R> + Send>>,
|
F : FnOnce(State, B) -> Pin<Box<dyn Future<Output = (State, R)> + Send>>,
|
||||||
R : ResourceResult
|
R : ResourceResult
|
||||||
{
|
{
|
||||||
let body = to_bytes(Body::take_from(&mut state)).await;
|
let body = to_bytes(Body::take_from(&mut state)).await;
|
||||||
|
@ -158,10 +158,10 @@ where
|
||||||
return (state, res)
|
return (state, res)
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
get_result(&mut state, body)
|
get_result(state, body)
|
||||||
};
|
};
|
||||||
|
|
||||||
let res = res.await;
|
let (state, res) = res.await;
|
||||||
let res = res.into_response().await;
|
let res = res.into_response().await;
|
||||||
|
|
||||||
let res = match res {
|
let res = match res {
|
||||||
|
@ -177,7 +177,7 @@ where
|
||||||
fn handle_with_body<B, F, R>(state : State, get_result : F) -> Pin<Box<HandlerFuture>>
|
fn handle_with_body<B, F, R>(state : State, get_result : F) -> Pin<Box<HandlerFuture>>
|
||||||
where
|
where
|
||||||
B : RequestBody + 'static,
|
B : RequestBody + 'static,
|
||||||
F : FnOnce(&mut State, B) -> Pin<Box<dyn Future<Output = R> + Send>> + Send + 'static,
|
F : FnOnce(State, B) -> Pin<Box<dyn Future<Output = (State, R)> + Send>> + Send + 'static,
|
||||||
R : ResourceResult + Send + 'static
|
R : ResourceResult + Send + 'static
|
||||||
{
|
{
|
||||||
body_to_res(state, get_result)
|
body_to_res(state, get_result)
|
||||||
|
|
|
@ -325,6 +325,7 @@ fn expand(method : Method, attrs : TokenStream, item : TokenStream) -> Result<To
|
||||||
let repo_ident = format_ident!("repo");
|
let repo_ident = format_ident!("repo");
|
||||||
let conn_ident = format_ident!("conn");
|
let conn_ident = format_ident!("conn");
|
||||||
let auth_ident = format_ident!("auth");
|
let auth_ident = format_ident!("auth");
|
||||||
|
let res_ident = format_ident!("res");
|
||||||
|
|
||||||
// extract arguments into pattern, ident and type
|
// extract arguments into pattern, ident and type
|
||||||
let args = fun.sig.inputs.iter()
|
let args = fun.sig.inputs.iter()
|
||||||
|
@ -366,12 +367,12 @@ fn expand(method : Method, attrs : TokenStream, item : TokenStream) -> Result<To
|
||||||
let ty = arg.ty.quote_ty();
|
let ty = arg.ty.quote_ty();
|
||||||
quote!(#ident : #ty)
|
quote!(#ident : #ty)
|
||||||
}).collect();
|
}).collect();
|
||||||
args_def.insert(0, quote!(#state_ident : &mut #krate::export::State));
|
args_def.insert(0, quote!(mut #state_ident : #krate::export::State));
|
||||||
|
|
||||||
// extract the arguments to pass over to the supplied method
|
// extract the arguments to pass over to the supplied method
|
||||||
let args_pass : Vec<TokenStream2> = args.iter().map(|arg| match (&arg.ty, &arg.ident) {
|
let args_pass : Vec<TokenStream2> = args.iter().map(|arg| match (&arg.ty, &arg.ident) {
|
||||||
(MethodArgumentType::StateRef, _) => quote!(#state_ident),
|
(MethodArgumentType::StateRef, _) => quote!(&#state_ident),
|
||||||
(MethodArgumentType::StateMutRef, _) => quote!(#state_ident),
|
(MethodArgumentType::StateMutRef, _) => quote!(&mut #state_ident),
|
||||||
(MethodArgumentType::MethodArg(_), ident) => quote!(#ident),
|
(MethodArgumentType::MethodArg(_), ident) => quote!(#ident),
|
||||||
(MethodArgumentType::DatabaseConnection(_), _) => quote!(&#conn_ident),
|
(MethodArgumentType::DatabaseConnection(_), _) => quote!(&#conn_ident),
|
||||||
(MethodArgumentType::AuthStatus(_), _) => quote!(#auth_ident),
|
(MethodArgumentType::AuthStatus(_), _) => quote!(#auth_ident),
|
||||||
|
@ -407,7 +408,7 @@ fn expand(method : Method, attrs : TokenStream, item : TokenStream) -> Result<To
|
||||||
let auth_ty = arg.ty.quote_ty();
|
let auth_ty = arg.ty.quote_ty();
|
||||||
state_block = quote! {
|
state_block = quote! {
|
||||||
#state_block
|
#state_block
|
||||||
let #auth_ident : #auth_ty = <#auth_ty>::borrow_from(#state_ident).clone();
|
let #auth_ident : #auth_ty = <#auth_ty>::borrow_from(&#state_ident).clone();
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -430,10 +431,6 @@ fn expand(method : Method, attrs : TokenStream, item : TokenStream) -> Result<To
|
||||||
#fun_vis mod #mod_ident
|
#fun_vis mod #mod_ident
|
||||||
{
|
{
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::{
|
|
||||||
future::Future,
|
|
||||||
pin::Pin
|
|
||||||
};
|
|
||||||
|
|
||||||
struct #handler_ident;
|
struct #handler_ident;
|
||||||
|
|
||||||
|
@ -450,7 +447,7 @@ fn expand(method : Method, attrs : TokenStream, item : TokenStream) -> Result<To
|
||||||
{
|
{
|
||||||
#(#generics)*
|
#(#generics)*
|
||||||
|
|
||||||
fn #method_ident(#(#args_def),*) -> Pin<Box<dyn Future<Output = #ret> + Send>>
|
fn #method_ident(#(#args_def),*) -> std::pin::Pin<Box<dyn std::future::Future<Output = (#krate::export::State, #ret)> + Send>>
|
||||||
{
|
{
|
||||||
#[allow(unused_imports)]
|
#[allow(unused_imports)]
|
||||||
use #krate::export::{FromState, FutureExt};
|
use #krate::export::{FromState, FutureExt};
|
||||||
|
@ -458,7 +455,8 @@ fn expand(method : Method, attrs : TokenStream, item : TokenStream) -> Result<To
|
||||||
#state_block
|
#state_block
|
||||||
|
|
||||||
async move {
|
async move {
|
||||||
#block
|
let #res_ident = { #block };
|
||||||
|
(#state_ident, #res_ident)
|
||||||
}.boxed()
|
}.boxed()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue