From dd9e10a154d2ad250a4a44a47e8ccb4447a590c9 Mon Sep 17 00:00:00 2001 From: Dominic Date: Sun, 20 Oct 2019 15:53:30 +0200 Subject: [PATCH] match content type --- gotham_restful/src/routing.rs | 51 ++++++++++++++++++++++++++++++----- 1 file changed, 44 insertions(+), 7 deletions(-) diff --git a/gotham_restful/src/routing.rs b/gotham_restful/src/routing.rs index d587e38..5a16281 100644 --- a/gotham_restful/src/routing.rs +++ b/gotham_restful/src/routing.rs @@ -20,7 +20,11 @@ use gotham::{ router::{ builder::*, non_match::RouteNonMatch, - route::matcher::{AcceptHeaderRouteMatcher, RouteMatcher} + route::matcher::{ + content_type::ContentTypeHeaderRouteMatcher, + AcceptHeaderRouteMatcher, + RouteMatcher + } }, state::{FromState, State} }; @@ -305,6 +309,33 @@ impl From>> for MaybeMatchAcceptHeader } } +#[derive(Clone)] +struct MaybeMatchContentTypeHeader +{ + matcher : Option +} + +impl RouteMatcher for MaybeMatchContentTypeHeader +{ + fn is_match(&self, state : &State) -> Result<(), RouteNonMatch> + { + match &self.matcher { + Some(matcher) => matcher.is_match(state), + None => Ok(()) + } + } +} + +impl From>> for MaybeMatchContentTypeHeader +{ + fn from(types : Option>) -> Self + { + Self { + matcher: types.map(ContentTypeHeaderRouteMatcher::new) + } + } +} + macro_rules! implDrawResourceRoutes { ($implType:ident) => { @@ -386,9 +417,11 @@ macro_rules! implDrawResourceRoutes { Res : ResourceResult, Handler : ResourceCreate { - let matcher : MaybeMatchAcceptHeader = Res::accepted_types().into(); + let accept_matcher : MaybeMatchAcceptHeader = Res::accepted_types().into(); + let content_matcher : MaybeMatchContentTypeHeader = Body::supported_types().into(); self.0.post(&self.1) - .extend_route_matcher(matcher) + .extend_route_matcher(accept_matcher) + .extend_route_matcher(content_matcher) .to(|state| create_handler::(state)); } @@ -398,9 +431,11 @@ macro_rules! implDrawResourceRoutes { Res : ResourceResult, Handler : ResourceUpdateAll { - let matcher : MaybeMatchAcceptHeader = Res::accepted_types().into(); + let accept_matcher : MaybeMatchAcceptHeader = Res::accepted_types().into(); + let content_matcher : MaybeMatchContentTypeHeader = Body::supported_types().into(); self.0.put(&self.1) - .extend_route_matcher(matcher) + .extend_route_matcher(accept_matcher) + .extend_route_matcher(content_matcher) .to(|state| update_all_handler::(state)); } @@ -411,9 +446,11 @@ macro_rules! implDrawResourceRoutes { Res : ResourceResult, Handler : ResourceUpdate { - let matcher : MaybeMatchAcceptHeader = Res::accepted_types().into(); + let accept_matcher : MaybeMatchAcceptHeader = Res::accepted_types().into(); + let content_matcher : MaybeMatchContentTypeHeader = Body::supported_types().into(); self.0.put(&format!("{}/:id", self.1)) - .extend_route_matcher(matcher) + .extend_route_matcher(accept_matcher) + .extend_route_matcher(content_matcher) .with_path_extractor::>() .to(|state| update_handler::(state)); }