mod stream;
#[cfg(all(test, not(target_arch = "wasm32")))]
mod tests;
use bytes::Bytes;
use http_body_util::BodyExt;
#[cfg(not(target_arch = "wasm32"))]
use std::pin::Pin;
use crate::error::Error;
#[cfg(not(target_arch = "wasm32"))]
pub use stream::BodyStreamLocal;
pub use stream::BodyStreamSend;
pub type RequestBodySend = http_body_util::combinators::UnsyncBoxBody<Bytes, Error>;
#[cfg(not(target_arch = "wasm32"))]
pub type RequestBodyLocal = Pin<Box<dyn http_body::Body<Data = Bytes, Error = Error> + 'static>>;
#[cfg(not(target_arch = "wasm32"))]
pub type ResponseBodyLocal = Pin<Box<dyn http_body::Body<Data = Bytes, Error = Error> + 'static>>;
pub enum RequestBody {
Buffered(Bytes),
#[cfg(not(target_arch = "wasm32"))]
Streaming(RequestBodySend),
}
impl std::fmt::Debug for RequestBody {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
RequestBody::Buffered(_) => f.debug_tuple("Buffered").field(&"..").finish(),
#[cfg(not(target_arch = "wasm32"))]
RequestBody::Streaming(_) => f.debug_tuple("Streaming").field(&"..").finish(),
}
}
}
impl RequestBody {
pub(crate) fn into_hyper_body(self) -> RequestBodySend {
match self {
RequestBody::Buffered(b) => http_body_util::Full::new(b)
.map_err(|never| match never {})
.boxed_unsync(),
#[cfg(not(target_arch = "wasm32"))]
RequestBody::Streaming(body) => body,
}
}
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn into_local_body(self) -> RequestBodyLocal {
match self {
RequestBody::Buffered(b) => {
Box::pin(http_body_util::Full::new(b).map_err(|never| match never {}))
}
RequestBody::Streaming(body) => Box::pin(body),
}
}
pub fn try_clone(&self) -> Option<Self> {
match self {
RequestBody::Buffered(b) => Some(RequestBody::Buffered(b.clone())),
#[cfg(not(target_arch = "wasm32"))]
RequestBody::Streaming(_) => None,
}
}
}
impl From<Bytes> for RequestBody {
fn from(b: Bytes) -> Self {
RequestBody::Buffered(b)
}
}
impl From<Vec<u8>> for RequestBody {
fn from(v: Vec<u8>) -> Self {
RequestBody::Buffered(Bytes::from(v))
}
}
impl From<String> for RequestBody {
fn from(s: String) -> Self {
RequestBody::Buffered(Bytes::from(s))
}
}
impl From<&'static str> for RequestBody {
fn from(s: &'static str) -> Self {
RequestBody::Buffered(Bytes::from_static(s.as_bytes()))
}
}
impl From<&'static [u8]> for RequestBody {
fn from(s: &'static [u8]) -> Self {
RequestBody::Buffered(Bytes::from_static(s))
}
}
#[cfg(not(target_arch = "wasm32"))]
impl From<RequestBodySend> for RequestBody {
fn from(body: RequestBodySend) -> Self {
RequestBody::Streaming(body)
}
}