Skip to content

Commit

Permalink
feat: Add util functions to ReqBody (#442)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrislearn authored Oct 5, 2023
1 parent 4412d5f commit 4469241
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 26 deletions.
47 changes: 38 additions & 9 deletions crates/core/src/http/body/req.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,35 @@ pub enum ReqBody {
Once(Bytes),
/// Hyper default body.
Hyper(Incoming),
/// Inner body.
/// Boxed body.
Boxed(Pin<Box<dyn Body<Data = Bytes, Error = BoxedError> + Send + Sync + 'static>>),
}
impl fmt::Debug for ReqBody {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ReqBody::None => f.debug_tuple("ReqBody::None").finish(),
ReqBody::Once(_) => f.debug_tuple("ReqBody::Once").finish(),
ReqBody::Hyper(_) => f.debug_tuple("ReqBody::Hyper").finish(),
ReqBody::Boxed(_) => f.debug_tuple("ReqBody::Boxed").finish(),
}
impl ReqBody {
/// Check is that body is not set.
#[inline]
pub fn is_none(&self) -> bool {
matches!(*self, Self::None)
}
/// Check is that body is once.
#[inline]
pub fn is_once(&self) -> bool {
matches!(*self, Self::Once(_))
}
/// Check is that body is hyper default body type.
#[inline]
pub fn is_hyper(&self) -> bool {
matches!(*self, Self::Hyper(_))
}
/// Check is that body is stream.
#[inline]
pub fn is_boxed(&self) -> bool {
matches!(*self, Self::Boxed(_))
}

/// Set body to none and returns current body.
#[inline]
pub fn take(&mut self) -> Self {
std::mem::replace(self, Self::None)
}
}

Expand Down Expand Up @@ -208,3 +226,14 @@ cfg_feature! {
}
}
}

impl fmt::Debug for ReqBody {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self {
ReqBody::None => f.debug_tuple("ReqBody::None").finish(),
ReqBody::Once(_) => f.debug_tuple("ReqBody::Once").finish(),
ReqBody::Hyper(_) => f.debug_tuple("ReqBody::Hyper").finish(),
ReqBody::Boxed(_) => f.debug_tuple("ReqBody::Boxed").finish(),
}
}
}
39 changes: 22 additions & 17 deletions crates/core/src/http/body/res.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum ResBody {
Chunks(VecDeque<Bytes>),
/// Hyper default body.
Hyper(Incoming),
/// Inner body.
/// Boxed body.
Boxed(Pin<Box<dyn Body<Data = Bytes, Error = BoxedError> + Send + Sync + 'static>>),
/// Stream body.
Stream(SyncWrapper<BoxStream<'static, Result<Bytes, BoxedError>>>),
Expand All @@ -41,31 +41,36 @@ impl ResBody {
/// Check is that body is not set.
#[inline]
pub fn is_none(&self) -> bool {
matches!(*self, ResBody::None)
matches!(*self, Self::None)
}
/// Check is that body is once.
#[inline]
pub fn is_once(&self) -> bool {
matches!(*self, ResBody::Once(_))
matches!(*self, Self::Once(_))
}
/// Check is that body is chunks.
#[inline]
pub fn is_chunks(&self) -> bool {
matches!(*self, ResBody::Chunks(_))
matches!(*self, Self::Chunks(_))
}
/// Check is that body is hyper default body type.
#[inline]
pub fn is_hyper(&self) -> bool {
matches!(*self, Self::Hyper(_))
}
/// Check is that body is stream.
#[inline]
pub fn is_boxed(&self) -> bool {
matches!(*self, ResBody::Boxed(_))
matches!(*self, Self::Boxed(_))
}
/// Check is that body is stream.
#[inline]
pub fn is_stream(&self) -> bool {
matches!(*self, ResBody::Stream(_))
matches!(*self, Self::Stream(_))
}
/// Check is that body is error will be process in catcher.
pub fn is_error(&self) -> bool {
matches!(*self, ResBody::Error(_))
matches!(*self, Self::Error(_))
}

/// Wrap a futures `Stream` in a box inside `Body`.
Expand All @@ -76,27 +81,27 @@ impl ResBody {
E: Into<BoxedError> + 'static,
{
let mapped = stream.map_ok(Into::into).map_err(Into::into);
ResBody::Stream(SyncWrapper::new(Box::pin(mapped)))
Self::Stream(SyncWrapper::new(Box::pin(mapped)))
}

/// Get body's size.
#[inline]
pub fn size(&self) -> Option<u64> {
match self {
ResBody::None => Some(0),
ResBody::Once(bytes) => Some(bytes.len() as u64),
ResBody::Chunks(chunks) => Some(chunks.iter().map(|bytes| bytes.len() as u64).sum()),
ResBody::Hyper(_) => None,
ResBody::Boxed(_) => None,
ResBody::Stream(_) => None,
ResBody::Error(_) => None,
Self::None => Some(0),
Self::Once(bytes) => Some(bytes.len() as u64),
Self::Chunks(chunks) => Some(chunks.iter().map(|bytes| bytes.len() as u64).sum()),
Self::Hyper(_) => None,
Self::Boxed(_) => None,
Self::Stream(_) => None,
Self::Error(_) => None,
}
}

/// Set body to none and returns current body.
#[inline]
pub fn take(&mut self) -> ResBody {
std::mem::replace(self, ResBody::None)
pub fn take(&mut self) -> Self {
std::mem::replace(self, Self::None)
}
}

Expand Down

0 comments on commit 4469241

Please sign in to comment.