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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use crate::response::Response;
use crate::Result;
use hyper::{Body, StatusCode};
use serde::Serialize;

/// This trait is implemented for all the common types you can return from an endpoint
///
/// It's also implemented for `Response` and `hyper::Response` for compatibility.
/// There is an implementation for `Result<R> where R: Responder` which allows fallible
/// functions to be used as endpoints
///
/// ```
/// use highnoon::{Request, Responder, Json, StatusCode};
///
/// fn example_1(_: Request<()>) -> impl Responder {
///     // return status code
///     StatusCode::NOT_FOUND
/// }
///
/// fn example_2(_: Request<()>) -> impl Responder {
///     // return strings (&str or String)
///     "Hello World"
/// }
///
/// fn example_3(_: Request<()>) -> impl Responder {
///     // return status code with data
///     (StatusCode::NOT_FOUND, "Not found!")
/// }
///
/// fn example_4(_: Request<()>) -> impl Responder {
///     // return JSON data - for any type implementing `serde::Serialize`
///     Json(vec![1, 2, 3])
/// }
///
/// fn example_5(_: Request<()>) -> highnoon::Result<impl Responder> {
///     // fallible functions too
///     // (also works the return type as `impl Responder` as long as Rust can infer
///     // the function returns `highnoon::Result`)
///     Ok((StatusCode::CONFLICT, "Already Exists"))
/// }
/// ```

pub trait Responder {
    fn into_response(self) -> Result<Response>;
}

impl Responder for StatusCode {
    fn into_response(self) -> Result<Response> {
        Ok(Response::status(self))
    }
}

impl Responder for String {
    fn into_response(self) -> Result<Response> {
        Ok(Response::ok().body(self))
    }
}

impl Responder for &str {
    fn into_response(self) -> Result<Response> {
        Ok(Response::ok().body(self.to_owned()))
    }
}

impl Responder for &[u8] {
    fn into_response(self) -> Result<Response> {
        Ok(Response::ok().body(self.to_vec()))
    }
}

impl Responder for Vec<u8> {
    fn into_response(self) -> Result<Response> {
        Ok(Response::ok().body(self))
    }
}

impl<R: Responder> Responder for (StatusCode, R) {
    fn into_response(self) -> Result<Response> {
        let mut resp = self.1.into_response()?;
        resp.set_status(self.0);
        Ok(resp)
    }
}

/// Returns `StatusCode::NotFound` for `None`, and the inner value for `Some`
impl<R: Responder> Responder for Option<R> {
    fn into_response(self) -> Result<Response> {
        match self {
            None => StatusCode::NOT_FOUND.into_response(),
            Some(r) => r.into_response(),
        }
    }
}

/// A Wrapper to return a JSON payload. This can be wrapped over any `serde::Serialize` type.
/// ```
/// use highnoon::{Request, Responder, Json};
/// fn returns_json(_: Request<()>) -> impl Responder {
///     Json(vec!["an", "array"])
/// }
/// ```
pub struct Json<T: Serialize>(pub T);

impl<T: Serialize> Responder for Json<T> {
    fn into_response(self) -> Result<Response> {
        Response::ok().json(self.0)
    }
}

/// A Wrapper to return Form data. This can be wrapped over any `serde::Serialize` type.
pub struct Form<T: Serialize>(pub T);

impl<T: Serialize> Responder for Form<T> {
    fn into_response(self) -> Result<Response> {
        Response::ok().form(self.0)
    }
}

/// Identity implementation
impl Responder for Response {
    fn into_response(self) -> Result<Response> {
        Ok(self)
    }
}

/// Compatibility with the inner hyper::Response
impl Responder for hyper::Response<Body> {
    fn into_response(self) -> Result<Response> {
        Ok(self.into())
    }
}

impl<R: Responder> Responder for Result<R> {
    fn into_response(self) -> Result<Response> {
        self.and_then(|r| r.into_response())
    }
}