Skip to main content

maxwell_cdc/
message.rs

1//! The message enum and its `type` tag.
2
3use crate::row::{ControlMessage, RowChange};
4use crate::schema::{
5    DatabaseChange, DatabaseDropChange, TableAlterChange, TableCreateChange, TableDropChange,
6};
7use serde::{Deserialize, Serialize};
8
9/// The operation type for row change messages.
10///
11/// Non-exhaustive for the same reason as [`Message`]: a new row-carrying message type would
12/// otherwise make [`Message::op_type`] return a variant that breaks existing matches.
13#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
14#[serde(rename_all = "kebab-case")]
15#[non_exhaustive]
16pub enum OpType {
17    /// Row insertion.
18    Insert,
19    /// Row update.
20    Update,
21    /// Row deletion.
22    Delete,
23    /// Bootstrap row insert (initial snapshot).
24    BootstrapInsert,
25}
26
27/// A Maxwell CDC message parsed from JSON.
28///
29/// Non-exhaustive: Maxwell has added message types before, and a caller matching on this
30/// enum must carry a catch-all arm so a new one is not a breaking change here.
31#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
32#[serde(tag = "type", rename_all = "kebab-case")]
33#[non_exhaustive]
34pub enum Message {
35    /// Row insertion into a table.
36    Insert(RowChange),
37    /// Row update in a table.
38    Update(RowChange),
39    /// Row deletion from a table.
40    Delete(RowChange),
41    /// Row inserted during bootstrap (initial snapshot).
42    BootstrapInsert(RowChange),
43    /// Start of bootstrap period for a table.
44    BootstrapStart(ControlMessage),
45    /// End of bootstrap period for a table.
46    BootstrapComplete(ControlMessage),
47    /// Table created.
48    TableCreate(TableCreateChange),
49    /// Table altered.
50    TableAlter(TableAlterChange),
51    /// Table dropped.
52    TableDrop(TableDropChange),
53    /// Database created.
54    DatabaseCreate(DatabaseChange),
55    /// Database altered.
56    DatabaseAlter(DatabaseChange),
57    /// Database dropped.
58    DatabaseDrop(DatabaseDropChange),
59}
60
61impl Message {
62    /// The operation type if this is a row change, else `None`. Exhaustive, so a new
63    /// row-carrying variant fails to compile here instead of silently reporting nothing.
64    #[must_use]
65    pub const fn op_type(&self) -> Option<OpType> {
66        match self {
67            Self::Insert(_) => Some(OpType::Insert),
68            Self::Update(_) => Some(OpType::Update),
69            Self::Delete(_) => Some(OpType::Delete),
70            Self::BootstrapInsert(_) => Some(OpType::BootstrapInsert),
71            Self::BootstrapStart(_)
72            | Self::BootstrapComplete(_)
73            | Self::TableCreate(_)
74            | Self::TableAlter(_)
75            | Self::TableDrop(_)
76            | Self::DatabaseCreate(_)
77            | Self::DatabaseAlter(_)
78            | Self::DatabaseDrop(_) => None,
79        }
80    }
81
82    /// The `type` tag Maxwell writes for this variant. Exhaustive, so a new variant cannot
83    /// be added without deciding its tag.
84    #[must_use]
85    pub const fn tag(&self) -> &'static str {
86        match self {
87            Self::Insert(_) => "insert",
88            Self::Update(_) => "update",
89            Self::Delete(_) => "delete",
90            Self::BootstrapInsert(_) => "bootstrap-insert",
91            Self::BootstrapStart(_) => "bootstrap-start",
92            Self::BootstrapComplete(_) => "bootstrap-complete",
93            Self::TableCreate(_) => "table-create",
94            Self::TableAlter(_) => "table-alter",
95            Self::TableDrop(_) => "table-drop",
96            Self::DatabaseCreate(_) => "database-create",
97            Self::DatabaseAlter(_) => "database-alter",
98            Self::DatabaseDrop(_) => "database-drop",
99        }
100    }
101}