use std::convert::Infallible;
use bytes::BufMut;
use crate::{api::error::IntoHttpError, serde::slice_to_buf};
pub trait OutgoingBody {
type Error: Into<IntoHttpError>;
fn content_type(&self) -> Option<http::HeaderValue>;
fn try_into_buf<T: Default + BufMut + AsRef<[u8]>>(self) -> Result<T, Self::Error>;
}
#[expect(clippy::exhaustive_structs)]
pub struct EmptyBody<const TRULY_EMPTY: bool = true>;
impl<const TRULY_EMPTY: bool> OutgoingBody for EmptyBody<TRULY_EMPTY> {
type Error = Infallible;
fn content_type(&self) -> Option<http::HeaderValue> {
if TRULY_EMPTY { None } else { Some(crate::http_headers::APPLICATION_JSON) }
}
fn try_into_buf<T: Default + BufMut + AsRef<[u8]>>(self) -> Result<T, Infallible> {
if TRULY_EMPTY { Ok(Default::default()) } else { Ok(slice_to_buf(b"{}")) }
}
}
#[expect(clippy::exhaustive_structs)]
pub struct BytesBody(pub Vec<u8>);
impl OutgoingBody for BytesBody {
type Error = Infallible;
fn content_type(&self) -> Option<http::HeaderValue> {
Some(crate::http_headers::APPLICATION_OCTET_STREAM)
}
fn try_into_buf<T: Default + BufMut + AsRef<[u8]>>(self) -> Result<T, Infallible> {
Ok(slice_to_buf(&self.0))
}
}