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, Unspecified};
19pub use rows::{
20    ColumnDefinition, ColumnType, CompleteImage, DecimalValue, Full, ImageMode, IntegerValue,
21    Minimal, Row, RowField, RowFields, RowImage, RowImages, RowValue, Rows, RowsDecoder, RowsEvent,
22    RowsKind, SparseImage, TypedRow, TypedRows, TypedRowsDecoder, TypedRowsEvent,
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 = Unspecified> {
61    FormatDescription(FormatDescriptionEvent<'a>),
62    Rotate(RotateEvent<'a>),
63    Query(QueryEvent<'a>),
64    TableMap(TableMapEvent<'a>),
65    Rows(RowsEvent<'a, M>),
66    Xid(XidEvent),
67    Gtid(GtidEvent),
68    AnonymousGtid(GtidEvent),
69    TaggedGtid(TaggedGtidEvent<'a>),
70    PreviousGtids(PreviousGtidsEvent<'a>),
71    Heartbeat(HeartbeatEvent<'a>),
72    /// Unknown or deliberately unimplemented body; never silently discarded.
73    Raw(RawEvent<'a>),
74}
75impl<'a, M: BinlogMode> Event<'a> for EventData<'a, M> {
76    fn dump(&self, writer: &mut dyn Write) -> Result<()> {
77        match self {
78            Self::FormatDescription(event) => event.dump(writer),
79            Self::Rotate(event) => event.dump(writer),
80            Self::Query(event) => event.dump(writer),
81            Self::TableMap(event) => event.dump(writer),
82            Self::Rows(event) => event.dump(writer),
83            Self::Xid(event) => event.dump(writer),
84            Self::TaggedGtid(event) => event.dump(writer),
85            Self::PreviousGtids(event) => event.dump(writer),
86            Self::Heartbeat(event) => event.dump(writer),
87            Self::Raw(event) => event.dump(writer),
88            Self::Gtid(event) | Self::AnonymousGtid(event) => event.dump(writer),
89        }
90    }
91
92    fn decode(&mut self, data: &'a [u8]) -> Result<()> {
93        match self {
94            Self::FormatDescription(v) => v.decode(data),
95            Self::Rotate(v) => v.decode(data),
96            Self::Query(v) => v.decode(data),
97            Self::TableMap(v) => v.decode(data),
98            Self::Rows(v) => v.decode(data),
99            Self::Xid(v) => v.decode(data),
100            Self::Gtid(v) | Self::AnonymousGtid(v) => v.decode(data),
101            Self::TaggedGtid(v) => v.decode(data),
102            Self::PreviousGtids(v) => v.decode(data),
103            Self::Heartbeat(v) => v.decode(data),
104            Self::Raw(v) => v.decode(data),
105        }
106    }
107}
108
109impl<M: BinlogMode> EventData<'_, M> {
110    pub(super) fn layout(&self) -> EventData<'static, M> {
111        match self {
112            Self::FormatDescription(v) => EventData::FormatDescription(FormatDescriptionEvent {
113                checksum_algorithm: v.checksum_algorithm,
114                ..Default::default()
115            }),
116            Self::Rotate(v) => EventData::Rotate(RotateEvent::with_post_header(v.post_header)),
117            Self::Query(v) => EventData::Query(QueryEvent::with_post_header(v.post_header)),
118            Self::TableMap(v) => {
119                EventData::TableMap(TableMapEvent::with_post_header(v.post_header))
120            }
121            Self::Rows(v) => EventData::Rows(RowsEvent::for_mode(v.kind, v.post_header, v.v2)),
122            Self::Xid(_) => EventData::Xid(Default::default()),
123            Self::Gtid(_) => EventData::Gtid(Default::default()),
124            Self::AnonymousGtid(_) => EventData::AnonymousGtid(Default::default()),
125            Self::TaggedGtid(_) => EventData::TaggedGtid(Default::default()),
126            Self::PreviousGtids(_) => EventData::PreviousGtids(Default::default()),
127            Self::Heartbeat(_) => EventData::Heartbeat(Default::default()),
128            Self::Raw(_) => EventData::Raw(Default::default()),
129        }
130    }
131}