tower_web/response/
str.rs

1use super::{Context, Response, Serializer};
2use error;
3
4use http;
5use http::header::{self, HeaderValue};
6
7use std::io;
8
9impl Response for String {
10    type Buf = io::Cursor<Vec<u8>>;
11    type Body = error::Map<String>;
12
13    fn into_http<S: Serializer>(self, context: &Context<S>) -> Result<http::Response<Self::Body>, ::Error> {
14        respond(self, context)
15    }
16}
17
18impl Response for &'static str {
19    type Buf = io::Cursor<&'static [u8]>;
20    type Body = error::Map<&'static str>;
21
22    fn into_http<S: Serializer>(self, context: &Context<S>) -> Result<http::Response<Self::Body>, ::Error> {
23        respond(self, context)
24    }
25}
26
27fn respond<T, S: Serializer>(value: T, context: &Context<S>)
28    -> Result<http::Response<error::Map<T>>, ::Error>
29{
30    let content_type = context.content_type_header()
31        .map(|content_type| content_type.clone())
32        .unwrap_or_else(|| HeaderValue::from_static("text/plain"));
33
34    let response = http::Response::builder()
35        // Customize response
36        .status(200)
37        .header(header::CONTENT_TYPE, content_type)
38        .body(error::Map::new(value))
39        .unwrap();
40
41    Ok(response)
42}