gotham/router/route/matcher/
mod.rs1mod accept;
4mod access_control_request_method;
5mod and;
6mod any;
7mod content_type;
8
9pub use self::accept::AcceptHeaderRouteMatcher;
10pub use self::access_control_request_method::AccessControlRequestMethodMatcher;
11pub use self::and::AndRouteMatcher;
12pub use self::any::AnyRouteMatcher;
13pub use self::content_type::ContentTypeHeaderRouteMatcher;
14
15mod lookup_table;
16use self::lookup_table::{LookupTable, LookupTableFromTypes};
17
18use std::panic::RefUnwindSafe;
19
20use hyper::{Method, StatusCode};
21use log::trace;
22
23use crate::router::non_match::RouteNonMatch;
24use crate::state::{request_id, FromState, State};
25
26pub trait RouteMatcher: RefUnwindSafe + Clone {
29 fn is_match(&self, state: &State) -> Result<(), RouteNonMatch>;
31}
32
33pub trait IntoRouteMatcher {
35 type Output: RouteMatcher;
37
38 fn into_route_matcher(self) -> Self::Output;
40}
41
42impl IntoRouteMatcher for Vec<Method> {
43 type Output = MethodOnlyRouteMatcher;
44
45 fn into_route_matcher(self) -> Self::Output {
46 MethodOnlyRouteMatcher::new(self)
47 }
48}
49
50impl<M> IntoRouteMatcher for M
51where
52 M: RouteMatcher + Send + Sync + 'static,
53{
54 type Output = M;
55
56 fn into_route_matcher(self) -> Self::Output {
57 self
58 }
59}
60
61#[derive(Clone)]
88pub struct MethodOnlyRouteMatcher {
89 methods: Vec<Method>,
90}
91
92impl MethodOnlyRouteMatcher {
93 pub fn new(methods: Vec<Method>) -> Self {
95 MethodOnlyRouteMatcher { methods }
96 }
97}
98
99impl RouteMatcher for MethodOnlyRouteMatcher {
100 fn is_match(&self, state: &State) -> Result<(), RouteNonMatch> {
102 let method = Method::borrow_from(state);
103 if self.methods.iter().any(|m| m == method) {
104 trace!(
105 "[{}] matched request method {} to permitted method",
106 request_id(state),
107 method
108 );
109 Ok(())
110 } else {
111 trace!(
112 "[{}] did not match request method {}",
113 request_id(state),
114 method
115 );
116 Err(RouteNonMatch::new(StatusCode::METHOD_NOT_ALLOWED)
117 .with_allow_list(self.methods.as_slice()))
118 }
119 }
120}