use http::HeaderMap;
#[derive(Debug)]
pub struct Frame<D> {
pub(super) kind: Kind<D>,
}
#[derive(Debug)]
pub(super) enum Kind<D> {
Data(D),
Trailers(HeaderMap),
}
impl<D> Frame<D> {
#[inline]
pub fn data(data: D) -> Self {
Self { kind: Kind::Data(data) }
}
#[inline]
pub fn trailers(trailers: HeaderMap) -> Self {
Self {
kind: Kind::Trailers(trailers),
}
}
#[inline]
pub fn data_ref(&self) -> Option<&D> {
match &self.kind {
Kind::Data(data) => Some(data),
Kind::Trailers(_) => None,
}
}
#[inline]
pub fn trailers_ref(&self) -> Option<&HeaderMap> {
match &self.kind {
Kind::Data(_) => None,
Kind::Trailers(trailers) => Some(trailers),
}
}
#[inline]
pub fn into_data(self) -> Result<D, Self> {
match self.kind {
Kind::Data(data) => Ok(data),
Kind::Trailers(_) => Err(self),
}
}
#[inline]
pub fn into_trailers(self) -> Result<HeaderMap, Self> {
match self.kind {
Kind::Data(_) => Err(self),
Kind::Trailers(trailers) => Ok(trailers),
}
}
#[inline]
pub fn map_data<T>(self, map: impl FnOnce(D) -> T) -> Frame<T> {
match self.kind {
Kind::Data(data) => Frame::data(map(data)),
Kind::Trailers(trailers) => Frame::trailers(trailers),
}
}
}