desirable 1.2.0

A minimal Rust web application framework
Documentation
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
use crate::{HyperResponse, Result};
use bytes::Bytes;
use http_body_util::Full;
use hyper::StatusCode;
use hyper::header;

/// Cached content-type header value for JSON responses.
static CONTENT_TYPE_JSON: header::HeaderValue =
  header::HeaderValue::from_static("application/json");

/// Cached content-type header value for plain text responses.
static CONTENT_TYPE_TEXT: header::HeaderValue =
  header::HeaderValue::from_static("text/plain; charset=utf-8");

/// Cached content-type header value for octet-stream responses.
/// Currently unused but kept for potential future byte-based responses.
#[allow(dead_code)]
static CONTENT_TYPE_OCTET: header::HeaderValue =
  header::HeaderValue::from_static("application/octet-stream");

/// The HTTP response type for the desirable framework.
///
/// Provides constructors for common response types and ergonomic builders.
///
/// # Example
///
/// ```rust,ignore
/// use desirable::{Response, Result};
///
/// async fn handler() -> Result {
///   Response::json(&User { name: "Alice" })
/// }
/// ```
pub struct Response {
  /// The underlying hyper response
  pub inner: HyperResponse,
}

impl Response {
  /// Creates a new Response with the given hyper response.
  fn new(response: HyperResponse) -> Self {
    Self { inner: response }
  }

  /// Returns the response status code.
  pub fn status(&self) -> StatusCode {
    self.inner.status()
  }

  /// Creates a text response with the given body.
  ///
  /// # Type Parameters
  ///
  /// * `T` - Any type that can be converted to `Bytes`
  ///
  /// # Arguments
  ///
  /// * `body` - The response body
  ///
  /// # Returns
  ///
  /// A `Result` containing the response or an error
  ///
  /// # Example
  ///
  /// ```rust,ignore
  /// Response::body("Hello, World!")
  /// ```
  pub fn body<T>(body: T) -> Result<Self>
  where
    Bytes: From<T>,
  {
    let response = hyper::http::Response::builder()
      .body(Full::new(Bytes::from(body)))?
      .into();
    Ok(response)
  }

  /// Creates a text response with a specific status code.
  ///
  /// # Arguments
  ///
  /// * `status` - The HTTP status code (e.g., 200, 404, 500)
  /// * `val` - The response body as a string
  ///
  /// # Returns
  ///
  /// A `Result` containing the response or an error
  ///
  /// # Example
  ///
  /// ```rust,ignore
  /// Response::with_status(404, "Not Found".to_string())
  /// ```
  pub fn with_status(status: u16, val: String) -> Result<Self> {
    let response = hyper::http::Response::builder()
      .header(header::CONTENT_TYPE, CONTENT_TYPE_TEXT.clone())
      .status(StatusCode::from_u16(status)?)
      .body(Full::new(Bytes::from(val)))?
      .into();
    Ok(response)
  }

  /// Creates a JSON response with the given serializable payload.
  ///
  /// # Type Parameters
  ///
  /// * `T` - A type that implements `serde::Serialize`
  ///
  /// # Arguments
  ///
  /// * `payload` - The data to serialize as JSON
  ///
  /// # Returns
  ///
  /// A JSON response with `Content-Type: application/json`
  ///
  /// # Panics
  ///
  /// Panics if serialization fails. This should never happen for well-formed types.
  ///
  /// # Example
  ///
  /// ```rust,ignore
  /// #[derive(serde::Serialize)]
  /// struct User {
  ///   id: i32,
  ///   name: String,
  /// }
  ///
  /// // Now supports references without 'static bound:
  /// let user = User { id: 1, name: "Alice".into() };
  /// Response::json(&user)
  /// ```
  pub fn json<T: serde::Serialize>(payload: T) -> Self {
    let data = serde_json::to_vec(&payload).expect("JSON serialization failed");
    hyper::http::Response::builder()
      .header(header::CONTENT_TYPE, CONTENT_TYPE_JSON.clone())
      .body(Full::new(Bytes::from(data)))
      .unwrap()
      .into()
  }

  /// Creates a redirect response to the given URL.
  ///
  /// # Arguments
  ///
  /// * `status` - The redirect status code (301, 302, 303, 307, 308)
  /// * `url` - The URL to redirect to
  ///
  /// # Returns
  ///
  /// A `Result` containing the redirect response or an error
  ///
  /// # Example
  ///
  /// ```rust,ignore
  /// Response::redirect(302, "/new-location")
  /// ```
  pub fn redirect(status: u16, url: &str) -> Result<Self> {
    let response = hyper::http::Response::builder()
      .status(hyper::StatusCode::from_u16(status)?)
      .header(header::LOCATION, url)
      .body(Full::new(Bytes::default()))?
      .into();
    Ok(response)
  }

  /// Sets a header on this response (mutable reference).
  ///
  /// # Arguments
  ///
  /// * `key` - The header name
  /// * `value` - The header value
  ///
  /// # Example
  ///
  /// ```rust,ignore
  /// response.set_header(header::CONTENT_TYPE, "application/json".parse().unwrap());
  /// ```
  pub fn set_header(&mut self, key: hyper::header::HeaderName, value: hyper::header::HeaderValue) {
    self.inner.headers_mut().insert(key, value);
  }

  /// Builder-style header setter. Consumes self and returns Self for chaining.
  ///
  /// # Arguments
  ///
  /// * `key` - The header name
  /// * `value` - The header value
  ///
  /// # Example
  ///
  /// ```rust,ignore
  /// let response = Response::body("hello")?
  ///     .with_header(header::CONTENT_TYPE, "text/plain".parse().unwrap());
  /// ```
  #[must_use]
  pub fn with_header(
    mut self,
    key: hyper::header::HeaderName,
    value: hyper::header::HeaderValue,
  ) -> Self {
    self.set_header(key, value);
    self
  }

  /// Returns a [`ResponseBuilder`] for constructing a response with the
  /// builder pattern.
  ///
  /// # Example
  ///
  /// ```rust,ignore
  /// Response::builder()
  ///     .status(StatusCode::CREATED)
  ///     .header("X-Custom".parse().unwrap(), "value".parse().unwrap())
  ///     .json(&my_data)
  /// ```
  #[must_use]
  pub fn builder() -> ResponseBuilder {
    ResponseBuilder::new()
  }
}

/// Builder for constructing HTTP responses.
///
/// Provides chainable methods for setting status, headers, and body.
/// Created via [`Response::builder()`].
pub struct ResponseBuilder {
  inner: hyper::http::response::Builder,
}

impl ResponseBuilder {
  fn new() -> Self {
    Self {
      inner: hyper::http::response::Builder::new(),
    }
  }

  /// Sets the HTTP status code.
  #[must_use]
  pub fn status(mut self, status: hyper::StatusCode) -> Self {
    self.inner = self.inner.status(status);
    self
  }

  /// Sets the HTTP status code from a `u16`.
  ///
  /// # Panics
  ///
  /// Panics if the status code is invalid (not in the range 100-999).
  #[must_use]
  pub fn status_u16(self, status: u16) -> Self {
    self.status(hyper::StatusCode::from_u16(status).expect("invalid HTTP status code"))
  }

  /// Sets a header on the response.
  #[must_use]
  pub fn header(
    mut self,
    key: hyper::header::HeaderName,
    value: hyper::header::HeaderValue,
  ) -> Self {
    self.inner = self.inner.header(key, value);
    self
  }

  /// Sets the response body as plain text with `Content-Type: text/plain; charset=utf-8`.
  ///
  /// # Returns
  ///
  /// A `Result` containing the response or an HTTP builder error.
  pub fn text<T>(self, body: T) -> Result<Response>
  where
    Bytes: From<T>,
  {
    let response = self
      .inner
      .header(header::CONTENT_TYPE, CONTENT_TYPE_TEXT.clone())
      .body(Full::new(Bytes::from(body)))?
      .into();
    Ok(response)
  }

  /// Sets the response body as JSON with `Content-Type: application/json`.
  ///
  /// # Panics
  ///
  /// Panics if serialization fails.
  pub fn json<T: serde::Serialize>(self, payload: T) -> Response {
    let data = serde_json::to_vec(&payload).expect("JSON serialization failed");
    self
      .inner
      .header(header::CONTENT_TYPE, CONTENT_TYPE_JSON.clone())
      .body(Full::new(Bytes::from(data)))
      .unwrap()
      .into()
  }

  /// Sets the response body without modifying headers.
  ///
  /// # Returns
  ///
  /// A `Result` containing the response or an HTTP builder error.
  pub fn body<T>(self, body: T) -> Result<Response>
  where
    Bytes: From<T>,
  {
    let response = self.inner.body(Full::new(Bytes::from(body)))?.into();
    Ok(response)
  }
}

impl From<HyperResponse> for Response {
  fn from(response: HyperResponse) -> Self {
    Response::new(response)
  }
}

impl From<()> for Response {
  fn from(_: ()) -> Self {
    hyper::http::Response::builder()
      .header(header::CONTENT_TYPE, &CONTENT_TYPE_TEXT)
      .body(Full::new(Bytes::default()))
      .unwrap()
      .into()
  }
}

impl From<String> for Response {
  fn from(val: String) -> Self {
    hyper::http::Response::builder()
      .header(header::CONTENT_TYPE, &CONTENT_TYPE_TEXT)
      .body(Full::new(Bytes::from(val)))
      .unwrap()
      .into()
  }
}

impl From<&'static str> for Response {
  fn from(val: &'static str) -> Self {
    hyper::http::Response::builder()
      .header(header::CONTENT_TYPE, &CONTENT_TYPE_TEXT)
      .body(Full::new(Bytes::from(val)))
      .unwrap()
      .into()
  }
}

impl From<anyhow::Error> for Response {
  fn from(e: anyhow::Error) -> Self {
    Response::with_status(500, e.to_string()).unwrap()
  }
}

#[cfg(test)]
mod tests {
  use super::*;

  #[test]
  fn test_response_status() {
    let response = Response::with_status(200, "OK".to_string()).unwrap();
    assert_eq!(response.status(), StatusCode::OK);
  }

  #[test]
  fn test_response_with_status() {
    let response = Response::with_status(404, "Not Found".to_string()).unwrap();
    assert_eq!(response.status(), StatusCode::NOT_FOUND);
  }

  #[test]
  fn test_response_body() {
    let response = Response::body("Hello").unwrap();
    assert_eq!(response.status(), StatusCode::OK);
  }

  #[test]
  fn test_response_json() {
    #[derive(serde::Serialize)]
    struct TestData {
      name: String,
      value: i32,
    }
    let data = TestData {
      name: "test".to_string(),
      value: 42,
    };
    let response = Response::json(data);
    assert_eq!(response.status(), StatusCode::OK);
  }

  #[test]
  fn test_response_json_ref() {
    #[derive(serde::Serialize)]
    struct TestData {
      name: String,
    }
    let data = TestData {
      name: "ref".to_string(),
    };
    // Can now pass references without 'static bound
    let response = Response::json(&data);
    assert_eq!(response.status(), StatusCode::OK);
  }

  #[test]
  fn test_response_redirect() {
    let response = Response::redirect(302, "/redirected").unwrap();
    assert_eq!(response.status(), StatusCode::FOUND);
  }

  #[test]
  fn test_response_from_string() {
    let response: Response = "hello".into();
    assert_eq!(response.status(), StatusCode::OK);
  }

  #[test]
  fn test_response_from_static_str() {
    let response: Response = "static".into();
    assert_eq!(response.status(), StatusCode::OK);
  }

  #[test]
  fn test_response_from_unit() {
    let response: Response = ().into();
    assert_eq!(response.status(), StatusCode::OK);
  }

  #[test]
  fn test_response_set_header() {
    let mut response = Response::body("test").unwrap();
    response.set_header(
      hyper::header::HeaderName::from_static("x-custom"),
      hyper::header::HeaderValue::from_static("value"),
    );
    assert_eq!(response.inner.headers().get("x-custom").unwrap(), "value");
  }

  #[test]
  fn test_response_with_header() {
    let response = Response::body("test").unwrap().with_header(
      hyper::header::HeaderName::from_static("x-custom"),
      hyper::header::HeaderValue::from_static("chained"),
    );
    assert_eq!(response.inner.headers().get("x-custom").unwrap(), "chained");
  }

  #[test]
  fn test_response_builder_json() {
    #[derive(serde::Serialize)]
    struct Msg {
      msg: String,
    }
    let response = Response::builder()
      .status(hyper::StatusCode::CREATED)
      .json(Msg {
        msg: "created".into(),
      });
    assert_eq!(response.status(), hyper::StatusCode::CREATED);
  }

  #[test]
  fn test_response_builder_text() {
    let response = Response::builder()
      .status(hyper::StatusCode::NOT_FOUND)
      .text("not found")
      .unwrap();
    assert_eq!(response.status(), hyper::StatusCode::NOT_FOUND);
  }

  #[test]
  fn test_response_builder_body() {
    let response = Response::builder()
      .status_u16(200)
      .body("raw body")
      .unwrap();
    assert_eq!(response.status(), hyper::StatusCode::OK);
  }

  #[test]
  fn test_response_builder_header() {
    let response = Response::builder()
      .header(
        hyper::header::HeaderName::from_static("x-custom"),
        hyper::header::HeaderValue::from_static("builder-value"),
      )
      .text("ok")
      .unwrap();
    assert_eq!(
      response.inner.headers().get("x-custom").unwrap(),
      "builder-value"
    );
  }
}