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