1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
use hyper::method::Method;
use handlers;
use std::fmt;

use Path;
use Handler;
use super::RouteBuilder;

/// Holds route information
pub struct Route {
    /// HTTP method to match
    pub method: Method,

    /// Path to match
    pub path: Path,

    /// Request handler
    ///
    /// This should be method that accepts Hyper's Request and Response:
    ///
    /// ```ignore
    /// use hyper::server::{Request, Response};
    ///
    /// fn hello_handler(_: Request, res: Response) {
    ///   res.send(b"Hello World").unwrap();
    /// }
    /// ``` 
    pub handler: Handler
}

impl Route {
    pub fn options(path: &str) -> RouteBuilder {
        Route::from(Method::Options, path)
    }

    pub fn get(path: &str) -> RouteBuilder {
        Route::from(Method::Get, path)
    }

    pub fn post(path: &str) -> RouteBuilder {
        Route::from(Method::Post, path)
    }

    pub fn put(path: &str) -> RouteBuilder {
        Route::from(Method::Put, path)
    }

    pub fn delete(path: &str) -> RouteBuilder {
        Route::from(Method::Delete, path)
    }

    pub fn head(path: &str) -> RouteBuilder {
        Route::from(Method::Head, path)
    }

    pub fn trace(path: &str) -> RouteBuilder {
        Route::from(Method::Trace, path)
    }

    pub fn connect(path: &str) -> RouteBuilder {
        Route::from(Method::Connect, path)
    }

    pub fn patch(path: &str) -> RouteBuilder {
        Route::from(Method::Patch, path)
    }

    pub fn from(method: Method, path: &str) -> RouteBuilder {
        RouteBuilder::new(Route {
            method: method,
            path: Path::new(path),
            .. Route::default()
        })
    }
}

impl Default for Route {
    fn default() -> Route {
        Route {
            method: Method::Get,
            path: Path::new("/"),
            handler: handlers::not_implemented_handler
        }
    }
}

impl fmt::Debug for Route {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Route {{method: {:?}, path: {:?}}}", self.method, self.path)
    }
}