Skip to main content

saluki_core/data_model/payload/
mod.rs

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