Skip to main content

rustack_ses_http/
body.rs

1//! SES HTTP response body type.
2
3use std::{
4    pin::Pin,
5    task::{Context, Poll},
6};
7
8use bytes::Bytes;
9use http_body_util::Full;
10
11/// Response body for SES HTTP responses.
12///
13/// SES responses are either XML (v1 awsQuery), JSON (v2 restJson1), or empty.
14#[derive(Debug, Default)]
15pub enum SesResponseBody {
16    /// A fully buffered response body.
17    Buffered(Full<Bytes>),
18    /// An empty body.
19    #[default]
20    Empty,
21}
22
23impl SesResponseBody {
24    /// Create a response body from raw bytes.
25    #[must_use]
26    pub fn from_bytes(data: impl Into<Bytes>) -> Self {
27        Self::Buffered(Full::new(data.into()))
28    }
29
30    /// Create a response body from XML bytes.
31    #[must_use]
32    pub fn from_xml(xml: Vec<u8>) -> Self {
33        Self::Buffered(Full::new(Bytes::from(xml)))
34    }
35
36    /// Create a response body from a JSON string.
37    #[must_use]
38    pub fn from_json(json: String) -> Self {
39        Self::Buffered(Full::new(Bytes::from(json)))
40    }
41
42    /// Create an empty response body.
43    #[must_use]
44    pub fn empty() -> Self {
45        Self::Empty
46    }
47}
48
49impl http_body::Body for SesResponseBody {
50    type Data = Bytes;
51    type Error = std::io::Error;
52
53    fn poll_frame(
54        self: Pin<&mut Self>,
55        cx: &mut Context<'_>,
56    ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
57        match self.get_mut() {
58            Self::Buffered(full) => Pin::new(full)
59                .poll_frame(cx)
60                .map_err(|never| match never {}),
61            Self::Empty => Poll::Ready(None),
62        }
63    }
64
65    fn is_end_stream(&self) -> bool {
66        match self {
67            Self::Buffered(full) => full.is_end_stream(),
68            Self::Empty => true,
69        }
70    }
71
72    fn size_hint(&self) -> http_body::SizeHint {
73        match self {
74            Self::Buffered(full) => full.size_hint(),
75            Self::Empty => http_body::SizeHint::with_exact(0),
76        }
77    }
78}