Skip to main content

rustack_logs_http/
body.rs

1//! CloudWatch Logs 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 CloudWatch Logs HTTP responses.
12///
13/// All CloudWatch Logs responses are either JSON (buffered) or empty.
14#[derive(Debug, Default)]
15pub enum LogsResponseBody {
16    /// A fully buffered response body (JSON payload).
17    Buffered(Full<Bytes>),
18    /// An empty body.
19    #[default]
20    Empty,
21}
22
23impl LogsResponseBody {
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 an empty response body.
31    #[must_use]
32    pub fn empty() -> Self {
33        Self::Empty
34    }
35
36    /// Create a response body from a JSON-serialized value.
37    #[must_use]
38    pub fn from_json(json: Vec<u8>) -> Self {
39        Self::Buffered(Full::new(Bytes::from(json)))
40    }
41}
42
43impl http_body::Body for LogsResponseBody {
44    type Data = Bytes;
45    type Error = std::io::Error;
46
47    fn poll_frame(
48        self: Pin<&mut Self>,
49        cx: &mut Context<'_>,
50    ) -> Poll<Option<Result<http_body::Frame<Self::Data>, Self::Error>>> {
51        match self.get_mut() {
52            Self::Buffered(full) => Pin::new(full)
53                .poll_frame(cx)
54                .map_err(|never| match never {}),
55            Self::Empty => Poll::Ready(None),
56        }
57    }
58
59    fn is_end_stream(&self) -> bool {
60        match self {
61            Self::Buffered(full) => full.is_end_stream(),
62            Self::Empty => true,
63        }
64    }
65
66    fn size_hint(&self) -> http_body::SizeHint {
67        match self {
68            Self::Buffered(full) => full.size_hint(),
69            Self::Empty => http_body::SizeHint::with_exact(0),
70        }
71    }
72}