tower_web/response/
context.rs

1use response::{Serializer, SerializerContext};
2
3use bytes::Bytes;
4use http;
5use http::header::HeaderValue;
6use http::status::StatusCode;
7use serde::Serialize;
8
9/// Context available when serializing the response.
10#[derive(Debug)]
11pub struct Context<'a, S: Serializer + 'a> {
12    request: &'a http::Request<()>,
13    serializer: &'a S,
14    default_format: Option<&'a S::Format>,
15    content_type: Option<&'a HeaderValue>,
16    resource_mod: Option<&'a str>,
17    resource_name: Option<&'a str>,
18    handler_name: Option<&'a str>,
19    template: Option<&'a str>,
20}
21
22impl<'a, S> Context<'a, S>
23where
24    S: Serializer,
25{
26    /// Create a new response context.
27    pub fn new(request: &'a http::Request<()>, serializer: &'a S) -> Context<'a, S>
28    {
29        Context {
30            request,
31            serializer,
32            default_format: None,
33            content_type: None,
34            resource_mod: None,
35            resource_name: None,
36            handler_name: None,
37            template: None,
38        }
39    }
40
41    /// Returns a reference to the request
42    pub fn request(&self) -> &http::Request<()> {
43        self.request
44    }
45
46    #[doc(hidden)]
47    pub fn set_resource_mod(&mut self, value: &'a str) {
48        self.resource_mod = Some(value);
49    }
50
51    #[doc(hidden)]
52    pub fn set_resource_name(&mut self, value: &'a str) {
53        self.resource_name = Some(value);
54    }
55
56    #[doc(hidden)]
57    pub fn set_handler_name(&mut self, value: &'a str) {
58        self.handler_name = Some(value);
59    }
60
61    #[doc(hidden)]
62    pub fn serializer_context(&self) -> SerializerContext {
63        let mut ret = SerializerContext::new(self.request);
64        ret.set_resource_mod(self.resource_mod);
65        ret.set_resource_name(self.resource_name);
66        ret.set_handler_name(self.handler_name);
67
68        if let Some(template) = self.template {
69            ret.set_template(template);
70        }
71
72        ret
73    }
74
75    #[doc(hidden)]
76    pub fn set_default_format(&mut self, value: &'a S::Format) {
77        self.default_format = Some(value);
78    }
79
80    #[doc(hidden)]
81    pub fn set_content_type(&mut self, value: &'a HeaderValue) {
82        self.content_type = Some(value);
83    }
84
85    /// Returns the `template` value set for the response.
86    pub fn template(&self) -> Option<&str> {
87        self.template
88    }
89
90    #[doc(hidden)]
91    pub fn set_template(&mut self, value: &'a str) {
92        self.template = Some(value);
93    }
94
95    /// Serialize a value.
96    ///
97    /// This uses the default content type for the action.
98    ///
99    /// Returns an error when a default content type is not set.
100    pub fn serialize<T>(&self, value: &T, context: &SerializerContext)
101        -> Result<Bytes, ::Error>
102    where
103        T: Serialize,
104    {
105        let format = match self.default_format {
106            Some(format) => format,
107            None => {
108                warn!("no default serialization format associated with action");
109                return Err(::Error::from(StatusCode::INTERNAL_SERVER_ERROR));
110            }
111        };
112
113        self.serializer.serialize(value, format, context)
114    }
115
116    /// Serialize a value as the specified content type.
117    pub fn serialize_as<T>(&self, _value: &T, _content_type: &str)
118        -> Result<Bytes, ::Error>
119    where
120        T: Serialize,
121    {
122        unimplemented!();
123    }
124
125    /// Returns a `HeaderValue` representation of the default content type.
126    pub fn content_type_header(&self) -> Option<&HeaderValue> {
127        self.content_type
128    }
129}