mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-23 04:52:28 +00:00
update routing to properly extend every DrawRoutes type
This commit is contained in:
parent
4c50ea0959
commit
7486c23727
1 changed files with 191 additions and 146 deletions
|
@ -16,7 +16,10 @@ use gotham::{
|
|||
extractor::QueryStringExtractor,
|
||||
handler::{HandlerFuture, IntoHandlerError},
|
||||
helpers::http::response::{create_empty_response, create_response},
|
||||
pipeline::chain::PipelineHandleChain,
|
||||
pipeline::{
|
||||
chain::PipelineHandleChain,
|
||||
set::PipelineSet
|
||||
},
|
||||
router::{
|
||||
builder::*,
|
||||
non_match::RouteNonMatch,
|
||||
|
@ -24,7 +27,8 @@ use gotham::{
|
|||
content_type::ContentTypeHeaderRouteMatcher,
|
||||
AcceptHeaderRouteMatcher,
|
||||
RouteMatcher
|
||||
}
|
||||
},
|
||||
tree::node::Node
|
||||
},
|
||||
state::{FromState, State}
|
||||
};
|
||||
|
@ -49,7 +53,10 @@ struct PathExtractor<ID : RefUnwindSafe + Send + 'static>
|
|||
/// router into one that will only allow RESTful resources, but record them and generate
|
||||
/// an OpenAPI specification on request.
|
||||
#[cfg(feature = "openapi")]
|
||||
pub trait WithOpenapi<D>
|
||||
pub trait WithOpenapi<C, P>
|
||||
where
|
||||
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||
P : RefUnwindSafe + Send + Sync + 'static
|
||||
{
|
||||
fn with_openapi<F, Title, Version, Url>(&mut self, title : Title, version : Version, server_url : Url, block : F)
|
||||
where
|
||||
|
@ -61,9 +68,12 @@ pub trait WithOpenapi<D>
|
|||
|
||||
/// This trait adds the `resource` method to gotham's routing. It allows you to register
|
||||
/// any RESTful `Resource` with a path.
|
||||
pub trait DrawResources
|
||||
pub trait DrawResources<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);
|
||||
fn resource<R : Resource>(&mut self, path : &str);
|
||||
}
|
||||
|
||||
/// This trait allows to draw routes within an resource. Use this only inside the
|
||||
|
@ -336,14 +346,12 @@ impl From<Option<Vec<Mime>>> for MaybeMatchContentTypeHeader
|
|||
}
|
||||
}
|
||||
|
||||
macro_rules! implDrawResourceRoutes {
|
||||
($implType:ident) => {
|
||||
|
||||
#[cfg(feature = "openapi")]
|
||||
impl<'a, C, P> WithOpenapi<Self> for $implType<'a, C, P>
|
||||
impl<C, P, T> WithOpenapi<C, P> for T
|
||||
where
|
||||
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||
P : RefUnwindSafe + Send + Sync + 'static
|
||||
P : RefUnwindSafe + Send + Sync + 'static,
|
||||
T : DrawRoutes<C, P>
|
||||
{
|
||||
fn with_openapi<F, Title, Version, Url>(&mut self, title : Title, version : Version, server_url : Url, block : F)
|
||||
where
|
||||
|
@ -357,19 +365,61 @@ macro_rules! implDrawResourceRoutes {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, C, P> DrawResources for $implType<'a, C, P>
|
||||
impl<C, P, T> DrawResources<C, P> for T
|
||||
where
|
||||
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||
P : RefUnwindSafe + Send + Sync + 'static,
|
||||
T : DrawRoutes<C, P>
|
||||
{
|
||||
fn resource<R : Resource>(&mut self, path : &str)
|
||||
{
|
||||
R::setup(ResourceSetup::new(self, path));
|
||||
}
|
||||
}
|
||||
|
||||
struct ResourceSetup<'a, C, P>
|
||||
where
|
||||
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||
P : Send + Sync + 'static
|
||||
{
|
||||
node_builder : &'a mut Node,
|
||||
pipeline_chain : C,
|
||||
pipelines : PipelineSet<P>,
|
||||
path : &'a str
|
||||
}
|
||||
|
||||
impl<'a, C, P> ResourceSetup<'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)
|
||||
fn new<D>(router : &'a mut D, path : &'a str) -> Self
|
||||
where
|
||||
D : DrawRoutes<C, P> + 'a
|
||||
{
|
||||
R::setup((self, path.to_string()));
|
||||
let (node_builder, pipeline_chain, pipelines) = router.component_refs();
|
||||
Self {
|
||||
node_builder,
|
||||
pipeline_chain: *pipeline_chain,
|
||||
pipelines: pipelines.clone(),
|
||||
path
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, C, P> DrawRoutes<C, P> for ResourceSetup<'a, C, P>
|
||||
where
|
||||
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||
P : RefUnwindSafe + Send + Sync + 'static
|
||||
{
|
||||
fn component_refs(&mut self) -> (&mut Node, &mut C, &PipelineSet<P>)
|
||||
{
|
||||
(&mut self.node_builder, &mut self.pipeline_chain, &self.pipelines)
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::redundant_closure)] // doesn't work because of type parameters
|
||||
impl<'a, C, P> DrawResourceRoutes for (&mut $implType<'a, C, P>, String)
|
||||
impl<'a, C, P> DrawResourceRoutes for ResourceSetup<'a, C, P>
|
||||
where
|
||||
C : PipelineHandleChain<P> + Copy + Send + Sync + 'static,
|
||||
P : RefUnwindSafe + Send + Sync + 'static
|
||||
|
@ -380,7 +430,7 @@ macro_rules! implDrawResourceRoutes {
|
|||
Handler : ResourceReadAll<Res>
|
||||
{
|
||||
let matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
self.0.get(&self.1)
|
||||
self.get(self.path)
|
||||
.extend_route_matcher(matcher)
|
||||
.to(|state| read_all_handler::<Handler, Res>(state));
|
||||
}
|
||||
|
@ -392,7 +442,7 @@ macro_rules! implDrawResourceRoutes {
|
|||
Handler : ResourceRead<ID, Res>
|
||||
{
|
||||
let matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
self.0.get(&format!("{}/:id", self.1))
|
||||
self.get(&format!("{}/:id", self.path))
|
||||
.extend_route_matcher(matcher)
|
||||
.with_path_extractor::<PathExtractor<ID>>()
|
||||
.to(|state| read_handler::<Handler, ID, Res>(state));
|
||||
|
@ -405,7 +455,7 @@ macro_rules! implDrawResourceRoutes {
|
|||
Handler : ResourceSearch<Query, Res>
|
||||
{
|
||||
let matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
self.0.get(&format!("{}/search", self.1))
|
||||
self.get(&format!("{}/search", self.path))
|
||||
.extend_route_matcher(matcher)
|
||||
.with_query_string_extractor::<Query>()
|
||||
.to(|state| search_handler::<Handler, Query, Res>(state));
|
||||
|
@ -419,7 +469,7 @@ macro_rules! implDrawResourceRoutes {
|
|||
{
|
||||
let accept_matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
let content_matcher : MaybeMatchContentTypeHeader = Body::supported_types().into();
|
||||
self.0.post(&self.1)
|
||||
self.post(self.path)
|
||||
.extend_route_matcher(accept_matcher)
|
||||
.extend_route_matcher(content_matcher)
|
||||
.to(|state| create_handler::<Handler, Body, Res>(state));
|
||||
|
@ -433,7 +483,7 @@ macro_rules! implDrawResourceRoutes {
|
|||
{
|
||||
let accept_matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
let content_matcher : MaybeMatchContentTypeHeader = Body::supported_types().into();
|
||||
self.0.put(&self.1)
|
||||
self.put(self.path)
|
||||
.extend_route_matcher(accept_matcher)
|
||||
.extend_route_matcher(content_matcher)
|
||||
.to(|state| update_all_handler::<Handler, Body, Res>(state));
|
||||
|
@ -448,7 +498,7 @@ macro_rules! implDrawResourceRoutes {
|
|||
{
|
||||
let accept_matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
let content_matcher : MaybeMatchContentTypeHeader = Body::supported_types().into();
|
||||
self.0.put(&format!("{}/:id", self.1))
|
||||
self.put(&format!("{}/:id", self.path))
|
||||
.extend_route_matcher(accept_matcher)
|
||||
.extend_route_matcher(content_matcher)
|
||||
.with_path_extractor::<PathExtractor<ID>>()
|
||||
|
@ -461,7 +511,7 @@ macro_rules! implDrawResourceRoutes {
|
|||
Handler : ResourceDeleteAll<Res>
|
||||
{
|
||||
let matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
self.0.delete(&self.1)
|
||||
DrawRoutes::delete(self, self.path)
|
||||
.extend_route_matcher(matcher)
|
||||
.to(|state| delete_all_handler::<Handler, Res>(state));
|
||||
}
|
||||
|
@ -473,14 +523,9 @@ macro_rules! implDrawResourceRoutes {
|
|||
Handler : ResourceDelete<ID, Res>
|
||||
{
|
||||
let matcher : MaybeMatchAcceptHeader = Res::accepted_types().into();
|
||||
self.0.delete(&format!("{}/:id", self.1))
|
||||
DrawRoutes::delete(self, &format!("{}/:id", self.path))
|
||||
.extend_route_matcher(matcher)
|
||||
.with_path_extractor::<PathExtractor<ID>>()
|
||||
.to(|state| delete_handler::<Handler, ID, Res>(state));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
implDrawResourceRoutes!(RouterBuilder);
|
||||
implDrawResourceRoutes!(ScopeBuilder);
|
||||
|
|
Loading…
Add table
Reference in a new issue