Skip to content

Commit

Permalink
feat(http-body): Change return type of Frame::into_* methods from Opt…
Browse files Browse the repository at this point in the history
…ion<T> to Result<T, Self>

Fixes #77
BREAKING CHANGE: Frame::into_* methods now return Result<T, Self> instead of Option<T>
  • Loading branch information
oliviacrain authored and seanmonstar committed Dec 20, 2022
1 parent e856e57 commit 661e4ea
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
15 changes: 9 additions & 6 deletions http-body-util/src/collected.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,21 @@ impl<B: Buf> Collected<B> {
}

pub(crate) fn push_frame(&mut self, frame: Frame<B>) {
if frame.is_data() {
let data = frame.into_data().unwrap();
self.bufs.push(data);
} else if frame.is_trailers() {
let trailers = frame.into_trailers().unwrap();
let frame = match frame.into_data() {
Ok(data) => {
self.bufs.push(data);
return;
}
Err(frame) => frame,
};

if let Ok(trailers) = frame.into_trailers() {
if let Some(current) = &mut self.trailers {
current.extend(trailers.into_iter());
} else {
self.trailers = Some(trailers);
}
}
};
}
}

Expand Down
18 changes: 10 additions & 8 deletions http-body/src/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ impl<T> Frame<T> {

/// Consumes self into the buf of the DATA frame.
///
/// Check `Frame::is_data` before to determine if the frame is DATA.
pub fn into_data(self) -> Option<T> {
/// Returns an [`Err`] containing the original [`Frame`] when frame is not a DATA frame.
/// `Frame::is_data` can also be used to determine if the frame is a DATA frame.
pub fn into_data(self) -> Result<T, Self> {
match self.kind {
Kind::Data(data) => Some(data),
_ => None,
Kind::Data(data) => Ok(data),
_ => Err(self),
}
}

Expand Down Expand Up @@ -73,11 +74,12 @@ impl<T> Frame<T> {

/// Consumes self into the buf of the trailers frame.
///
/// Check `Frame::is_trailers` before to determine if the frame is a trailers frame.
pub fn into_trailers(self) -> Option<HeaderMap> {
/// Returns an [`Err`] containing the original [`Frame`] when frame is not a trailers frame.
/// `Frame::is_trailers` can also be used to determine if the frame is a trailers frame.
pub fn into_trailers(self) -> Result<HeaderMap, Self> {
match self.kind {
Kind::Trailers(trailers) => Some(trailers),
_ => None,
Kind::Trailers(trailers) => Ok(trailers),
_ => Err(self),
}
}

Expand Down

0 comments on commit 661e4ea

Please sign in to comment.