tower_web/response/
content_type.rs

1use http::header::HeaderValue;
2
3/// Content type of a response
4///
5/// Instances of `ContentType` are returned by [`Serializer::lookup`]. This type
6/// is not intended to be used by the end user besides using it as an argument
7/// to [`Context::new`].
8///
9/// [`Serializer::lookup`]: trait.Serializer.html#method.lookup
10/// [`Context::new`]: struct.Context.html
11#[derive(Debug)]
12pub struct ContentType<T> {
13    /// The HTTP header representing the content-type
14    header: HeaderValue,
15
16    /// Used by `Serializer` to match the content type with a specific
17    /// serializer.
18    format: T,
19}
20
21impl<T> ContentType<T> {
22    pub(crate) fn new(header: HeaderValue, format: T) -> Self {
23        ContentType {
24            header,
25            format,
26        }
27    }
28
29    #[doc(hidden)]
30    pub fn header(&self) -> &HeaderValue {
31        &self.header
32    }
33
34    #[doc(hidden)]
35    pub fn format(&self) -> &T {
36        &self.format
37    }
38
39    pub(crate) fn map<F, U>(self, f: F) -> ContentType<U>
40    where F: FnOnce(T) -> U
41    {
42        ContentType {
43            header: self.header,
44            format: f(self.format),
45        }
46    }
47}