use std::{
pin::Pin,
task::{Context, Poll},
};
use bytes::Bytes;
use http_body_util::Full;
#[derive(Debug, Default)]
pub enum SesResponseBody {
Buffered(Full<Bytes>),
#[default]
Empty,
}
impl SesResponseBody {
#[must_use]
pub fn from_bytes(data: impl Into<Bytes>) -> Self {
Self::Buffered(Full::new(data.into()))
}
#[must_use]
pub fn from_xml(xml: Vec<u8>) -> Self {
Self::Buffered(Full::new(Bytes::from(xml)))
}
#[must_use]
pub fn from_json(json: String) -> Self {
Self::Buffered(Full::new(Bytes::from(json)))
}
#[must_use]
pub fn empty() -> Self {
Self::Empty
}
}
impl http_body::Body for SesResponseBody {
type Data = Bytes;
type Error = std::io::Error;
fn poll_frame(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
match self.get_mut() {
Self::Buffered(full) => Pin::new(full)
.poll_frame(cx)
.map_err(|never| match never {}),
Self::Empty => Poll::Ready(None),
}
}
fn is_end_stream(&self) -> bool {
match self {
Self::Buffered(full) => full.is_end_stream(),
Self::Empty => true,
}
}
fn size_hint(&self) -> http_body::SizeHint {
match self {
Self::Buffered(full) => full.size_hint(),
Self::Empty => http_body::SizeHint::with_exact(0),
}
}
}