use crate::courierust_bytes::Bytes;
use alloc::string::String;
use alloc::vec::Vec;
#[derive(Clone, Default, PartialEq, Eq, Hash)]
pub enum Body {
#[default]
Empty,
Bytes(Bytes),
}
impl Body {
#[inline]
pub fn is_empty(&self) -> bool {
match self {
Self::Empty => true,
Self::Bytes(b) => b.is_empty(),
}
}
#[inline]
pub fn len(&self) -> usize {
match self {
Self::Empty => 0,
Self::Bytes(b) => b.len(),
}
}
#[inline]
pub fn into_bytes(self) -> Option<Bytes> {
match self {
Self::Empty => None,
Self::Bytes(b) => Some(b),
}
}
#[inline]
pub fn as_bytes(&self) -> Option<&[u8]> {
match self {
Self::Empty => None,
Self::Bytes(b) => Some(b.as_slice()),
}
}
}
impl From<Bytes> for Body {
#[inline]
fn from(b: Bytes) -> Self {
if b.is_empty() {
Self::Empty
} else {
Self::Bytes(b)
}
}
}
impl From<Vec<u8>> for Body {
#[inline]
fn from(v: Vec<u8>) -> Self {
Self::from(Bytes::from(v))
}
}
impl From<&'static [u8]> for Body {
#[inline]
fn from(b: &'static [u8]) -> Self {
Self::from(Bytes::from_static(b))
}
}
impl From<&'static str> for Body {
#[inline]
fn from(s: &'static str) -> Self {
Self::from(Bytes::from_static(s.as_bytes()))
}
}
impl From<String> for Body {
#[inline]
fn from(s: String) -> Self {
Self::from(Bytes::from(s))
}
}
impl core::fmt::Debug for Body {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::Empty => write!(f, "Body::Empty"),
Self::Bytes(b) => write!(f, "Body::Bytes({} bytes)", b.len()),
}
}
}