rocket/local/
response.rs

1macro_rules! getter_method {
2    ($doc_prelude:literal, $desc:literal, $f:ident -> $r:ty) => (
3        getter_method!(@$doc_prelude, $f, $desc, $r,
4            concat!("let ", stringify!($f), " = response.", stringify!($f), "();"));
5    );
6    (@$doc_prelude:literal, $f:ident, $desc:expr, $r:ty, $use_it:expr) => (
7        /// Returns the
8        #[doc = $desc]
9        /// of `self`.
10        ///
11        /// # Example
12        ///
13        /// ```rust
14        #[doc = $doc_prelude]
15        ///
16        /// # Client::_test(|_, _, response| {
17        /// let response: LocalResponse = response;
18        #[doc = $use_it]
19        /// # });
20        /// ```
21        #[inline(always)]
22        pub fn $f(&self) -> $r {
23            self._response().$f()
24        }
25    )
26}
27
28macro_rules! pub_response_impl {
29    ($doc_prelude:literal $($prefix:tt $suffix:tt)?) =>
30{
31    getter_method!($doc_prelude, "HTTP status",
32        status -> crate::http::Status);
33
34    getter_method!($doc_prelude, "Content-Type, if a valid one is set,",
35        content_type -> Option<crate::http::ContentType>);
36
37    getter_method!($doc_prelude, "HTTP headers",
38        headers -> &crate::http::HeaderMap<'_>);
39
40    /// Return a cookie jar containing the HTTP cookies in the response.
41    ///
42    /// # Example
43    ///
44    /// ```rust
45    #[doc = $doc_prelude]
46    ///
47    /// # Client::_test(|_, _, response| {
48    /// let response: LocalResponse = response;
49    /// let string = response.cookies();
50    /// # });
51    /// ```
52    #[inline(always)]
53    pub fn cookies(&self) -> &crate::http::CookieJar<'_> {
54        self._cookies()
55    }
56
57    getter_method!($doc_prelude, "response body, if there is one,",
58        body -> &crate::response::Body<'_>);
59
60    /// Consumes `self` and reads the entirety of its body into a string.
61    ///
62    /// If reading fails, the body contains invalid UTF-8 characters, or the
63    /// body is unset in the response, returns `None`. Otherwise, returns
64    /// `Some`. The string may be empty if the body is empty.
65    ///
66    /// # Example
67    ///
68    /// ```rust
69    #[doc = $doc_prelude]
70    ///
71    /// # Client::_test(|_, _, response| {
72    /// let response: LocalResponse = response;
73    /// let string = response.into_string();
74    /// # });
75    /// ```
76    #[inline(always)]
77    pub $($prefix)? fn into_string(self) -> Option<String> {
78        if self._response().body().is_none() {
79            return None;
80        }
81
82        self._into_string() $(.$suffix)? .ok()
83    }
84
85    /// Consumes `self` and reads the entirety of its body into a `Vec` of
86    /// bytes.
87    ///
88    /// If reading fails or the body is unset in the response, returns `None`.
89    /// Otherwise, returns `Some`. The returned vector may be empty if the body
90    /// is empty.
91    ///
92    /// # Example
93    ///
94    /// ```rust
95    #[doc = $doc_prelude]
96    ///
97    /// # Client::_test(|_, _, response| {
98    /// let response: LocalResponse = response;
99    /// let bytes = response.into_bytes();
100    /// # });
101    /// ```
102    #[inline(always)]
103    pub $($prefix)? fn into_bytes(self) -> Option<Vec<u8>> {
104        if self._response().body().is_none() {
105            return None;
106        }
107
108        self._into_bytes() $(.$suffix)? .ok()
109    }
110
111    /// Consumes `self` and deserializes its body as JSON without buffering in
112    /// memory.
113    ///
114    /// If deserialization fails or the body is unset in the response, returns
115    /// `None`. Otherwise, returns `Some`.
116    ///
117    /// # Example
118    ///
119    /// ```rust
120    #[doc = $doc_prelude]
121    /// use rocket::serde::Deserialize;
122    ///
123    /// #[derive(Deserialize)]
124    /// struct Task {
125    ///     id: usize,
126    ///     complete: bool,
127    ///     text: String,
128    /// }
129    ///
130    /// # Client::_test(|_, _, response| {
131    /// let response: LocalResponse = response;
132    /// let task = response.into_json::<Task>();
133    /// # });
134    /// ```
135    #[cfg(feature = "json")]
136    #[cfg_attr(nightly, doc(cfg(feature = "json")))]
137    pub $($prefix)? fn into_json<T>(self) -> Option<T>
138        where T: Send + serde::de::DeserializeOwned + 'static
139    {
140        if self._response().body().is_none() {
141            return None;
142        }
143
144        self._into_json() $(.$suffix)?
145    }
146
147    /// Consumes `self` and deserializes its body as MessagePack without
148    /// buffering in memory.
149    ///
150    /// If deserialization fails or the body is unset in the response, returns
151    /// `None`. Otherwise, returns `Some`.
152    ///
153    /// # Example
154    ///
155    /// ```rust
156    #[doc = $doc_prelude]
157    /// use rocket::serde::Deserialize;
158    ///
159    /// #[derive(Deserialize)]
160    /// struct Task {
161    ///     id: usize,
162    ///     complete: bool,
163    ///     text: String,
164    /// }
165    ///
166    /// # Client::_test(|_, _, response| {
167    /// let response: LocalResponse = response;
168    /// let task = response.into_msgpack::<Task>();
169    /// # });
170    /// ```
171    #[cfg(feature = "msgpack")]
172    #[cfg_attr(nightly, doc(cfg(feature = "msgpack")))]
173    pub $($prefix)? fn into_msgpack<T>(self) -> Option<T>
174        where T: Send + serde::de::DeserializeOwned + 'static
175    {
176        if self._response().body().is_none() {
177            return None;
178        }
179
180        self._into_msgpack() $(.$suffix)?
181    }
182
183    #[cfg(test)]
184    #[allow(dead_code)]
185    fn _ensure_impls_exist() {
186        fn is_debug<T: std::fmt::Debug>() {}
187        is_debug::<Self>();
188    }
189}}