yeti_types/resource/
response_body.rs1use bytes::Bytes;
4use futures::stream::Stream;
5use std::pin::Pin;
6
7use super::realtime::Subscription;
8use crate::content_type::ContentType;
9use crate::error::Result;
10
11pub enum ResponseBody {
13 Complete(Vec<u8>),
15 Stream(Pin<Box<dyn Stream<Item = Result<Bytes>> + Send>>),
17 Subscription(Subscription, ContentType),
22}
23
24impl std::fmt::Debug for ResponseBody {
25 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
26 match self {
27 Self::Complete(data) => f
28 .debug_tuple("Complete")
29 .field(&format!("{} bytes", data.len()))
30 .finish(),
31 Self::Stream(_) => f.debug_tuple("Stream").field(&"<stream>").finish(),
32 Self::Subscription(_, format) => f
33 .debug_tuple("Subscription")
34 .field(&"<subscription>")
35 .field(format)
36 .finish(),
37 }
38 }
39}
40
41impl ResponseBody {
42 #[must_use]
44 pub const fn complete(data: Vec<u8>) -> Self {
45 Self::Complete(data)
46 }
47
48 pub fn stream<S>(stream: S) -> Self
50 where
51 S: Stream<Item = Result<Bytes>> + Send + 'static,
52 {
53 Self::Stream(Box::pin(stream))
54 }
55
56 #[must_use]
59 pub const fn subscription(sub: Subscription, format: ContentType) -> Self {
60 Self::Subscription(sub, format)
61 }
62
63 #[must_use]
65 pub fn into_bytes(self) -> Option<Vec<u8>> {
66 match self {
67 Self::Complete(data) => Some(data),
68 Self::Stream(_) | Self::Subscription(..) => None,
69 }
70 }
71
72 #[must_use]
74 pub const fn as_bytes(&self) -> Option<&[u8]> {
75 match self {
76 Self::Complete(data) => Some(data.as_slice()),
77 Self::Stream(_) | Self::Subscription(..) => None,
78 }
79 }
80}