2019-09-27 16:36:38 +02:00
|
|
|
use crate::{GetResource, IndexResource, Resource, ResourceResult};
|
2019-09-26 17:24:40 +02:00
|
|
|
use futures::future::{err, ok};
|
|
|
|
use gotham::{
|
|
|
|
handler::{HandlerFuture, IntoHandlerError},
|
|
|
|
helpers::http::response::create_response,
|
|
|
|
pipeline::chain::PipelineHandleChain,
|
|
|
|
router::builder::*,
|
2019-09-27 16:36:38 +02:00
|
|
|
state::{FromState, State}
|
2019-09-26 17:24:40 +02:00
|
|
|
};
|
|
|
|
use mime::APPLICATION_JSON;
|
2019-09-27 16:36:38 +02:00
|
|
|
use serde::de::DeserializeOwned;
|
2019-09-26 17:24:40 +02:00
|
|
|
use std::panic::RefUnwindSafe;
|
|
|
|
|
2019-09-27 16:36:38 +02:00
|
|
|
/// Allow us to extract an id from a path.
|
|
|
|
#[derive(Deserialize, StateData, StaticResponseExtender)]
|
|
|
|
struct PathExtractor<ID : RefUnwindSafe + Send + 'static>
|
|
|
|
{
|
|
|
|
id : ID
|
|
|
|
}
|
|
|
|
|
2019-09-26 17:42:28 +02:00
|
|
|
/// This trait adds the `resource` method to gotham's routing. It allows you to register
|
|
|
|
/// any RESTful `Resource` with a path.
|
|
|
|
pub trait DrawResources
|
|
|
|
{
|
|
|
|
fn resource<R : Resource, T : ToString>(&mut self, path : T);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This trait allows to draw routes within an resource. Use this only inside the
|
|
|
|
/// `Resource::setup` method.
|
2019-09-26 17:24:40 +02:00
|
|
|
pub trait DrawResourceRoutes
|
|
|
|
{
|
2019-09-27 16:36:38 +02:00
|
|
|
fn index<R, IR>(&mut self)
|
|
|
|
where
|
|
|
|
R : ResourceResult,
|
|
|
|
IR : IndexResource<R>;
|
|
|
|
|
|
|
|
fn get<ID, R, IR>(&mut self)
|
|
|
|
where
|
|
|
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
|
|
|
R : ResourceResult,
|
|
|
|
IR : GetResource<ID, R>;
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 15:35:02 +02:00
|
|
|
fn to_handler_future<F, R>(mut state : State, get_result : F) -> Box<HandlerFuture>
|
2019-09-26 17:24:40 +02:00
|
|
|
where
|
2019-09-27 15:35:02 +02:00
|
|
|
F : FnOnce(&mut State) -> R,
|
|
|
|
R : ResourceResult
|
2019-09-26 17:24:40 +02:00
|
|
|
{
|
2019-09-27 15:35:02 +02:00
|
|
|
let res = get_result(&mut state).to_json();
|
|
|
|
match res {
|
|
|
|
Ok((status, body)) => {
|
2019-09-26 17:24:40 +02:00
|
|
|
let res = create_response(&state, status, APPLICATION_JSON, body);
|
|
|
|
Box::new(ok((state, res)))
|
|
|
|
},
|
|
|
|
Err(e) => Box::new(err((state, e.into_handler_error())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 15:35:02 +02:00
|
|
|
fn index_handler<R : ResourceResult, IR : IndexResource<R>>(state : State) -> Box<HandlerFuture>
|
2019-09-26 17:24:40 +02:00
|
|
|
{
|
2019-09-27 15:35:02 +02:00
|
|
|
to_handler_future(state, |state| IR::index(state))
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
|
2019-09-27 16:36:38 +02:00
|
|
|
fn get_handler<ID, R : ResourceResult, GR : GetResource<ID, R>>(state : State) -> Box<HandlerFuture>
|
|
|
|
where
|
|
|
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static
|
|
|
|
{
|
|
|
|
let id = {
|
|
|
|
let path : &PathExtractor<ID> = PathExtractor::borrow_from(&state);
|
|
|
|
path.id.clone()
|
|
|
|
};
|
|
|
|
to_handler_future(state, |state| GR::get(state, id))
|
|
|
|
}
|
|
|
|
|
2019-09-26 17:24:40 +02:00
|
|
|
macro_rules! implDrawResourceRoutes {
|
|
|
|
($implType:ident) => {
|
2019-09-26 17:42:28 +02:00
|
|
|
impl<'a, C, P> DrawResources for $implType<'a, C, P>
|
|
|
|
where
|
|
|
|
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
|
|
|
P : RefUnwindSafe + Send + Sync + 'static
|
|
|
|
{
|
|
|
|
fn resource<R : Resource, T : ToString>(&mut self, path : T)
|
|
|
|
{
|
2019-09-27 15:35:02 +02:00
|
|
|
R::setup((self, path.to_string()));
|
2019-09-26 17:42:28 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-27 15:35:02 +02:00
|
|
|
impl<'a, C, P> DrawResourceRoutes for (&mut $implType<'a, C, P>, String)
|
2019-09-26 17:24:40 +02:00
|
|
|
where
|
|
|
|
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
|
|
|
P : RefUnwindSafe + Send + Sync + 'static
|
|
|
|
{
|
2019-09-27 16:36:38 +02:00
|
|
|
fn index<R, IR>(&mut self)
|
|
|
|
where
|
|
|
|
R : ResourceResult,
|
|
|
|
IR : IndexResource<R>
|
|
|
|
{
|
|
|
|
self.0.get(&self.1)
|
|
|
|
.to(|state| index_handler::<R, IR>(state));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn get<ID, R, IR>(&mut self)
|
|
|
|
where
|
|
|
|
ID : DeserializeOwned + Clone + RefUnwindSafe + Send + Sync + 'static,
|
|
|
|
R : ResourceResult,
|
|
|
|
IR : GetResource<ID, R>
|
2019-09-26 17:24:40 +02:00
|
|
|
{
|
2019-09-27 16:36:38 +02:00
|
|
|
self.0.get(&format!("{}/:id", self.1))
|
|
|
|
.with_path_extractor::<PathExtractor<ID>>()
|
|
|
|
.to(|state| get_handler::<ID, R, IR>(state));
|
2019-09-26 17:24:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
implDrawResourceRoutes!(RouterBuilder);
|
|
|
|
implDrawResourceRoutes!(ScopeBuilder);
|