hyper_router/route/
route_impl.rs

1use crate::handlers;
2use hyper::Method;
3use std::fmt;
4
5use super::RouteBuilder;
6use crate::Handler;
7use crate::Path;
8
9/// Holds route information
10pub struct Route {
11    /// HTTP method to match
12    pub method: Method,
13
14    /// Path to match
15    pub path: Path,
16
17    /// Request handler
18    ///
19    /// This should be method that accepts Hyper's Request and Response:
20    ///
21    /// ```ignore
22    /// use hyper::server::{Request, Response};
23    /// use hyper::header::{ContentLength, ContentType};
24    ///
25    /// fn hello_handler(_: Request) -> Response {
26    ///     let body = "Hello World";
27    ///     Response::new()
28    ///         .with_header(ContentLength(body.len() as u64))
29    ///         .with_header(ContentType::plaintext())
30    ///         .with_body(body)
31    /// }
32    /// ```
33    pub handler: Handler,
34}
35
36impl Route {
37    pub fn options(path: &str) -> RouteBuilder {
38        Route::from(Method::OPTIONS, path)
39    }
40
41    pub fn get(path: &str) -> RouteBuilder {
42        Route::from(Method::GET, path)
43    }
44
45    pub fn post(path: &str) -> RouteBuilder {
46        Route::from(Method::POST, path)
47    }
48
49    pub fn put(path: &str) -> RouteBuilder {
50        Route::from(Method::PUT, path)
51    }
52
53    pub fn delete(path: &str) -> RouteBuilder {
54        Route::from(Method::DELETE, path)
55    }
56
57    pub fn head(path: &str) -> RouteBuilder {
58        Route::from(Method::HEAD, path)
59    }
60
61    pub fn trace(path: &str) -> RouteBuilder {
62        Route::from(Method::TRACE, path)
63    }
64
65    pub fn connect(path: &str) -> RouteBuilder {
66        Route::from(Method::CONNECT, path)
67    }
68
69    pub fn patch(path: &str) -> RouteBuilder {
70        Route::from(Method::PATCH, path)
71    }
72
73    pub fn from(method: Method, path: &str) -> RouteBuilder {
74        RouteBuilder::new(Route {
75            method,
76            path: Path::new(path),
77            ..Route::default()
78        })
79    }
80}
81
82impl Default for Route {
83    fn default() -> Route {
84        Route {
85            method: Method::GET,
86            path: Path::new("/"),
87            handler: handlers::not_implemented_handler,
88        }
89    }
90}
91
92impl fmt::Debug for Route {
93    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
94        write!(
95            f,
96            "Route {{method: {:?}, path: {:?}}}",
97            self.method, self.path
98        )
99    }
100}