diff --git a/http-body-util/src/collected.rs b/http-body-util/src/collected.rs index 439ff01..34e3bf7 100644 --- a/http-body-util/src/collected.rs +++ b/http-body-util/src/collected.rs @@ -37,18 +37,21 @@ impl Collected { } pub(crate) fn push_frame(&mut self, frame: Frame) { - 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); } - } + }; } } diff --git a/http-body/src/frame.rs b/http-body/src/frame.rs index 6672abf..67fd2bb 100644 --- a/http-body/src/frame.rs +++ b/http-body/src/frame.rs @@ -38,11 +38,12 @@ impl Frame { /// 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 { + /// 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 { match self.kind { - Kind::Data(data) => Some(data), - _ => None, + Kind::Data(data) => Ok(data), + _ => Err(self), } } @@ -73,11 +74,12 @@ impl Frame { /// 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 { + /// 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 { match self.kind { - Kind::Trailers(trailers) => Some(trailers), - _ => None, + Kind::Trailers(trailers) => Ok(trailers), + _ => Err(self), } }