use crate::Error;
use futures_lite::AsyncRead;
use std::pin::Pin;
use trillium_http::ReceivedBody;
use trillium_macros::AsyncRead;
use trillium_server_common::Transport;
#[derive(AsyncRead, Debug)]
pub struct ResponseBody<'a>(ReceivedBody<'a, Box<dyn Transport>>);
impl ResponseBody<'_> {
pub async fn read_bytes(self) -> Result<Vec<u8>, Error> {
self.0.read_bytes().await
}
pub async fn read_string(self) -> Result<String, Error> {
self.0.read_string().await
}
#[must_use]
pub fn with_max_len(mut self, max_len: u64) -> Self {
self.0.set_max_len(max_len);
self
}
pub fn set_max_len(&mut self, max_len: u64) -> &mut Self {
self.0.set_max_len(max_len);
self
}
pub fn content_length(&self) -> Option<u64> {
self.0.content_length()
}
pub(crate) async fn drain(self) -> std::io::Result<u64> {
self.0.drain().await
}
}
impl<'a> From<ReceivedBody<'a, Box<dyn Transport>>> for ResponseBody<'a> {
fn from(received_body: ReceivedBody<'a, Box<dyn Transport>>) -> Self {
Self(received_body)
}
}
impl<'a> From<ResponseBody<'a>> for ReceivedBody<'a, Box<dyn Transport>> {
fn from(value: ResponseBody<'a>) -> Self {
value.0
}
}
impl<'a> IntoFuture for ResponseBody<'a> {
type IntoFuture = Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>;
type Output = trillium_http::Result<String>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.read_string().await })
}
}