Skip to main content

mysql_replication/events/
mod.rs

1//! Concrete protocol events share decoding and diagnostic output, not CDC semantics.
2mod binlog;
3mod control;
4mod gtid;
5mod header;
6mod mode;
7mod rows;
8mod table_map;
9
10use crate::Result;
11pub use binlog::{BinlogEvent, OwnedBinlogEvent};
12pub use control::{
13    ChecksumAlgorithm, FormatDescriptionEvent, HeartbeatEvent, PreviousGtidsEvent, QueryEvent,
14    RotateEvent, SidIntervals, TaggedPreviousGtidsEvent, TaggedSidIntervals, XidEvent,
15};
16pub use gtid::{GtidEvent, TaggedGtidEvent};
17pub use header::EventHeader;
18pub use mode::{BinlogMode, Full, Minimal};
19pub use rows::{
20    ColumnDefinition, ColumnInfo, ColumnType, CompleteImage, DecimalValue, Field, Fields,
21    IntegerValue, OwnedRowImage, ProjectedOperation, Row, RowImage, RowImages, RowOperation,
22    RowReadError, RowValue, Rows, RowsDecoder, RowsEvent, RowsKind, RowsStatementEndEvent,
23    SparseImage, TableDefinition, is_rows_statement_end_marker,
24};
25use std::{fmt::Debug, io::Write};
26pub use table_map::{TableColumns, TableMapEvent};
27
28/// A decoder is bound to its file layout before receiving an event body.
29/// Borrowed fields remain valid for the input lifetime. Header decoding receives
30/// exactly 19 bytes; body decoding excludes the common header and checksum.
31/// This trait has no fields. Common header fields belong to [`EventHeader`] in
32/// [`BinlogEvent`]; concrete protocol-body fields belong to [`EventData`] variants.
33/// MINIMAL/FULL policies do not change this interface.
34pub trait Event<'a>: Debug {
35    fn decode(&mut self, data: &'a [u8]) -> Result<()>;
36    /// Writes diagnostics only. This does not serialize a binary event or CDC row.
37    fn dump(&self, writer: &mut dyn Write) -> Result<()>;
38}
39
40#[derive(Debug, Clone, Copy, Default)]
41pub struct RawEvent<'a> {
42    pub data: &'a [u8],
43}
44impl<'a> Event<'a> for RawEvent<'a> {
45    fn dump(&self, writer: &mut dyn Write) -> Result<()> {
46        writeln!(writer, "Data: {:02x?}\n", self.data)?;
47        Ok(())
48    }
49    fn decode(&mut self, data: &'a [u8]) -> Result<()> {
50        self.data = data;
51        Ok(())
52    }
53}
54
55/// An explicit variant retains typed access without downcasting.
56/// Each variant contains a protocol body, without duplicating the common header.
57/// Control-body fields are shared across modes; only [`RowsEvent`] carries `M`
58/// and exposes mode-specific decoded row-image types.
59#[derive(Debug, Clone)]
60#[non_exhaustive]
61pub enum EventData<'a, M: BinlogMode> {
62    FormatDescription(FormatDescriptionEvent<'a>),
63    Rotate(RotateEvent<'a>),
64    Query(QueryEvent<'a>),
65    TableMap(TableMapEvent<'a>),
66    Rows(RowsEvent<'a, M>),
67    RowsStatementEnd(RowsStatementEndEvent<'a>),
68    Xid(XidEvent),
69    Gtid(GtidEvent),
70    AnonymousGtid(GtidEvent),
71    TaggedGtid(TaggedGtidEvent<'a>),
72    PreviousGtids(PreviousGtidsEvent<'a>),
73    TaggedPreviousGtids(TaggedPreviousGtidsEvent<'a>),
74    Heartbeat(HeartbeatEvent<'a>),
75    /// Unknown or deliberately unimplemented body; never silently discarded.
76    Raw(RawEvent<'a>),
77}
78impl<'a, M: BinlogMode> Event<'a> for EventData<'a, M> {
79    fn dump(&self, writer: &mut dyn Write) -> Result<()> {
80        match self {
81            Self::FormatDescription(event) => event.dump(writer),
82            Self::Rotate(event) => event.dump(writer),
83            Self::Query(event) => event.dump(writer),
84            Self::TableMap(event) => event.dump(writer),
85            Self::Rows(event) => event.dump(writer),
86            Self::RowsStatementEnd(event) => event.dump(writer),
87            Self::Xid(event) => event.dump(writer),
88            Self::TaggedGtid(event) => event.dump(writer),
89            Self::PreviousGtids(event) => event.dump(writer),
90            Self::TaggedPreviousGtids(event) => event.dump(writer),
91            Self::Heartbeat(event) => event.dump(writer),
92            Self::Raw(event) => event.dump(writer),
93            Self::Gtid(event) | Self::AnonymousGtid(event) => event.dump(writer),
94        }
95    }
96
97    fn decode(&mut self, data: &'a [u8]) -> Result<()> {
98        match self {
99            Self::FormatDescription(v) => v.decode(data),
100            Self::Rotate(v) => v.decode(data),
101            Self::Query(v) => v.decode(data),
102            Self::TableMap(v) => v.decode(data),
103            Self::Rows(v) => v.decode(data),
104            Self::RowsStatementEnd(v) => v.decode(data),
105            Self::Xid(v) => v.decode(data),
106            Self::Gtid(v) | Self::AnonymousGtid(v) => v.decode(data),
107            Self::TaggedGtid(v) => v.decode(data),
108            Self::PreviousGtids(v) => v.decode(data),
109            Self::TaggedPreviousGtids(v) => v.decode(data),
110            Self::Heartbeat(v) => v.decode(data),
111            Self::Raw(v) => v.decode(data),
112        }
113    }
114}
115
116impl<M: BinlogMode> EventData<'_, M> {
117    pub(super) fn layout(&self) -> EventData<'static, M> {
118        match self {
119            Self::FormatDescription(v) => EventData::FormatDescription(FormatDescriptionEvent {
120                checksum_algorithm: v.checksum_algorithm,
121                ..Default::default()
122            }),
123            Self::Rotate(v) => EventData::Rotate(RotateEvent::with_post_header(v.post_header)),
124            Self::Query(v) => EventData::Query(QueryEvent::with_post_header(v.post_header)),
125            Self::TableMap(v) => {
126                EventData::TableMap(TableMapEvent::with_post_header(v.post_header))
127            }
128            Self::Rows(v) => {
129                let mut rows = RowsEvent::with_layout(v.kind, v.post_header, v.v2);
130                rows.decoder = v.decoder.clone();
131                EventData::Rows(rows)
132            }
133            Self::RowsStatementEnd(v) => EventData::RowsStatementEnd(v.layout()),
134            Self::Xid(_) => EventData::Xid(Default::default()),
135            Self::Gtid(_) => EventData::Gtid(Default::default()),
136            Self::AnonymousGtid(_) => EventData::AnonymousGtid(Default::default()),
137            Self::TaggedGtid(_) => EventData::TaggedGtid(Default::default()),
138            Self::PreviousGtids(_) => EventData::PreviousGtids(Default::default()),
139            Self::TaggedPreviousGtids(_) => EventData::TaggedPreviousGtids(Default::default()),
140            Self::Heartbeat(_) => EventData::Heartbeat(Default::default()),
141            Self::Raw(_) => EventData::Raw(Default::default()),
142        }
143    }
144}