fire_http_representation/response/
builder.rs1use super::Response;
2use crate::body::Body;
3use crate::header::{
4 values::IntoHeaderName, ContentType, HeaderValue, HeaderValues,
5 ResponseHeader, StatusCode, CONTENT_LENGTH,
6};
7
8use std::fmt;
9
10#[derive(Debug)]
12pub struct ResponseBuilder {
13 header: ResponseHeader,
14 body: Body,
15}
16
17impl ResponseBuilder {
18 pub fn new() -> Self {
20 Self {
21 header: ResponseHeader::default(),
22 body: Body::new(),
23 }
24 }
25
26 pub fn status_code(mut self, status_code: StatusCode) -> Self {
28 self.header.status_code = status_code;
29 self
30 }
31
32 pub fn content_type(
34 mut self,
35 content_type: impl Into<ContentType>,
36 ) -> Self {
37 self.header.content_type = content_type.into();
38 self
39 }
40
41 pub fn header<K, V>(mut self, key: K, val: V) -> Self
50 where
51 K: IntoHeaderName,
52 V: TryInto<HeaderValue>,
53 V::Error: fmt::Debug,
54 {
55 self.values_mut().insert(key, val);
56 self
57 }
58
59 pub fn values_mut(&mut self) -> &mut HeaderValues {
61 &mut self.header.values
62 }
63
64 pub fn body(mut self, body: impl Into<Body>) -> Self {
66 self.body = body.into();
67 self
68 }
69
70 pub fn build(mut self) -> Response {
73 if let Some(len) = self.body.len() {
76 self.values_mut().insert(CONTENT_LENGTH, len);
77 }
78
79 Response::new(self.header, self.body)
80 }
81}