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