finchers_core/output/
response.rs

1use either::Either;
2use http::StatusCode;
3use http::header::{HeaderMap, HeaderValue};
4
5/// Trait representing additional information for constructing an HTTP response.
6///
7/// This trait is used as a helper to define the implementation of `Responder`.
8pub trait HttpResponse {
9    /// Returns a HTTP status code.
10    fn status_code(&self) -> StatusCode;
11
12    /// Append header values to given header map.
13    #[allow(unused_variables)]
14    fn append_headers(&self, headers: &mut HeaderMap<HeaderValue>) {}
15}
16
17macro_rules! impl_status {
18    ($($t:ty),*) => {$(
19        impl HttpResponse for $t {
20            fn status_code(&self) -> StatusCode {
21                StatusCode::OK
22            }
23        }
24    )*};
25}
26
27impl_status!(
28    bool,
29    char,
30    f32,
31    f64,
32    String,
33    i8,
34    i16,
35    i32,
36    i64,
37    isize,
38    u8,
39    u16,
40    u32,
41    u64,
42    usize
43);
44
45impl<L, R> HttpResponse for Either<L, R>
46where
47    L: HttpResponse,
48    R: HttpResponse,
49{
50    fn status_code(&self) -> StatusCode {
51        match *self {
52            Either::Left(ref l) => l.status_code(),
53            Either::Right(ref r) => r.status_code(),
54        }
55    }
56
57    fn append_headers(&self, headers: &mut HeaderMap<HeaderValue>) {
58        match *self {
59            Either::Left(ref l) => l.append_headers(headers),
60            Either::Right(ref r) => r.append_headers(headers),
61        }
62    }
63}