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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
use std::{
    borrow::Cow,
    error::Error as StdError,
    fmt,
    ops::{Deref, DerefMut},
};

use viz_utils::serde;

use crate::{http, Error, Result};

/// Viz Response
pub struct Response {
    pub(crate) raw: http::Response,
}

impl StdError for Response {}

impl fmt::Debug for Response {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Response")
            .field("status", &self.status())
            .field("header", &self.headers())
            .finish()
    }
}

impl fmt::Display for Response {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Response")
            .field("status", &self.status())
            .field("header", &self.headers())
            .finish()
    }
}

impl Response {
    /// Creates a response
    pub fn new() -> Self {
        Self { raw: http::Response::new(http::Body::empty()) }
    }

    /// Gets raw response
    pub fn raw(&self) -> &http::Response {
        &self.raw
    }

    /// Get mut raw response
    pub fn raw_mut(&mut self) -> &mut http::Response {
        &mut self.raw
    }

    /// Responds Text
    pub fn text(data: impl Into<http::Body>) -> Self {
        Self::body(data, mime::TEXT_PLAIN.as_ref())
    }

    /// Responds HTML
    pub fn html(data: impl Into<http::Body>) -> Self {
        Self::body(data, mime::TEXT_HTML.as_ref())
    }

    /// Responds JSON
    pub fn json(data: impl Into<http::Body>) -> Self {
        Self::body(data, mime::APPLICATION_JSON.as_ref())
    }

    /// Sets body for response
    pub fn body(data: impl Into<http::Body>, ct: &'static str) -> Self {
        let mut raw = http::Response::new(data.into());
        raw.headers_mut().insert(http::header::CONTENT_TYPE, http::HeaderValue::from_static(ct));
        Self { raw }
    }

    /// Sets the `Content-Location` header
    pub fn location(location: &'static str) -> Self {
        let mut res = Self::new();
        res.headers_mut()
            .insert(http::header::CONTENT_LOCATION, http::HeaderValue::from_static(location));
        res
    }

    /// Redirects to the URL derived from the specified path
    pub fn redirect(location: &'static str, status: http::StatusCode) -> Self {
        let mut res = Self::new();
        res.headers_mut().insert(http::header::LOCATION, http::HeaderValue::from_static(location));
        res.with_status(status)
    }

    /*
    pub fn download(data: impl Into<http::Body>, ct: &'static str) -> Self {
        let mut raw = http::Response::new(data.into());
        raw.headers_mut().insert(http::header::CONTENT_TYPE, http::HeaderValue::from_static(ct));
        raw.headers_mut().insert(http::header::CONTENT_DISPOSITION, http::HeaderValue::from_static(""));
        Self { raw }
    }
    */

    /// Sets status for response
    pub fn with_status(mut self, status: http::StatusCode) -> Self {
        *self.status_mut() = status;
        self
    }
}

impl Default for Response {
    fn default() -> Self {
        Self::new()
    }
}

impl Deref for Response {
    type Target = http::Response;

    fn deref(&self) -> &Self::Target {
        &self.raw
    }
}

impl DerefMut for Response {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.raw
    }
}

impl From<Response> for http::Response {
    fn from(res: Response) -> Self {
        res.raw
    }
}

impl From<http::Response> for Response {
    fn from(raw: http::Response) -> Self {
        Self { raw }
    }
}

impl From<Error> for Response {
    fn from(e: Error) -> Self {
        let mut raw = http::Response::new(http::Body::from(e.to_string()));
        *raw.status_mut() = http::StatusCode::INTERNAL_SERVER_ERROR;
        Self { raw }
    }
}

impl<T, E> From<Result<T, E>> for Response
where
    T: Into<Response>,
    E: Into<Response>,
{
    fn from(r: Result<T, E>) -> Self {
        r.map_or_else(Into::into, Into::into)
    }
}

impl From<String> for Response {
    fn from(s: String) -> Self {
        Self { raw: http::Response::new(http::Body::from(s)) }
    }
}

impl From<&'_ str> for Response {
    fn from(s: &'_ str) -> Self {
        Self { raw: http::Response::new(s.to_owned().into()) }
    }
}

impl From<Cow<'_, str>> for Response {
    fn from(s: Cow<'_, str>) -> Self {
        s.into()
    }
}

impl From<&'_ [u8]> for Response {
    fn from(s: &'_ [u8]) -> Self {
        Self { raw: http::Response::new(http::Body::from(s.to_owned())) }
    }
}

impl From<http::Body> for Response {
    fn from(body: http::Body) -> Self {
        Self { raw: http::Response::new(body) }
    }
}

impl From<()> for Response {
    fn from(_: ()) -> Self {
        Self { raw: http::Response::new(http::Body::empty()) }
    }
}

impl From<http::StatusCode> for Response {
    fn from(s: http::StatusCode) -> Self {
        let mut res = Response::new();
        *res.status_mut() = s;
        // *res.body_mut() = s.to_string().into();
        res
    }
}

impl<T> From<(http::StatusCode, T)> for Response
where
    T: Into<Response>,
{
    fn from(t: (http::StatusCode, T)) -> Self {
        let mut res = t.1.into();
        *res.status_mut() = t.0;
        res
    }
}

impl From<serde::json::Value> for Response {
    fn from(v: serde::json::Value) -> Self {
        match serde::json::to_vec(&v) {
            Ok(d) => Self::json(d),
            Err(e) => Into::<Error>::into(e).into(),
        }
    }
}