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
92
93
94
95
96
use http;
use http::header::HeaderValue;
use http::Request;

use service::http::RequestBody;

// ==== traits ====

pub trait Input: InputImpl {}

pub trait InputImpl {
    fn build_request(self) -> http::Result<Request<RequestBody>>;
}

pub trait IntoRequestBody: IntoRequestBodyImpl {}

pub trait IntoRequestBodyImpl {
    fn content_type(&self) -> Option<HeaderValue> {
        None
    }
    fn into_request_body(self) -> RequestBody;
}

// === implementors ===

impl<T: IntoRequestBody> Input for Request<T> {}
impl<T: IntoRequestBody> InputImpl for Request<T> {
    fn build_request(mut self) -> http::Result<Request<RequestBody>> {
        if let Some(content_type) = self.body().content_type() {
            self.headers_mut().append("content-type", content_type);
        }
        Ok(self.map(IntoRequestBodyImpl::into_request_body))
    }
}

impl<T: IntoRequestBody, E: Into<http::Error>> Input for Result<Request<T>, E> {}
impl<T: IntoRequestBody, E: Into<http::Error>> InputImpl for Result<Request<T>, E> {
    fn build_request(self) -> http::Result<Request<RequestBody>> {
        self.map_err(Into::into)?.build_request()
    }
}

impl Input for http::request::Builder {}
impl InputImpl for http::request::Builder {
    fn build_request(mut self) -> http::Result<Request<RequestBody>> {
        (&mut self).build_request()
    }
}

impl<'a> Input for &'a mut http::request::Builder {}
impl<'a> InputImpl for &'a mut http::request::Builder {
    fn build_request(self) -> http::Result<Request<RequestBody>> {
        self.body(RequestBody(Default::default()))
    }
}

impl IntoRequestBody for () {}
impl IntoRequestBodyImpl for () {
    fn into_request_body(self) -> RequestBody {
        RequestBody(Default::default())
    }
}

impl<'a> IntoRequestBody for &'a str {}
impl<'a> IntoRequestBodyImpl for &'a str {
    fn content_type(&self) -> Option<HeaderValue> {
        Some(HeaderValue::from_static("text/plain; charset=utf-8"))
    }
    fn into_request_body(self) -> RequestBody {
        RequestBody(self.to_owned().into())
    }
}

impl IntoRequestBody for String {}
impl IntoRequestBodyImpl for String {
    fn content_type(&self) -> Option<HeaderValue> {
        Some(HeaderValue::from_static("text/plain; charset=utf-8"))
    }
    fn into_request_body(self) -> RequestBody {
        RequestBody(self.into())
    }
}

impl<'a> IntoRequestBody for &'a [u8] {}
impl<'a> IntoRequestBodyImpl for &'a [u8] {
    fn into_request_body(self) -> RequestBody {
        RequestBody(self.to_owned().into())
    }
}

impl IntoRequestBody for Vec<u8> {}
impl IntoRequestBodyImpl for Vec<u8> {
    fn into_request_body(self) -> RequestBody {
        RequestBody(self.into())
    }
}