mysql_binlog_connector_rust/event/
format_description_event.rsuse crate::{binlog_error::BinlogError, event::event_type::EventType};
use byteorder::{LittleEndian, ReadBytesExt};
use serde::{Deserialize, Serialize};
use std::io::{Cursor, Read, Seek, SeekFrom};
use super::checksum_type::ChecksumType;
#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct FormatDescriptionEvent {
pub binlog_version: u16,
pub server_version: String,
pub create_timestamp: u32,
pub header_length: u8,
pub checksum_type: ChecksumType,
}
impl FormatDescriptionEvent {
pub fn parse(cursor: &mut Cursor<&Vec<u8>>, data_length: usize) -> Result<Self, BinlogError> {
let binlog_version = cursor.read_u16::<LittleEndian>()?;
let mut server_version_buf = [0u8; 50];
cursor.read_exact(&mut server_version_buf)?;
let server_version = std::str::from_utf8(&server_version_buf)
.unwrap()
.to_string();
let create_timestamp = cursor.read_u32::<LittleEndian>()?;
let header_length = cursor.read_u8()?;
cursor.seek(SeekFrom::Current(EventType::FormatDescription as i64 - 1))?;
let payload_length = cursor.read_u8()? as usize;
let mut checksum_type = 0;
let checksum_block_length = data_length - payload_length;
if checksum_block_length > 0 {
let current_pos = 2 + 50 + 4 + 1 + EventType::FormatDescription as u8;
cursor.seek(SeekFrom::Current(
payload_length as i64 - current_pos as i64,
))?;
checksum_type = cursor.read_u8()?;
}
Ok(Self {
binlog_version,
server_version,
create_timestamp,
header_length,
checksum_type: ChecksumType::from_code(checksum_type),
})
}
}