use std::io;
use tokio::io::AsyncReadExt;
use crate::{Response, local::asynchronous, http::CookieJar};
use super::Client;
pub struct LocalResponse<'c> {
pub(in super) inner: asynchronous::LocalResponse<'c>,
pub(in super) client: &'c Client,
}
impl LocalResponse<'_> {
fn _response(&self) -> &Response<'_> {
&self.inner._response()
}
pub(crate) fn _cookies(&self) -> &CookieJar<'_> {
self.inner._cookies()
}
fn _into_string(self) -> io::Result<String> {
self.client.block_on(self.inner._into_string())
}
fn _into_bytes(self) -> io::Result<Vec<u8>> {
self.client.block_on(self.inner._into_bytes())
}
#[cfg(feature = "json")]
fn _into_json<T: Send + 'static>(self) -> Option<T>
where T: serde::de::DeserializeOwned
{
serde_json::from_reader(self).ok()
}
#[cfg(feature = "msgpack")]
fn _into_msgpack<T: Send + 'static>(self) -> Option<T>
where T: serde::de::DeserializeOwned
{
rmp_serde::from_read(self).ok()
}
pub_response_impl!("# use rocket::local::blocking::Client;\n\
use rocket::local::blocking::LocalResponse;");
}
impl io::Read for LocalResponse<'_> {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.client.block_on(self.inner.read(buf))
}
}
impl std::fmt::Debug for LocalResponse<'_> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self._response().fmt(f)
}
}