mysql_binlog_connector_rust/event/
gtid_event.rs1use crate::binlog_error::BinlogError;
2use byteorder::{LittleEndian, ReadBytesExt};
3use serde::{Deserialize, Serialize};
4use std::fmt::Write;
5use std::io::Cursor;
6
7#[derive(Debug, Deserialize, Serialize, Clone)]
8pub struct GtidEvent {
9 pub flags: u8,
10 pub gtid: String,
11}
12
13impl GtidEvent {
14 pub fn parse(cursor: &mut Cursor<&Vec<u8>>) -> Result<Self, BinlogError> {
15 let flags = cursor.read_u8()?;
18 let sid = Self::read_uuid(cursor)?;
19 let gno = cursor.read_u64::<LittleEndian>()?;
20
21 Ok(GtidEvent {
22 flags,
23 gtid: format!("{}:{}", sid, gno),
24 })
25 }
26
27 pub fn read_uuid(cursor: &mut Cursor<&Vec<u8>>) -> Result<String, BinlogError> {
28 Ok(format!(
29 "{}-{}-{}-{}-{}",
30 Self::bytes_to_hex_string(cursor, 4)?,
31 Self::bytes_to_hex_string(cursor, 2)?,
32 Self::bytes_to_hex_string(cursor, 2)?,
33 Self::bytes_to_hex_string(cursor, 2)?,
34 Self::bytes_to_hex_string(cursor, 6)?,
35 ))
36 }
37
38 fn bytes_to_hex_string(
39 cursor: &mut Cursor<&Vec<u8>>,
40 byte_count: u8,
41 ) -> Result<String, BinlogError> {
42 let mut res = String::new();
43 for _ in 0..byte_count {
44 write!(&mut res, "{:02x}", cursor.read_u8()?)?;
45 }
46 Ok(res)
47 }
48}