saluki_core/data_model/payload/
mod.rs

1//! Output payloads.
2
3use std::fmt;
4
5use bitmask_enum::bitmask;
6
7mod grpc;
8pub use self::grpc::GrpcPayload;
9
10mod http;
11pub use self::http::HttpPayload;
12use crate::topology::interconnect::Dispatchable;
13
14mod metadata;
15pub use self::metadata::PayloadMetadata;
16
17mod raw;
18pub use self::raw::RawPayload;
19
20/// Output payload type.
21///
22/// This type is a bitmask, which means different payload types can be combined together. This makes `PayloadType` mainly
23/// useful for defining the type of output payloads that a component emits, or can handle.
24#[bitmask(u8)]
25#[bitmask_config(vec_debug)]
26pub enum PayloadType {
27    /// Raw.
28    Raw,
29
30    /// HTTP.
31    Http,
32
33    /// gRPC.
34    Grpc,
35}
36
37impl Default for PayloadType {
38    fn default() -> Self {
39        Self::none()
40    }
41}
42
43impl fmt::Display for PayloadType {
44    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
45        let mut types = Vec::new();
46
47        if self.contains(Self::Raw) {
48            types.push("Raw");
49        }
50
51        if self.contains(Self::Http) {
52            types.push("HTTP");
53        }
54
55        if self.contains(Self::Grpc) {
56            types.push("gRPC");
57        }
58
59        write!(f, "{}", types.join("|"))
60    }
61}
62
63/// An output payload.
64#[derive(Clone)]
65#[allow(clippy::large_enum_variant)]
66pub enum Payload {
67    /// A raw payload.
68    ///
69    /// The payload is an opaque collection of bytes.
70    Raw(RawPayload),
71
72    /// An HTTP payload.
73    ///
74    /// Includes the relevant HTTP parameters (host, path, method, headers) and the payload body.
75    Http(HttpPayload),
76
77    /// A gRPC payload.
78    ///
79    /// Includes the gRPC endpoint, service path, and protobuf-encoded request body.
80    Grpc(GrpcPayload),
81}
82
83impl Payload {
84    /// Gets the type of this payload.
85    pub fn payload_type(&self) -> PayloadType {
86        match self {
87            Payload::Raw(_) => PayloadType::Raw,
88            Payload::Http(_) => PayloadType::Http,
89            Payload::Grpc(_) => PayloadType::Grpc,
90        }
91    }
92
93    /// Returns the inner payload value, if this event is a `RawPayload`.
94    ///
95    /// Otherwise, `None` is returned and the original payload is consumed.
96    pub fn try_into_raw(self) -> Option<RawPayload> {
97        match self {
98            Payload::Raw(payload) => Some(payload),
99            _ => None,
100        }
101    }
102
103    /// Returns the inner payload value, if this event is an `HttpPayload`.
104    ///
105    /// Otherwise, `None` is returned and the original payload is consumed.
106    pub fn try_into_http_payload(self) -> Option<HttpPayload> {
107        match self {
108            Payload::Http(payload) => Some(payload),
109            _ => None,
110        }
111    }
112
113    /// Returns the inner payload value, if this event is a `GrpcPayload`.
114    ///
115    /// Otherwise, `None` is returned and the original payload is consumed.
116    pub fn try_into_grpc_payload(self) -> Option<GrpcPayload> {
117        match self {
118            Payload::Grpc(payload) => Some(payload),
119            _ => None,
120        }
121    }
122}
123
124impl Dispatchable for Payload {
125    fn item_count(&self) -> usize {
126        1
127    }
128}