1use std::{fmt, future::poll_fn, mem, pin::Pin, task::Context, task::Poll};
2
3use crate::channel::bstream;
4use crate::http::{HeaderMap, error::PayloadError, h1, h2};
5use crate::util::{Bytes, Stream};
6
7pub type PayloadStream = Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>>>>;
9
10#[derive(Default)]
12pub enum Payload {
13 #[default]
15 None,
16 H1(h1::Payload),
18 H2(h2::Payload),
20 Stream(PayloadStream),
22}
23
24impl From<h1::Payload> for Payload {
25 fn from(v: h1::Payload) -> Self {
26 Payload::H1(v)
27 }
28}
29
30impl From<bstream::Receiver<PayloadError>> for Payload {
31 fn from(v: bstream::Receiver<PayloadError>) -> Self {
32 Payload::H1(v.into())
33 }
34}
35
36impl From<h2::Payload> for Payload {
37 fn from(v: h2::Payload) -> Self {
38 Payload::H2(v)
39 }
40}
41
42impl From<PayloadStream> for Payload {
43 fn from(pl: PayloadStream) -> Self {
44 Payload::Stream(pl)
45 }
46}
47
48impl fmt::Debug for Payload {
49 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50 match self {
51 Payload::None => write!(f, "Payload::None"),
52 Payload::H1(pl) => write!(f, "Payload::H1({pl:?})"),
53 Payload::H2(pl) => write!(f, "Payload::H2({pl:?})"),
54 Payload::Stream(_) => write!(f, "Payload::Stream(..)"),
55 }
56 }
57}
58
59impl Payload {
60 #[must_use]
61 pub fn take(&mut self) -> Self {
63 mem::take(self)
64 }
65
66 #[must_use]
67 pub fn from_stream<S>(stream: S) -> Self
72 where
73 S: Stream<Item = Result<Bytes, PayloadError>> + 'static,
74 {
75 Payload::Stream(Box::pin(stream))
76 }
77
78 #[inline]
79 pub async fn recv(&mut self) -> Option<Result<Bytes, PayloadError>> {
81 poll_fn(|cx| self.poll_recv(cx)).await
82 }
83
84 #[inline]
85 pub fn poll_recv(&mut self, cx: &mut Context<'_>) -> Poll<Option<Result<Bytes, PayloadError>>> {
90 match self {
91 Payload::None => Poll::Ready(None),
92 Payload::H1(pl) => pl.poll_read(cx),
93 Payload::H2(pl) => pl.poll_read(cx),
94 Payload::Stream(pl) => Pin::new(pl).poll_next(cx),
95 }
96 }
97
98 pub fn trailers(&self) -> Option<HeaderMap> {
103 match self {
104 Payload::H1(pl) => pl.trailers(),
105 Payload::H2(pl) => pl.trailers(),
106 _ => None,
107 }
108 }
109
110 pub fn take_trailers(&self) -> Option<HeaderMap> {
114 match self {
115 Payload::H1(pl) => pl.take_trailers(),
116 Payload::H2(pl) => pl.take_trailers(),
117 _ => None,
118 }
119 }
120}
121
122impl Stream for Payload {
123 type Item = Result<Bytes, PayloadError>;
124
125 #[inline]
126 fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
127 self.get_mut().poll_recv(cx)
128 }
129}
130
131#[cfg(test)]
132mod tests {
133 use super::*;
134
135 #[test]
140 fn payload_debug() {
141 assert!(format!("{:?}", Payload::None).contains("Payload::None"));
142 assert!(
143 format!(
144 "{:?}",
145 Payload::H1(crate::channel::bstream::channel().1.into())
146 )
147 .contains("Payload::H1")
148 );
149 assert!(
150 format!(
151 "{:?}",
152 Payload::Stream(Box::pin(crate::channel::bstream::channel().1))
153 )
154 .contains("Payload::Stream")
155 );
156
157 assert_eq!(
158 std::mem::size_of::<Payload>(),
159 std::mem::size_of::<Option<Payload>>()
160 );
161 }
162
163 #[crate::rt_test]
169 async fn payload_conversions() {
170 use crate::http::h1;
171
172 let (tx, rx) = crate::channel::bstream::channel();
173 let mut pl = Payload::from(h1::Payload::from(rx));
174 assert!(matches!(pl, Payload::H1(_)));
175 tx.feed_data(Bytes::from_static(b"data"));
176 tx.feed_eof();
177 assert_eq!(pl.recv().await.unwrap().unwrap(), "data");
178 assert!(pl.recv().await.is_none());
179 assert!(pl.trailers().is_none());
180
181 let mut pl = Payload::None;
182 assert!(pl.recv().await.is_none());
183 assert!(pl.trailers().is_none());
184
185 let (tx, rx) = crate::channel::bstream::channel();
186 let pl: PayloadStream = Box::pin(rx);
187 let mut pl = Payload::from(pl);
188 assert!(matches!(pl, Payload::Stream(_)));
189 tx.feed_eof();
190 assert!(pl.recv().await.is_none());
191 assert!(pl.trailers().is_none());
192 assert!(matches!(pl.take(), Payload::Stream(_)));
193 assert!(matches!(pl, Payload::None));
194 }
195}