use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
#[serde(rename_all = "snake_case")]
#[cfg_attr(feature = "cli", value(rename_all = "snake_case"))]
#[non_exhaustive]
pub enum EventType {
FormatDescription,
TableMap,
WriteRows,
UpdateRows,
DeleteRows,
PartialUpdateRows,
Query,
Xid,
Gtid,
AnonymousGtid,
PreviousGtids,
Rotate,
Stop,
RowsQuery,
Incident,
Intvar,
Rand,
UserVar,
BeginLoadQuery,
ExecuteLoadQuery,
Heartbeat,
Ignorable,
TransactionContext,
ViewChange,
XaPrepare,
TransactionPayload,
Unknown,
}
impl EventType {
pub fn as_str(self) -> &'static str {
match self {
Self::FormatDescription => "format_description",
Self::TableMap => "table_map",
Self::WriteRows => "write_rows",
Self::UpdateRows => "update_rows",
Self::DeleteRows => "delete_rows",
Self::PartialUpdateRows => "partial_update_rows",
Self::Query => "query",
Self::Xid => "xid",
Self::Gtid => "gtid",
Self::AnonymousGtid => "anonymous_gtid",
Self::PreviousGtids => "previous_gtids",
Self::Rotate => "rotate",
Self::Stop => "stop",
Self::RowsQuery => "rows_query",
Self::Incident => "incident",
Self::Intvar => "intvar",
Self::Rand => "rand",
Self::UserVar => "user_var",
Self::BeginLoadQuery => "begin_load_query",
Self::ExecuteLoadQuery => "execute_load_query",
Self::Heartbeat => "heartbeat",
Self::Ignorable => "ignorable",
Self::TransactionContext => "transaction_context",
Self::ViewChange => "view_change",
Self::XaPrepare => "xa_prepare",
Self::TransactionPayload => "transaction_payload",
Self::Unknown => "unknown",
}
}
pub fn from_raw(code: u8) -> Self {
match code {
2 => Self::Query,
3 => Self::Stop,
4 => Self::Rotate,
5 => Self::Intvar,
13 => Self::Rand,
14 => Self::UserVar,
15 => Self::FormatDescription,
16 => Self::Xid,
17 => Self::BeginLoadQuery,
18 => Self::ExecuteLoadQuery,
19 => Self::TableMap,
23 => Self::WriteRows,
24 => Self::UpdateRows,
25 => Self::DeleteRows,
26 => Self::Incident,
27 | 41 => Self::Heartbeat,
28 => Self::Ignorable,
29 => Self::RowsQuery,
30 => Self::WriteRows,
31 => Self::UpdateRows,
32 => Self::DeleteRows,
33 | 42 => Self::Gtid,
34 => Self::AnonymousGtid,
35 => Self::PreviousGtids,
36 => Self::TransactionContext,
37 => Self::ViewChange,
38 => Self::XaPrepare,
39 => Self::PartialUpdateRows,
40 => Self::TransactionPayload,
_ => Self::Unknown,
}
}
}
impl std::fmt::Display for EventType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct ParsedEvent {
pub offset: u64,
pub timestamp: u32,
pub event_type: EventType,
pub event_type_code: u8,
pub server_id: u32,
pub event_size: u32,
pub log_pos: u32,
pub payload: EventPayload,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
#[non_exhaustive]
pub enum EventPayload {
FormatDescription {
server_version: String,
},
TableMap {
table_id: u64,
database: String,
table: String,
column_count: u64,
},
Rows {
table_id: u64,
database: String,
table: String,
changes: Vec<RowChange>,
},
PartialUpdateRows {
table_id: u64,
database: String,
table: String,
before: Vec<Option<RowImage>>,
after: Vec<Option<RowImage>>,
},
Unresolved {
table_id: u64,
},
Query {
database: String,
sql: String,
},
Xid {
xid: u64,
},
Gtid {
gtid: String,
tag: Option<String>,
},
PreviousGtids {
gtid_set: String,
},
Rotate {
next_file: String,
position: u64,
},
Stop,
RowsQuery {
sql: String,
},
Incident {
incident_type: String,
message: String,
},
Intvar {
var_type: String,
value: u64,
},
Unknown {
raw_len: usize,
},
TransactionPayload {
compression: String,
uncompressed_size: u64,
events: Vec<ParsedEvent>,
},
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct RowChange {
pub before: Option<RowImage>,
pub after: Option<RowImage>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct RowImage {
pub columns: Vec<ColumnValue>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct ColumnValue {
pub index: usize,
pub name: Option<String>,
pub mysql_type: Option<String>,
pub signedness: Option<Signedness>,
pub value: CellValue,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", content = "value", rename_all = "snake_case")]
#[non_exhaustive]
pub enum CellValue {
Absent,
Null,
Bytes(String),
Int(i64),
UInt(u64),
Float(String),
Double(String),
Date {
year: u16,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
micros: u32,
},
Time {
negative: bool,
days: u32,
hours: u8,
minutes: u8,
seconds: u8,
micros: u32,
},
Json(serde_json::Value),
PartialJson {
diffs: Vec<JsonDiffRecord>,
reason: String,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct JsonDiffRecord {
pub path: String,
pub operation: String,
pub value: Option<serde_json::Value>,
}
#[derive(Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum Signedness {
Signed,
Unsigned,
Unknown,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn as_str_matches_serde_snake_case() {
let codes = [
0u8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 100,
160, 255,
];
for code in codes {
let event_type = EventType::from_raw(code);
let json = serde_json::to_string(&event_type).unwrap();
assert_eq!(
json.trim_matches('"'),
event_type.as_str(),
"serde and as_str disagree for code {code}"
);
}
}
#[test]
fn raw_code_table_matches_contract() {
let expected = [
(2u8, "query"),
(3, "stop"),
(4, "rotate"),
(5, "intvar"),
(13, "rand"),
(14, "user_var"),
(15, "format_description"),
(16, "xid"),
(17, "begin_load_query"),
(18, "execute_load_query"),
(19, "table_map"),
(23, "write_rows"),
(24, "update_rows"),
(25, "delete_rows"),
(26, "incident"),
(27, "heartbeat"),
(28, "ignorable"),
(29, "rows_query"),
(30, "write_rows"),
(31, "update_rows"),
(32, "delete_rows"),
(33, "gtid"),
(34, "anonymous_gtid"),
(35, "previous_gtids"),
(36, "transaction_context"),
(37, "view_change"),
(38, "xa_prepare"),
(39, "partial_update_rows"),
(40, "transaction_payload"),
(41, "heartbeat"),
(42, "gtid"),
];
for (code, name) in expected {
assert_eq!(EventType::from_raw(code).as_str(), name, "code {code}");
}
for code in [0u8, 1, 6, 7, 8, 9, 10, 11, 12, 20, 21, 22, 43, 160, 255] {
assert_eq!(EventType::from_raw(code), EventType::Unknown, "code {code}");
}
}
}