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
//! Provides implementations for more convenient use with the http crate.

use crate::RoutingFailure;
use http02 as http;

impl<B> crate::Request for http::Request<B> {
    type Method = http::Method;

    fn path(&self) -> &str {
        let path = self.uri().path();
        assert!(path.starts_with('/'));
        &path[1..]
    }

    fn method(&self) -> &Self::Method {
        self.method()
    }
}

/// Extension trait for RoutingFailure to provide `to_simple_response`
pub trait RoutingFailureExtHttp {
    /// Convert a `RoutingFailure` to a simple http Response
    fn to_simple_response<B: From<&'static str>>(&self) -> http::Response<B>;
}

impl RoutingFailureExtHttp for RoutingFailure {
    fn to_simple_response<B: From<&'static str>>(&self) -> http::Response<B> {
        let code = match self {
            RoutingFailure::NotFound => http::StatusCode::NOT_FOUND,
            RoutingFailure::MethodNotAllowed => http::StatusCode::METHOD_NOT_ALLOWED,
        };

        let mut resp = http::Response::new(code.canonical_reason().unwrap().into());
        *resp.status_mut() = code;

        resp
    }
}