Skip to main content

nakadi_types/event/
mod.rs

1//! Types for handling events
2//!
3//! Consumable and publishable event templates
4
5pub use crate::{event_type::EventTypeName, partition::PartitionId, FlowId};
6
7use chrono::{DateTime, Utc};
8use serde::{Deserialize, Serialize};
9use uuid::Uuid;
10
11pub mod publishable;
12
13new_type! {
14    #[doc="Identifier for an event.\n\nSometimes also called EID."]
15    #[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
16    pub copy struct EventId(Uuid);
17}
18
19/// Shortcut for creating a new random `EventId`
20pub struct Eid;
21
22impl From<Eid> for EventId {
23    fn from(_: Eid) -> Self {
24        Self::random()
25    }
26}
27
28impl EventId {
29    /// Generate a new random `EventId` from a version 4 UUID
30    pub fn random() -> Self {
31        Self::new(Uuid::new_v4())
32    }
33}
34
35new_type! {
36    #[doc=""]
37    #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
38    pub struct DataType(String);
39}
40
41/// The type of operation executed on the entity.
42#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
43pub enum DataOp {
44    #[serde(rename = "C")]
45    Creation,
46    #[serde(rename = "U")]
47    Update,
48    #[serde(rename = "D")]
49    Deletion,
50    #[serde(rename = "S")]
51    Snapshot,
52}
53
54/// A `DataChangeEvent` template for consumption of events
55///
56/// Represents a change on a resource. Also contains indicators
57/// for the data type and the type of operation performed.
58///
59/// See also [Nakadi Manual](https://nakadi.io/manual.html#definition_DataChangeEvent)
60#[derive(Debug, Clone, Deserialize)]
61pub struct DataChangeEvent<T> {
62    /// The payload of the type
63    pub data: T,
64    pub data_type: DataType,
65    pub data_op: DataOp,
66    pub metadata: EventMetaData,
67}
68
69/// A `BusinessEvent` template for consumption of events
70///
71/// See also [Nakadi Manual](https://nakadi.io/manual.html#definition_BusinessEvent)
72#[derive(Debug, Clone, Deserialize)]
73pub struct BusinessEvent<T> {
74    #[serde(flatten)]
75    pub data: T,
76    pub metadata: EventMetaData,
77}
78
79/// Metadata of an event
80///
81/// See also [Nakadi Manual](https://nakadi.io/manual.html#definition_EventMetadata)
82#[derive(Debug, Clone, Deserialize)]
83pub struct EventMetaData {
84    /// Identifier of this Event.
85    ///
86    /// Clients MUST generate this value and it SHOULD be guaranteed to be unique from the
87    /// perspective of the producer. Consumers MIGHT use this value to assert uniqueness of
88    /// reception of the Event.
89    pub eid: EventId,
90    /// The EventType of this Event
91    pub event_type: EventTypeName,
92    /// Timestamp of creation of the Event generated by the producer.
93    pub occurred_at: DateTime<Utc>,
94    /// Timestamp of the reception of the Event by Nakadi. This is enriched upon reception of
95    /// the Event.
96    pub received_at: DateTime<Utc>,
97    #[serde(default)]
98    /// Event identifier of the Event that caused the generation of this Event.
99    /// Set by the producer.
100    pub parent_eids: Vec<EventId>,
101    /// Indicates the partition assigned to this Event.
102    pub partition: PartitionId,
103    #[serde(default)]
104    pub version: String,
105    pub flow_id: FlowId,
106}