mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-10-02 15:25:32 +00:00
WIP: Lifetimes :(
This commit is contained in:
parent
8a1b9a89fd
commit
32a53e678a
@ -11,7 +11,7 @@ pub async fn establish<'a, 'b: 'a>(
|
|||||||
conn: &'a mut Connection,
|
conn: &'a mut Connection,
|
||||||
_options: ConnectOptions<'b>,
|
_options: ConnectOptions<'b>,
|
||||||
) -> Result<(), Error> {
|
) -> Result<(), Error> {
|
||||||
let init_packet = InitialHandshakePacket::deserialize(&conn.stream.next_bytes().await?)?;
|
let init_packet = InitialHandshakePacket::deserialize(&conn.stream.next_bytes().await?, None)?;
|
||||||
|
|
||||||
conn.capabilities = init_packet.capabilities;
|
conn.capabilities = init_packet.capabilities;
|
||||||
|
|
||||||
|
@ -92,7 +92,7 @@ impl Connection {
|
|||||||
self.send(ComPing()).await?;
|
self.send(ComPing()).await?;
|
||||||
|
|
||||||
// Ping response must be an OkPacket
|
// Ping response must be an OkPacket
|
||||||
OkPacket::deserialize(&self.stream.next_bytes().await?)?;
|
OkPacket::deserialize(&self.stream.next_bytes().await?, None)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
@ -3,142 +3,157 @@ use byteorder::{ByteOrder, LittleEndian};
|
|||||||
use bytes::Bytes;
|
use bytes::Bytes;
|
||||||
use failure::{err_msg, Error};
|
use failure::{err_msg, Error};
|
||||||
|
|
||||||
//pub struct Decoder<'a> {
|
pub struct Decoder<'a> {
|
||||||
// pub buf: &'a Bytes,
|
pub buf: &'a Bytes,
|
||||||
// pub index: usize,
|
pub index: usize,
|
||||||
//}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn decode_length(buf: &Bytes, index: &mut usize) -> Result<u32, Error> {
|
|
||||||
let length = decode_int_3(&buf, index);
|
|
||||||
|
|
||||||
if buf.len() < length as usize {
|
|
||||||
return Err(err_msg("Lengths to do not match"));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(length)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
impl<'a> Decoder<'a> {
|
||||||
pub fn decode_int_lenenc(buf: &Bytes, index: &mut usize) -> Option<usize> {
|
pub fn new(buf: &'a Bytes) -> Self {
|
||||||
match buf[*index] {
|
Decoder {
|
||||||
0xFB => {
|
buf,
|
||||||
*index += 1;
|
index: 0,
|
||||||
None
|
|
||||||
}
|
}
|
||||||
0xFC => {
|
}
|
||||||
let value = Some(LittleEndian::read_u16(&buf[*index + 1..]) as usize);
|
|
||||||
*index += 3;
|
#[inline]
|
||||||
value
|
pub fn decode_length(&mut self) -> Result<u32, Error> {
|
||||||
|
let length = self.decode_int_3();
|
||||||
|
|
||||||
|
if self.buf.len() < length as usize {
|
||||||
|
return Err(err_msg("Lengths to do not match"));
|
||||||
}
|
}
|
||||||
0xFD => {
|
|
||||||
let value = Some(LittleEndian::read_u24(&buf[*index + 1..]) as usize);
|
Ok(length)
|
||||||
*index += 4;
|
}
|
||||||
value
|
|
||||||
|
pub fn skip_bytes(&mut self, amount: usize) {
|
||||||
|
self.index += amount;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn decode_int_lenenc(&mut self) -> Option<usize> {
|
||||||
|
match self.buf[self.index] {
|
||||||
|
0xFB => {
|
||||||
|
self.index += 1;
|
||||||
|
None
|
||||||
|
}
|
||||||
|
0xFC => {
|
||||||
|
let value = Some(LittleEndian::read_u16(&self.buf[self.index + 1..]) as usize);
|
||||||
|
self.index += 3;
|
||||||
|
value
|
||||||
|
}
|
||||||
|
0xFD => {
|
||||||
|
let value = Some(LittleEndian::read_u24(&self.buf[self.index + 1..]) as usize);
|
||||||
|
self.index += 4;
|
||||||
|
value
|
||||||
|
}
|
||||||
|
0xFE => {
|
||||||
|
let value = Some(LittleEndian::read_u64(&self.buf[self.index + 1..]) as usize);
|
||||||
|
self.index += 9;
|
||||||
|
value
|
||||||
|
}
|
||||||
|
0xFF => panic!("int<lenenc> unprocessable first byte 0xFF"),
|
||||||
|
_ => {
|
||||||
|
let value = Some(self.buf[self.index] as usize);
|
||||||
|
self.index += 1;
|
||||||
|
value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
0xFE => {
|
}
|
||||||
let value = Some(LittleEndian::read_u64(&buf[*index + 1..]) as usize);
|
|
||||||
*index += 9;
|
#[inline]
|
||||||
value
|
pub fn decode_int_8(&mut self) -> u64 {
|
||||||
}
|
let value = LittleEndian::read_u64(&self.buf[self.index..]);
|
||||||
0xFF => panic!("int<lenenc> unprocessable first byte 0xFF"),
|
self.index += 8;
|
||||||
_ => {
|
value
|
||||||
let value = Some(buf[*index] as usize);
|
}
|
||||||
*index += 1;
|
|
||||||
value
|
#[inline]
|
||||||
|
pub fn decode_int_4(&mut self) -> u32 {
|
||||||
|
let value = LittleEndian::read_u32(&self.buf[self.index..]);
|
||||||
|
self.index += 4;
|
||||||
|
value
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn decode_int_3(&mut self) -> u32 {
|
||||||
|
let value = LittleEndian::read_u24(&self.buf[self.index..]);
|
||||||
|
self.index += 3;
|
||||||
|
value
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn decode_int_2(&mut self) -> u16 {
|
||||||
|
let value = LittleEndian::read_u16(&self.buf[self.index..]);
|
||||||
|
self.index += 2;
|
||||||
|
value
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn decode_int_1(&mut self) -> u8 {
|
||||||
|
let value = self.buf[self.index];
|
||||||
|
self.index += 1;
|
||||||
|
value
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn decode_string_lenenc(&mut self) -> Bytes {
|
||||||
|
let length = self.decode_int_3();
|
||||||
|
let value = Bytes::from(&self.buf[self.index..self.index + length as usize]);
|
||||||
|
self.index = self.index + length as usize;
|
||||||
|
value
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn decode_string_fix(&mut self, length: u32) -> Bytes {
|
||||||
|
let value = Bytes::from(&self.buf[self.index..self.index + length as usize]);
|
||||||
|
self.index = self.index + length as usize;
|
||||||
|
value
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn decode_string_eof(&mut self) -> Bytes {
|
||||||
|
let value = Bytes::from(&self.buf[self.index..]);
|
||||||
|
self.index = self.buf.len();
|
||||||
|
value
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn decode_string_null(&mut self) -> Result<Bytes, Error> {
|
||||||
|
if let Some(null_index) = memchr::memchr(0, &self.buf[self.index..]) {
|
||||||
|
let value = Bytes::from(&self.buf[self.index..self.index + null_index]);
|
||||||
|
self.index = self.index + null_index + 1;
|
||||||
|
Ok(value)
|
||||||
|
} else {
|
||||||
|
Err(err_msg("Null index no found"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn decode_byte_fix(&mut self, length: u32) -> Bytes {
|
||||||
|
let value = Bytes::from(&self.buf[self.index..self.index + length as usize]);
|
||||||
|
self.index = self.index + length as usize;
|
||||||
|
value
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn decode_byte_lenenc(&mut self) -> Bytes {
|
||||||
|
let length = self.decode_int_3();
|
||||||
|
let value = Bytes::from(&self.buf[self.index..self.index + length as usize]);
|
||||||
|
self.index = self.index + length as usize;
|
||||||
|
value
|
||||||
|
}
|
||||||
|
|
||||||
|
#[inline]
|
||||||
|
pub fn decode_byte_eof(&mut self) -> Bytes {
|
||||||
|
let value = Bytes::from(&self.buf[self.index..]);
|
||||||
|
self.index = self.buf.len();
|
||||||
|
value
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn decode_int_8(buf: &Bytes, index: &mut usize) -> u64 {
|
|
||||||
let value = LittleEndian::read_u64(&buf[*index..]);
|
|
||||||
*index += 8;
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn decode_int_4(buf: &Bytes, index: &mut usize) -> u32 {
|
|
||||||
let value = LittleEndian::read_u32(&buf[*index..]);
|
|
||||||
*index += 4;
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn decode_int_3(buf: &Bytes, index: &mut usize) -> u32 {
|
|
||||||
let value = LittleEndian::read_u24(&buf[*index..]);
|
|
||||||
*index += 3;
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn decode_int_2(buf: &Bytes, index: &mut usize) -> u16 {
|
|
||||||
let value = LittleEndian::read_u16(&buf[*index..]);
|
|
||||||
*index += 2;
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn decode_int_1(buf: &Bytes, index: &mut usize) -> u8 {
|
|
||||||
let value = buf[*index];
|
|
||||||
*index += 1;
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn decode_string_lenenc(buf: &Bytes, index: &mut usize) -> Bytes {
|
|
||||||
let length = decode_int_3(&buf, &mut *index);
|
|
||||||
let value = Bytes::from(&buf[*index..*index + length as usize]);
|
|
||||||
*index = *index + length as usize;
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn decode_string_fix(buf: &Bytes, index: &mut usize, length: usize) -> Bytes {
|
|
||||||
let value = Bytes::from(&buf[*index..*index + length as usize]);
|
|
||||||
*index = *index + length as usize;
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn decode_string_eof(buf: &Bytes, index: &mut usize) -> Bytes {
|
|
||||||
let value = Bytes::from(&buf[*index..]);
|
|
||||||
*index = buf.len();
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn decode_string_null(buf: &Bytes, index: &mut usize) -> Result<Bytes, Error> {
|
|
||||||
if let Some(null_index) = memchr::memchr(0, &buf[*index..]) {
|
|
||||||
let value = Bytes::from(&buf[*index..*index + null_index]);
|
|
||||||
*index = *index + null_index + 1;
|
|
||||||
Ok(value)
|
|
||||||
} else {
|
|
||||||
Err(err_msg("Null index no found"))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn decode_byte_fix(buf: &Bytes, index: &mut usize, length: usize) -> Bytes {
|
|
||||||
let value = Bytes::from(&buf[*index..*index + length as usize]);
|
|
||||||
*index = *index + length as usize;
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn decode_byte_lenenc(buf: &Bytes, index: &mut usize) -> Bytes {
|
|
||||||
let length = decode_int_3(&buf, &mut *index);
|
|
||||||
let value = Bytes::from(&buf[*index..*index + length as usize]);
|
|
||||||
*index = *index + length as usize;
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub fn decode_byte_eof(buf: &Bytes, index: &mut usize) -> Bytes {
|
|
||||||
let value = Bytes::from(&buf[*index..]);
|
|
||||||
*index = buf.len();
|
|
||||||
value
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
@ -146,24 +161,23 @@ mod tests {
|
|||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use failure::Error;
|
use failure::Error;
|
||||||
|
|
||||||
// [X] deserialize_int_lenenc
|
// [X] it_decodes_int_lenenc
|
||||||
// [X] deserialize_int_8
|
// [X] it_decodes_int_8
|
||||||
// [X] deserialize_int_4
|
// [X] it_decodes_int_4
|
||||||
// [X] deserialize_int_3
|
// [X] it_decodes_int_3
|
||||||
// [X] deserialize_int_2
|
// [X] it_decodes_int_2
|
||||||
// [X] deserialize_int_1
|
// [X] it_decodes_int_1
|
||||||
// [X] deserialize_string_lenenc
|
// [X] it_decodes_string_lenenc
|
||||||
// [X] deserialize_string_fix
|
// [X] it_decodes_string_fix
|
||||||
// [X] deserialize_string_eof
|
// [X] it_decodes_string_eof
|
||||||
// [X] deserialize_string_null
|
// [X] it_decodes_string_null
|
||||||
// [X] deserialize_byte_lenenc
|
// [X] it_decodes_byte_lenenc
|
||||||
// [X] deserialize_byte_eof
|
// [X] it_decodes_byte_eof
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_int_lenenc_0x_fb() {
|
fn it_decodes_int_lenenc_0x_fb() {
|
||||||
let buf: BytesMut = BytesMut::from(b"\xFB".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\xFB".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let int: Option<usize> = decoder.decode_int_lenenc();
|
||||||
let int: Option<usize> = decode_int_lenenc(&buf.freeze(), &mut index);
|
|
||||||
|
|
||||||
assert_eq!(int, None);
|
assert_eq!(int, None);
|
||||||
assert_eq!(index, 1);
|
assert_eq!(index, 1);
|
||||||
@ -171,9 +185,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_int_lenenc_0x_fc() {
|
fn it_decodes_int_lenenc_0x_fc() {
|
||||||
let buf = BytesMut::from(b"\xFC\x01\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\xFC\x01\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let int: Option<usize> = decoder.decode_int_lenenc();
|
||||||
let int: Option<usize> = decode_int_lenenc(&buf.freeze(), &mut index);
|
|
||||||
|
|
||||||
assert_eq!(int, Some(257));
|
assert_eq!(int, Some(257));
|
||||||
assert_eq!(index, 3);
|
assert_eq!(index, 3);
|
||||||
@ -181,9 +194,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_int_lenenc_0x_fd() {
|
fn it_decodes_int_lenenc_0x_fd() {
|
||||||
let buf = BytesMut::from(b"\xFD\x01\x01\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\xFD\x01\x01\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let int: Option<usize> = decoder.decode_int_lenenc();
|
||||||
let int: Option<usize> = decode_int_lenenc(&buf.freeze(), &mut index);
|
|
||||||
|
|
||||||
assert_eq!(int, Some(65793));
|
assert_eq!(int, Some(65793));
|
||||||
assert_eq!(index, 4);
|
assert_eq!(index, 4);
|
||||||
@ -191,9 +203,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_int_lenenc_0x_fe() {
|
fn it_decodes_int_lenenc_0x_fe() {
|
||||||
let buf = BytesMut::from(b"\xFE\x01\x01\x01\x01\x01\x01\x01\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\xFE\x01\x01\x01\x01\x01\x01\x01\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let int: Option<usize> = decoder.decode_int_lenenc();
|
||||||
let int: Option<usize> = decode_int_lenenc(&buf.freeze(), &mut index);
|
|
||||||
|
|
||||||
assert_eq!(int, Some(72340172838076673));
|
assert_eq!(int, Some(72340172838076673));
|
||||||
assert_eq!(index, 9);
|
assert_eq!(index, 9);
|
||||||
@ -201,9 +212,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_int_lenenc_0x_fa() {
|
fn it_decodes_int_lenenc_0x_fa() {
|
||||||
let buf = BytesMut::from(b"\xFA".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\xFA".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let int: Option<usize> = decoder.decode_int_lenenc();
|
||||||
let int: Option<usize> = decode_int_lenenc(&buf.freeze(), &mut index);
|
|
||||||
|
|
||||||
assert_eq!(int, Some(0xfA));
|
assert_eq!(int, Some(0xfA));
|
||||||
assert_eq!(index, 1);
|
assert_eq!(index, 1);
|
||||||
@ -211,9 +221,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_int_8() {
|
fn it_decodes_int_8() {
|
||||||
let buf = BytesMut::from(b"\x01\x01\x01\x01\x01\x01\x01\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\x01\x01\x01\x01\x01\x01\x01\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let int: u64 = decoder.decode_int_8();
|
||||||
let int: u64 = decode_int_8(&buf.freeze(), &mut index);
|
|
||||||
|
|
||||||
assert_eq!(int, 72340172838076673);
|
assert_eq!(int, 72340172838076673);
|
||||||
assert_eq!(index, 8);
|
assert_eq!(index, 8);
|
||||||
@ -221,9 +230,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_int_4() {
|
fn it_decodes_int_4() {
|
||||||
let buf = BytesMut::from(b"\x01\x01\x01\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\x01\x01\x01\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let int: u32 = decoder.decode_int_4();
|
||||||
let int: u32 = decode_int_4(&buf.freeze(), &mut index);
|
|
||||||
|
|
||||||
assert_eq!(int, 16843009);
|
assert_eq!(int, 16843009);
|
||||||
assert_eq!(index, 4);
|
assert_eq!(index, 4);
|
||||||
@ -231,9 +239,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_int_3() {
|
fn it_decodes_int_3() {
|
||||||
let buf = BytesMut::from(b"\x01\x01\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\x01\x01\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let int: u32 = decoder.decode_int_3();
|
||||||
let int: u32 = decode_int_3(&buf.freeze(), &mut index);
|
|
||||||
|
|
||||||
assert_eq!(int, 65793);
|
assert_eq!(int, 65793);
|
||||||
assert_eq!(index, 3);
|
assert_eq!(index, 3);
|
||||||
@ -241,9 +248,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_int_2() {
|
fn it_decodes_int_2() {
|
||||||
let buf = BytesMut::from(b"\x01\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\x01\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let int: u16 = decoder.decode_int_2();
|
||||||
let int: u16 = decode_int_2(&buf.freeze(), &mut index);
|
|
||||||
|
|
||||||
assert_eq!(int, 257);
|
assert_eq!(int, 257);
|
||||||
assert_eq!(index, 2);
|
assert_eq!(index, 2);
|
||||||
@ -251,9 +257,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_int_1() {
|
fn it_decodes_int_1() {
|
||||||
let buf = BytesMut::from(b"\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let int: u8 = decoder.decode_int_1();
|
||||||
let int: u8 = decode_int_1(&buf.freeze(), &mut index);
|
|
||||||
|
|
||||||
assert_eq!(int, 1);
|
assert_eq!(int, 1);
|
||||||
assert_eq!(index, 1);
|
assert_eq!(index, 1);
|
||||||
@ -261,9 +266,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_string_lenenc() {
|
fn it_decodes_string_lenenc() {
|
||||||
let buf = BytesMut::from(b"\x01\x00\x00\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\x01\x00\x00\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let string: Bytes = decoder.decode_string_lenenc();
|
||||||
let string: Bytes = decode_string_lenenc(&buf.freeze(), &mut index);
|
|
||||||
|
|
||||||
assert_eq!(string[0], b'\x01');
|
assert_eq!(string[0], b'\x01');
|
||||||
assert_eq!(string.len(), 1);
|
assert_eq!(string.len(), 1);
|
||||||
@ -272,9 +276,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_string_fix() {
|
fn it_decodes_string_fix() {
|
||||||
let buf = BytesMut::from(b"\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let string: Bytes = decoder.decode_string_fix(1);
|
||||||
let string: Bytes = decode_string_fix(&buf.freeze(), &mut index, 1);
|
|
||||||
|
|
||||||
assert_eq!(string[0], b'\x01');
|
assert_eq!(string[0], b'\x01');
|
||||||
assert_eq!(string.len(), 1);
|
assert_eq!(string.len(), 1);
|
||||||
@ -283,9 +286,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_string_eof() {
|
fn it_decodes_string_eof() {
|
||||||
let buf = BytesMut::from(b"\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let string: Bytes = decoder.decode_string_eof();
|
||||||
let string: Bytes = decode_string_eof(&buf.freeze(), &mut index);
|
|
||||||
|
|
||||||
assert_eq!(string[0], b'\x01');
|
assert_eq!(string[0], b'\x01');
|
||||||
assert_eq!(string.len(), 1);
|
assert_eq!(string.len(), 1);
|
||||||
@ -294,9 +296,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_string_null() -> Result<(), Error> {
|
fn it_decodes_string_null() -> Result<(), Error> {
|
||||||
let buf = BytesMut::from(b"random\x00\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"random\x00\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let string: Bytes = decoder.decode_string_null()?;
|
||||||
let string: Bytes = decode_string_null(&buf.freeze(), &mut index)?;
|
|
||||||
|
|
||||||
assert_eq!(&string[..], b"random");
|
assert_eq!(&string[..], b"random");
|
||||||
|
|
||||||
@ -309,9 +310,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_byte_fix() {
|
fn it_decodes_byte_fix() {
|
||||||
let buf = BytesMut::from(b"\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let string: Bytes = decoder.decode_byte_fix(1);
|
||||||
let string: Bytes = decode_byte_fix(&buf.freeze(), &mut index, 1);
|
|
||||||
|
|
||||||
assert_eq!(string[0], b'\x01');
|
assert_eq!(string[0], b'\x01');
|
||||||
assert_eq!(string.len(), 1);
|
assert_eq!(string.len(), 1);
|
||||||
@ -320,9 +320,8 @@ mod tests {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn it_decodes_byte_eof() {
|
fn it_decodes_byte_eof() {
|
||||||
let buf = BytesMut::from(b"\x01".to_vec());
|
let mut decoder = Decoder::new(&BytesMut::from(b"\x01".to_vec()).freeze());
|
||||||
let mut index = 0;
|
let string: Bytes = decoder.decode_byte_eof();
|
||||||
let string: Bytes = decode_byte_eof(&buf.freeze(), &mut index);
|
|
||||||
|
|
||||||
assert_eq!(string[0], b'\x01');
|
assert_eq!(string[0], b'\x01');
|
||||||
assert_eq!(string.len(), 1);
|
assert_eq!(string.len(), 1);
|
||||||
|
@ -7,7 +7,7 @@ use failure::{err_msg, Error};
|
|||||||
use std::convert::TryFrom;
|
use std::convert::TryFrom;
|
||||||
|
|
||||||
pub trait Deserialize: Sized {
|
pub trait Deserialize: Sized {
|
||||||
fn deserialize(buf: &Bytes) -> Result<Self, Error>;
|
fn deserialize<'a: 'b, 'b>(buf: &Bytes, decoder: Option<&mut Decoder>) -> Result<Self, Error>;
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -141,6 +141,12 @@ impl Default for ServerStatusFlag {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for FieldDetailFlag {
|
||||||
|
fn default() -> Self {
|
||||||
|
FieldDetailFlag::NOT_NULL
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Default for FieldType {
|
impl Default for FieldType {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
FieldType::MysqlTypeDecimal
|
FieldType::MysqlTypeDecimal
|
||||||
@ -197,6 +203,7 @@ pub struct ColumnPacket {
|
|||||||
pub columns: Option<usize>,
|
pub columns: Option<usize>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
pub struct ColumnDefPacket {
|
pub struct ColumnDefPacket {
|
||||||
pub length: u32,
|
pub length: u32,
|
||||||
pub seq_no: u8,
|
pub seq_no: u8,
|
||||||
@ -236,74 +243,73 @@ impl Message {
|
|||||||
let tag = buf[4];
|
let tag = buf[4];
|
||||||
|
|
||||||
Ok(Some(match tag {
|
Ok(Some(match tag {
|
||||||
0xFF => Message::ErrPacket(ErrPacket::deserialize(&buf)?),
|
0xFF => Message::ErrPacket(ErrPacket::deserialize(&buf, None)?),
|
||||||
0x00 | 0xFE => Message::OkPacket(OkPacket::deserialize(&buf)?),
|
0x00 | 0xFE => Message::OkPacket(OkPacket::deserialize(&buf, None)?),
|
||||||
_ => unimplemented!(),
|
_ => unimplemented!(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Deserialize for InitialHandshakePacket {
|
impl Deserialize for InitialHandshakePacket {
|
||||||
fn deserialize(buf: &Bytes) -> Result<Self, Error> {
|
fn deserialize<'a: 'b, 'b>(buf: &'a Bytes, decoder: Option<&mut Decoder<'b>>) -> Result<Self, Error> {
|
||||||
let mut index = 0;
|
let mut decoder: &mut Decoder = decoder.unwrap_or(&mut Decoder::new(&buf));
|
||||||
|
let length = decoder.decode_length()?;
|
||||||
let length = decode_length(&buf, &mut index)?;
|
let seq_no = decoder.decode_int_1();
|
||||||
let seq_no = decode_int_1(&buf, &mut index);
|
|
||||||
|
|
||||||
if seq_no != 0 {
|
if seq_no != 0 {
|
||||||
return Err(err_msg("Squence Number of Initial Handshake Packet is not 0"));
|
return Err(err_msg("Sequence Number of Initial Handshake Packet is not 0"));
|
||||||
}
|
}
|
||||||
|
|
||||||
let protocol_version = decode_int_1(&buf, &mut index);
|
let protocol_version = decoder.decode_int_1();
|
||||||
let server_version = decode_string_null(&buf, &mut index)?;
|
let server_version = decoder.decode_string_null()?;
|
||||||
let connection_id = decode_int_4(&buf, &mut index);
|
let connection_id = decoder.decode_int_4();
|
||||||
let auth_seed = decode_string_fix(&buf, &mut index, 8);
|
let auth_seed = decoder.decode_string_fix(8);
|
||||||
|
|
||||||
// Skip reserved byte
|
// Skip reserved byte
|
||||||
index += 1;
|
decoder.skip_bytes(1);
|
||||||
|
|
||||||
let mut capabilities =
|
let mut capabilities =
|
||||||
Capabilities::from_bits_truncate(decode_int_2(&buf, &mut index).into());
|
Capabilities::from_bits_truncate(decoder.decode_int_2().into());
|
||||||
|
|
||||||
let collation = decode_int_1(&buf, &mut index);
|
let collation = decoder.decode_int_1();
|
||||||
let status =
|
let status =
|
||||||
ServerStatusFlag::from_bits_truncate(decode_int_2(&buf, &mut index).into());
|
ServerStatusFlag::from_bits_truncate(decoder.decode_int_2().into());
|
||||||
|
|
||||||
capabilities |= Capabilities::from_bits_truncate(
|
capabilities |= Capabilities::from_bits_truncate(
|
||||||
((decode_int_2(&buf, &mut index) as u32) << 16).into(),
|
((decoder.decode_int_2() as u32) << 16).into(),
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut plugin_data_length = 0;
|
let mut plugin_data_length = 0;
|
||||||
if !(capabilities & Capabilities::PLUGIN_AUTH).is_empty() {
|
if !(capabilities & Capabilities::PLUGIN_AUTH).is_empty() {
|
||||||
plugin_data_length = decode_int_1(&buf, &mut index);
|
plugin_data_length = decoder.decode_int_1();
|
||||||
} else {
|
} else {
|
||||||
// Skip reserve byte
|
// Skip reserve byte
|
||||||
index += 1;
|
decoder.skip_bytes(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Skip filler
|
// Skip filler
|
||||||
index += 6;
|
decoder.skip_bytes(6);
|
||||||
|
|
||||||
if (capabilities & Capabilities::CLIENT_MYSQL).is_empty() {
|
if (capabilities & Capabilities::CLIENT_MYSQL).is_empty() {
|
||||||
capabilities |= Capabilities::from_bits_truncate(
|
capabilities |= Capabilities::from_bits_truncate(
|
||||||
((decode_int_4(&buf, &mut index) as u128) << 32).into(),
|
((decoder.decode_int_4() as u128) << 32).into(),
|
||||||
);
|
);
|
||||||
} else {
|
} else {
|
||||||
// Skip filler
|
// Skip filler
|
||||||
index += 4;
|
decoder.skip_bytes(4);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut scramble: Option<Bytes> = None;
|
let mut scramble: Option<Bytes> = None;
|
||||||
if !(capabilities & Capabilities::SECURE_CONNECTION).is_empty() {
|
if !(capabilities & Capabilities::SECURE_CONNECTION).is_empty() {
|
||||||
let len = std::cmp::max(12, plugin_data_length as usize - 9);
|
let len = std::cmp::max(12, plugin_data_length as usize - 9);
|
||||||
scramble = Some(decode_string_fix(&buf, &mut index, len));
|
scramble = Some(decoder.decode_string_fix(len as u32));
|
||||||
// Skip reserve byte
|
// Skip reserve byte
|
||||||
index += 1;
|
decoder.skip_bytes(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut auth_plugin_name: Option<Bytes> = None;
|
let mut auth_plugin_name: Option<Bytes> = None;
|
||||||
if !(capabilities & Capabilities::PLUGIN_AUTH).is_empty() {
|
if !(capabilities & Capabilities::PLUGIN_AUTH).is_empty() {
|
||||||
auth_plugin_name = Some(decode_string_null(&buf, &mut index)?);
|
auth_plugin_name = Some(decoder.decode_string_null()?);
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(InitialHandshakePacket {
|
Ok(InitialHandshakePacket {
|
||||||
@ -324,30 +330,30 @@ impl Deserialize for InitialHandshakePacket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Deserialize for OkPacket {
|
impl Deserialize for OkPacket {
|
||||||
fn deserialize(buf: &Bytes) -> Result<Self, Error> {
|
fn deserialize(buf: &Bytes, decoder: Option<&mut Decoder>) -> Result<Self, Error> {
|
||||||
let mut index = 0;
|
let mut decoder = decoder.unwrap_or(&mut Decoder::new(&buf));
|
||||||
|
|
||||||
// Packet header
|
// Packet header
|
||||||
let length = decode_length(&buf, &mut index)?;
|
let length = decoder.decode_length()?;
|
||||||
let seq_no = decode_int_1(&buf, &mut index);
|
let seq_no = decoder.decode_int_1();
|
||||||
|
|
||||||
// Packet body
|
// Packet body
|
||||||
let packet_header = decode_int_1(&buf, &mut index);
|
let packet_header = decoder.decode_int_1();
|
||||||
if packet_header != 0 && packet_header != 0xFE {
|
if packet_header != 0 && packet_header != 0xFE {
|
||||||
panic!("Packet header is not 0 or 0xFE for OkPacket");
|
panic!("Packet header is not 0 or 0xFE for OkPacket");
|
||||||
}
|
}
|
||||||
|
|
||||||
let affected_rows = decode_int_lenenc(&buf, &mut index);
|
let affected_rows = decoder.decode_int_lenenc();
|
||||||
let last_insert_id = decode_int_lenenc(&buf, &mut index);
|
let last_insert_id = decoder.decode_int_lenenc();
|
||||||
let server_status =
|
let server_status =
|
||||||
ServerStatusFlag::from_bits_truncate(decode_int_2(&buf, &mut index).into());
|
ServerStatusFlag::from_bits_truncate(decoder.decode_int_2().into());
|
||||||
let warning_count = decode_int_2(&buf, &mut index);
|
let warning_count = decoder.decode_int_2();
|
||||||
|
|
||||||
// Assuming CLIENT_SESSION_TRACK is unsupported
|
// Assuming CLIENT_SESSION_TRACK is unsupported
|
||||||
let session_state_info = None;
|
let session_state_info = None;
|
||||||
let value = None;
|
let value = None;
|
||||||
|
|
||||||
let info = Bytes::from(&buf[index..]);
|
let info = decoder.decode_byte_eof();
|
||||||
|
|
||||||
Ok(OkPacket {
|
Ok(OkPacket {
|
||||||
length,
|
length,
|
||||||
@ -364,18 +370,18 @@ impl Deserialize for OkPacket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Deserialize for ErrPacket {
|
impl Deserialize for ErrPacket {
|
||||||
fn deserialize(buf: &Bytes) -> Result<Self, Error> {
|
fn deserialize(buf: &Bytes, decoder: Option<&mut Decoder>) -> Result<Self, Error> {
|
||||||
let mut index = 0;
|
let mut decoder = decoder.unwrap_or(&mut Decoder::new(&buf));
|
||||||
|
|
||||||
let length = decode_length(&buf, &mut index)?;
|
let length = decoder.decode_length()?;
|
||||||
let seq_no = decode_int_1(&buf, &mut index);
|
let seq_no = decoder.decode_int_1();
|
||||||
|
|
||||||
let packet_header = decode_int_1(&buf, &mut index);
|
let packet_header = decoder.decode_int_1();
|
||||||
if packet_header != 0xFF {
|
if packet_header != 0xFF {
|
||||||
panic!("Packet header is not 0xFF for ErrPacket");
|
panic!("Packet header is not 0xFF for ErrPacket");
|
||||||
}
|
}
|
||||||
|
|
||||||
let error_code = ErrorCode::try_from(decode_int_2(&buf, &mut index))?;
|
let error_code = ErrorCode::try_from(decoder.decode_int_2())?;
|
||||||
|
|
||||||
let mut stage = None;
|
let mut stage = None;
|
||||||
let mut max_stage = None;
|
let mut max_stage = None;
|
||||||
@ -388,17 +394,17 @@ impl Deserialize for ErrPacket {
|
|||||||
|
|
||||||
// Progress Reporting
|
// Progress Reporting
|
||||||
if error_code as u16 == 0xFFFF {
|
if error_code as u16 == 0xFFFF {
|
||||||
stage = Some(decode_int_1(buf, &mut index));
|
stage = Some(decoder.decode_int_1());
|
||||||
max_stage = Some(decode_int_1(buf, &mut index));
|
max_stage = Some(decoder.decode_int_1());
|
||||||
progress = Some(decode_int_3(buf, &mut index));
|
progress = Some(decoder.decode_int_3());
|
||||||
progress_info = Some(decode_string_lenenc(&buf, &mut index));
|
progress_info = Some(decoder.decode_string_lenenc());
|
||||||
} else {
|
} else {
|
||||||
if buf[index] == b'#' {
|
if buf[decoder.index] == b'#' {
|
||||||
sql_state_marker = Some(decode_string_fix(buf, &mut index, 1));
|
sql_state_marker = Some(decoder.decode_string_fix(1));
|
||||||
sql_state = Some(decode_string_fix(buf, &mut index, 5));
|
sql_state = Some(decoder.decode_string_fix(5));
|
||||||
error_message = Some(decode_string_eof(buf, &mut index));
|
error_message = Some(decoder.decode_string_eof());
|
||||||
} else {
|
} else {
|
||||||
error_message = Some(decode_string_eof(buf, &mut index));
|
error_message = Some(decoder.decode_string_eof());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -418,12 +424,12 @@ impl Deserialize for ErrPacket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Deserialize for ColumnPacket {
|
impl Deserialize for ColumnPacket {
|
||||||
fn deserialize(buf: &Bytes) -> Result<Self, Error> {
|
fn deserialize(buf: &Bytes, decoder: Option<&mut Decoder>) -> Result<Self, Error> {
|
||||||
let mut index = 0;
|
let mut decoder = decoder.unwrap_or(&mut Decoder::new(&buf));
|
||||||
|
|
||||||
let length = decode_length(&buf, &mut index)?;
|
let length = decoder.decode_length()?;
|
||||||
let seq_no = decode_int_1(&buf, &mut index);
|
let seq_no = decoder.decode_int_1();
|
||||||
let columns = decode_int_lenenc(&buf, &mut index);
|
let columns = decoder.decode_int_lenenc();
|
||||||
|
|
||||||
Ok(ColumnPacket {
|
Ok(ColumnPacket {
|
||||||
length,
|
length,
|
||||||
@ -434,24 +440,24 @@ impl Deserialize for ColumnPacket {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Deserialize for ColumnDefPacket {
|
impl Deserialize for ColumnDefPacket {
|
||||||
fn deserialize(buf: &Bytes) -> Result<Self, Error> {
|
fn deserialize(buf: &Bytes, decoder: Option<&mut Decoder>) -> Result<Self, Error> {
|
||||||
let mut index = 0;
|
let mut decoder = decoder.unwrap_or(&mut Decoder::new(&buf));
|
||||||
|
|
||||||
let length = decode_length(&buf, &mut index)?;
|
let length = decoder.decode_length()?;
|
||||||
let seq_no = decode_int_1(&buf, &mut index);
|
let seq_no = decoder.decode_int_1();
|
||||||
|
|
||||||
let catalog = decode_string_lenenc(&buf, &mut index);
|
let catalog = decoder.decode_string_lenenc();
|
||||||
let schema = decode_string_lenenc(&buf, &mut index);
|
let schema = decoder.decode_string_lenenc();
|
||||||
let table_alias = decode_string_lenenc(&buf, &mut index);
|
let table_alias = decoder.decode_string_lenenc();
|
||||||
let table = decode_string_lenenc(&buf, &mut index);
|
let table = decoder.decode_string_lenenc();
|
||||||
let column_alias = decode_string_lenenc(&buf, &mut index);
|
let column_alias = decoder.decode_string_lenenc();
|
||||||
let column = decode_string_lenenc(&buf, &mut index);
|
let column = decoder.decode_string_lenenc();
|
||||||
let length_of_fixed_fields = decode_int_lenenc(&buf, &mut index);
|
let length_of_fixed_fields = decoder.decode_int_lenenc();
|
||||||
let char_set = decode_int_2(&buf, &mut index);
|
let char_set = decoder.decode_int_2();
|
||||||
let max_columns = decode_int_4(&buf, &mut index);
|
let max_columns = decoder.decode_int_4();
|
||||||
let field_type = FieldType::try_from(decode_int_1(&buf, &mut index))?;
|
let field_type = FieldType::try_from(decoder.decode_int_1())?;
|
||||||
let field_details = FieldDetailFlag::from_bits_truncate(decode_int_2(&buf, &mut index));
|
let field_details = FieldDetailFlag::from_bits_truncate(decoder.decode_int_2());
|
||||||
let decimals = decode_int_1(&buf, &mut index);
|
let decimals = decoder.decode_int_1();
|
||||||
|
|
||||||
// Skip last two unused bytes
|
// Skip last two unused bytes
|
||||||
// index += 2;
|
// index += 2;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user