Skip to main content

nakadi_types/event/
publishable.rs

1//! Publishable events
2pub use crate::{event_type::EventTypeName, partition::PartitionId, FlowId};
3
4use chrono::{DateTime, Utc};
5use serde::Serialize;
6
7pub use super::{DataOp, DataType, EventId};
8
9/// A `DataChangeEvent` template for publishing of events
10///
11/// See also [Nakadi Manual](https://nakadi.io/manual.html#definition_AuthorizationAttribute)
12#[derive(Debug, Clone, Serialize)]
13pub struct DataChangeEventPub<T> {
14    pub data: T,
15    pub data_type: DataType,
16    pub data_op: DataOp,
17    pub metadata: EventMetaDataPub,
18}
19
20impl<T> From<super::DataChangeEvent<T>> for DataChangeEventPub<T> {
21    fn from(e: super::DataChangeEvent<T>) -> Self {
22        Self {
23            data: e.data,
24            data_type: e.data_type,
25            data_op: e.data_op,
26            metadata: e.metadata.into(),
27        }
28    }
29}
30
31/// A `BusinessEvent` template for publishing of events
32///
33/// See also [Nakadi Manual](https://nakadi.io/manual.html#definition_DataChangeEvent)
34#[derive(Debug, Clone, Serialize)]
35pub struct BusinessEventPub<T> {
36    #[serde(flatten)]
37    pub data: T,
38    pub metadata: EventMetaDataPub,
39}
40
41impl<T> From<super::BusinessEvent<T>> for BusinessEventPub<T> {
42    fn from(e: super::BusinessEvent<T>) -> Self {
43        Self {
44            data: e.data,
45            metadata: e.metadata.into(),
46        }
47    }
48}
49
50/// Metadata of an event
51///
52/// See also [Nakadi Manual](https://nakadi.io/manual.html#definition_EventMetadata)
53#[derive(Debug, Clone, Serialize)]
54pub struct EventMetaDataPub {
55    /// Identifier of this Event.
56    pub eid: EventId,
57    /// The EventType of this Event. This is enriched by Nakadi on reception of the Event
58    /// based on the endpoint where the Producer sent the Event to.
59    ///
60    /// If provided MUST match the endpoint. Failure to do so will cause rejection of the
61    /// Event.
62    #[serde(skip_serializing_if = "Option::is_none")]
63    pub event_type: Option<EventTypeName>,
64    /// Timestamp of creation of the Event generated by the producer.
65    pub occurred_at: DateTime<Utc>,
66    /// Event identifier of the Event that caused the generation of this Event.
67    /// Set by the producer.
68    #[serde(default)]
69    pub parent_eids: Vec<EventId>,
70    /// Indicates the partition assigned to this Event.
71    ///
72    /// Required to be set by the client if partition strategy of the EventType is
73    /// ‘user_defined’.
74    #[serde(skip_serializing_if = "Option::is_none")]
75    pub partition: Option<PartitionId>,
76    #[serde(skip_serializing_if = "Option::is_none")]
77    pub flow_id: Option<FlowId>,
78}
79
80impl EventMetaDataPub {
81    pub fn new<T: Into<EventId>>(eid: T) -> Self {
82        Self {
83            eid: eid.into(),
84            event_type: None,
85            occurred_at: Utc::now(),
86            parent_eids: Vec::new(),
87            partition: None,
88            flow_id: None,
89        }
90    }
91
92    pub fn random_eid() -> Self {
93        Self::new(EventId::random())
94    }
95
96    pub fn event_type<T: Into<EventTypeName>>(mut self, v: T) -> Self {
97        self.event_type = Some(v.into());
98        self
99    }
100
101    pub fn occurred_at<T: Into<DateTime<Utc>>>(mut self, v: T) -> Self {
102        self.occurred_at = v.into();
103        self
104    }
105
106    pub fn parent_eid<T: Into<EventId>>(mut self, v: T) -> Self {
107        self.parent_eids.push(v.into());
108        self
109    }
110
111    pub fn partition<T: Into<PartitionId>>(mut self, v: T) -> Self {
112        self.partition = Some(v.into());
113        self
114    }
115
116    pub fn flow_id<T: Into<FlowId>>(mut self, v: T) -> Self {
117        self.flow_id = Some(v.into());
118        self
119    }
120}
121
122impl From<super::EventMetaData> for EventMetaDataPub {
123    fn from(m: super::EventMetaData) -> Self {
124        Self {
125            eid: m.eid,
126            event_type: Some(m.event_type),
127            occurred_at: m.occurred_at,
128            parent_eids: m.parent_eids,
129            partition: None,
130            flow_id: Some(m.flow_id),
131        }
132    }
133}