gotham/router/builder/
modify.rs

1use hyper::Body;
2
3use std::panic::RefUnwindSafe;
4
5use crate::extractor::{PathExtractor, QueryStringExtractor};
6use crate::pipeline::PipelineHandleChain;
7use crate::router::builder::single::DefineSingleRoute;
8use crate::router::builder::SingleRouteBuilder;
9use crate::router::route::matcher::{AndRouteMatcher, RouteMatcher};
10
11/// Describes the operation of replacing a `PathExtractor` on a route. This trait exists to remove
12/// type clutter from the documentation of `SingleRouteBuilder::with_path_extractor`.
13pub trait ReplacePathExtractor<T>
14where
15    T: PathExtractor<Body>,
16{
17    /// The type returned when replacing the `PathExtractor` with the target type.
18    type Output: DefineSingleRoute;
19
20    #[doc(hidden)]
21    /// Replaces the `PathExtractor` in `self` with the parameterized type `T`. This is a type
22    /// level operation so takes no value.
23    fn replace_path_extractor(self) -> Self::Output;
24}
25
26impl<'a, M, C, P, PE, QSE, NPE> ReplacePathExtractor<NPE>
27    for SingleRouteBuilder<'a, M, C, P, PE, QSE>
28where
29    M: RouteMatcher + Send + Sync + 'static,
30    C: PipelineHandleChain<P> + Send + Sync + 'static,
31    P: RefUnwindSafe + Send + Sync + 'static,
32    PE: PathExtractor<Body> + Send + Sync + 'static,
33    QSE: QueryStringExtractor<Body> + Send + Sync + 'static,
34    NPE: PathExtractor<Body> + Send + Sync + 'static,
35{
36    type Output = SingleRouteBuilder<'a, M, C, P, NPE, QSE>;
37
38    fn replace_path_extractor(self) -> Self::Output {
39        self.coerce()
40    }
41}
42
43/// Describes the operation of replacing a `QueryStringExtractor` on a route. This trait exists to
44/// remove type clutter from the documentation of `SingleRouteBuilder::with_query_string_extractor`.
45pub trait ReplaceQueryStringExtractor<T>
46where
47    T: QueryStringExtractor<Body>,
48{
49    /// The type returned when replacing the `QueryStringExtractor` with the target type.
50    type Output: DefineSingleRoute;
51
52    #[doc(hidden)]
53    /// Replaces the `QueryStringExtractor` in `self` with the parameterized type `T`. This is a
54    /// type level operation so takes no value.
55    fn replace_query_string_extractor(self) -> Self::Output;
56}
57
58impl<'a, M, C, P, PE, QSE, NQSE> ReplaceQueryStringExtractor<NQSE>
59    for SingleRouteBuilder<'a, M, C, P, PE, QSE>
60where
61    M: RouteMatcher + Send + Sync + 'static,
62    C: PipelineHandleChain<P> + Send + Sync + 'static,
63    P: RefUnwindSafe + Send + Sync + 'static,
64    PE: PathExtractor<Body> + Send + Sync + 'static,
65    QSE: QueryStringExtractor<Body> + Send + Sync + 'static,
66    NQSE: QueryStringExtractor<Body> + Send + Sync + 'static,
67{
68    type Output = SingleRouteBuilder<'a, M, C, P, PE, NQSE>;
69
70    fn replace_query_string_extractor(self) -> Self::Output {
71        self.coerce()
72    }
73}
74
75/// Describes the operation of extending a `RouteMatcher` on a route. This trait exists to remove
76/// type clutter from the documentation of `SingleRouteBuilder::add_route_matcher`.
77pub trait ExtendRouteMatcher<NRM>
78where
79    NRM: RouteMatcher + Send + Sync + 'static,
80{
81    /// The type returned when extending the existing `RouteMatcher` with the target type.
82    type Output: DefineSingleRoute;
83
84    #[doc(hidden)]
85    /// Combines the existing `RouteMatcher` using an `AndRouteMatcher` with the `RouteMatcher`
86    /// defined as NM
87    fn extend_route_matcher(self, matcher: NRM) -> Self::Output;
88}
89
90impl<'a, M, NRM, C, P, PE, QSE> ExtendRouteMatcher<NRM> for SingleRouteBuilder<'a, M, C, P, PE, QSE>
91where
92    M: RouteMatcher + Send + Sync + 'static,
93    NRM: RouteMatcher + Send + Sync + 'static,
94    C: PipelineHandleChain<P> + Send + Sync + 'static,
95    P: RefUnwindSafe + Send + Sync + 'static,
96    PE: PathExtractor<Body> + Send + Sync + 'static,
97    QSE: QueryStringExtractor<Body> + Send + Sync + 'static,
98{
99    /// The type returned when extending the existing `RouteMatcher` with the target type.
100    type Output = SingleRouteBuilder<'a, AndRouteMatcher<M, NRM>, C, P, PE, QSE>;
101
102    fn extend_route_matcher(self, matcher: NRM) -> Self::Output {
103        SingleRouteBuilder {
104            matcher: AndRouteMatcher::<M, NRM>::new(self.matcher, matcher),
105            phantom: self.phantom,
106            node_builder: self.node_builder,
107            pipeline_chain: self.pipeline_chain,
108            pipelines: self.pipelines,
109        }
110    }
111}