use std::pin::Pin;
use std::task::{Context, Poll};
use bytes::Bytes;
use http_body::Frame;
pub type BoxError = Box<dyn std::error::Error + Send + Sync>;
pub type BoxBody = http_body_util::combinators::BoxBody<bytes::Bytes, BoxError>;
pub type Body = BoxBody;
pub type Request<B = BoxBody> = http::Request<B>;
pub type Response<B = BoxBody> = http::Response<B>;
struct VolterBody(Option<Bytes>);
impl http_body::Body for VolterBody {
type Data = Bytes;
type Error = BoxError;
fn poll_frame(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Option<Result<Frame<Self::Data>, Self::Error>>> {
if let Some(data) = self.get_mut().0.take() {
Poll::Ready(Some(Ok(Frame::data(data))))
} else {
Poll::Ready(None)
}
}
fn is_end_stream(&self) -> bool {
self.0.is_none()
}
fn size_hint(&self) -> http_body::SizeHint {
let mut hint = http_body::SizeHint::new();
if let Some(ref data) = self.0 {
hint.set_exact(data.len() as u64);
}
hint
}
}
pub fn empty_body() -> BoxBody {
BoxBody::new(VolterBody(None))
}
pub fn full_body(bytes: Bytes) -> BoxBody {
BoxBody::new(VolterBody(Some(bytes)))
}