mirror of
https://github.com/launchbadge/sqlx.git
synced 2025-12-29 21:00:54 +00:00
Deserialize -> Decode
This commit is contained in:
parent
1137fe4150
commit
433ec628da
@ -1,8 +1,8 @@
|
||||
use super::Connection;
|
||||
use crate::{
|
||||
mariadb::{
|
||||
Capabilities, ComStmtExec, DeContext, Deserialize, EofPacket, ErrPacket,
|
||||
HandshakeResponsePacket, InitialHandshakePacket, Message, OkPacket, StmtExecFlag,
|
||||
Capabilities, ComStmtExec, DeContext, Decode, EofPacket, ErrPacket,
|
||||
HandshakeResponsePacket, InitialHandshakePacket, OkPacket, StmtExecFlag,
|
||||
},
|
||||
ConnectOptions,
|
||||
};
|
||||
@ -16,7 +16,7 @@ pub async fn establish<'a, 'b: 'a>(
|
||||
) -> Result<(), Error> {
|
||||
let buf = conn.stream.next_packet().await?;
|
||||
let mut de_ctx = DeContext::new(&mut conn.context, buf);
|
||||
let initial = InitialHandshakePacket::deserialize(&mut de_ctx)?;
|
||||
let initial = InitialHandshakePacket::decode(&mut de_ctx)?;
|
||||
|
||||
de_ctx.ctx.capabilities = de_ctx.ctx.capabilities.bitand(initial.capabilities);
|
||||
|
||||
@ -25,7 +25,7 @@ pub async fn establish<'a, 'b: 'a>(
|
||||
capabilities: de_ctx.ctx.capabilities,
|
||||
max_packet_size: 1024,
|
||||
extended_capabilities: Some(Capabilities::from_bits_truncate(0)),
|
||||
username: Bytes::from(options.user.unwrap_or("")),
|
||||
username: options.user.unwrap_or(""),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
@ -35,10 +35,10 @@ pub async fn establish<'a, 'b: 'a>(
|
||||
|
||||
match ctx.decoder.peek_tag() {
|
||||
0xFF => {
|
||||
return Err(ErrPacket::deserialize(&mut ctx)?.into());
|
||||
return Err(ErrPacket::decode(&mut ctx)?.into());
|
||||
}
|
||||
0x00 => {
|
||||
OkPacket::deserialize(&mut ctx)?;
|
||||
OkPacket::decode(&mut ctx)?;
|
||||
}
|
||||
_ => failure::bail!("Did not receive an ErrPacket nor OkPacket when one is expected"),
|
||||
}
|
||||
@ -174,8 +174,8 @@ mod test {
|
||||
ctx.next_packet().await?;
|
||||
|
||||
match ctx.decoder.peek_tag() {
|
||||
0xFF => println!("{:?}", ErrPacket::deserialize(&mut ctx)?),
|
||||
0x00 => println!("{:?}", OkPacket::deserialize(&mut ctx)?),
|
||||
0xFF => println!("{:?}", ErrPacket::decode(&mut ctx)?),
|
||||
0x00 => println!("{:?}", OkPacket::decode(&mut ctx)?),
|
||||
_ => println!("{:?}", ResultSet::deserialize(ctx).await?),
|
||||
}
|
||||
|
||||
|
||||
@ -1,8 +1,8 @@
|
||||
use crate::{
|
||||
mariadb::{
|
||||
protocol::encode, Capabilities, ComInitDb, ComPing, ComQuery, ComQuit, ComStmtPrepare,
|
||||
ComStmtPrepareResp, DeContext, Decoder, Deserialize, Encode, ErrPacket, Message, OkPacket,
|
||||
PacketHeader, ResultSet, ServerStatusFlag,
|
||||
ComStmtPrepareResp, DeContext, Decode, Decoder, Encode, ErrPacket, OkPacket, PacketHeader,
|
||||
ResultSet, ServerStatusFlag,
|
||||
},
|
||||
ConnectOptions,
|
||||
};
|
||||
@ -81,7 +81,7 @@ impl Connection {
|
||||
connection_id: -1,
|
||||
seq_no: 1,
|
||||
last_seq_no: 0,
|
||||
capabilities: Capabilities::CLIENT_MYSQL,
|
||||
capabilities: Capabilities::CLIENT_PROTOCOL_41,
|
||||
status: ServerStatusFlag::default(),
|
||||
},
|
||||
};
|
||||
@ -124,9 +124,9 @@ impl Connection {
|
||||
ctx.next_packet().await?;
|
||||
|
||||
match ctx.decoder.peek_tag() {
|
||||
0xFF => Err(ErrPacket::deserialize(&mut ctx)?.into()),
|
||||
0xFF => Err(ErrPacket::decode(&mut ctx)?.into()),
|
||||
0x00 => {
|
||||
OkPacket::deserialize(&mut ctx)?;
|
||||
OkPacket::decode(&mut ctx)?;
|
||||
Ok(None)
|
||||
}
|
||||
0xFB => unimplemented!(),
|
||||
@ -143,10 +143,10 @@ impl Connection {
|
||||
let mut ctx = DeContext::new(&mut self.context, self.stream.next_packet().await?);
|
||||
match ctx.decoder.peek_tag() {
|
||||
0xFF => {
|
||||
ErrPacket::deserialize(&mut ctx)?;
|
||||
ErrPacket::decode(&mut ctx)?;
|
||||
}
|
||||
0x00 => {
|
||||
OkPacket::deserialize(&mut ctx)?;
|
||||
OkPacket::decode(&mut ctx)?;
|
||||
}
|
||||
_ => failure::bail!("Did not receive an ErrPacket nor OkPacket when one was expected"),
|
||||
}
|
||||
@ -158,7 +158,7 @@ impl Connection {
|
||||
self.send(ComPing()).await?;
|
||||
|
||||
// Ping response must be an OkPacket
|
||||
OkPacket::deserialize(&mut DeContext::new(
|
||||
OkPacket::decode(&mut DeContext::new(
|
||||
&mut self.context,
|
||||
self.stream.next_packet().await?,
|
||||
))?;
|
||||
|
||||
@ -7,9 +7,8 @@ pub use protocol::{
|
||||
AuthenticationSwitchRequestPacket, BufMut, Capabilities, ColumnDefPacket, ColumnPacket,
|
||||
ComDebug, ComInitDb, ComPing, ComProcessKill, ComQuery, ComQuit, ComResetConnection,
|
||||
ComSetOption, ComShutdown, ComSleep, ComStatistics, ComStmtClose, ComStmtExec, ComStmtFetch,
|
||||
ComStmtPrepare, ComStmtPrepareOk, ComStmtPrepareResp, DeContext, Decoder, Deserialize, Encode,
|
||||
ComStmtPrepare, ComStmtPrepareOk, ComStmtPrepareResp, DeContext, Decode, Decoder, Encode,
|
||||
EofPacket, ErrPacket, ErrorCode, FieldDetailFlag, FieldType, HandshakeResponsePacket,
|
||||
InitialHandshakePacket, Message, OkPacket, PacketHeader, ResultRow, ResultSet,
|
||||
SSLRequestPacket, ServerStatusFlag, SessionChangeType, SetOptionOptions, ShutdownOptions,
|
||||
StmtExecFlag,
|
||||
InitialHandshakePacket, OkPacket, PacketHeader, ResultRow, ResultSet, SSLRequestPacket,
|
||||
ServerStatusFlag, SessionChangeType, SetOptionOptions, ShutdownOptions, StmtExecFlag,
|
||||
};
|
||||
|
||||
@ -1,9 +1,59 @@
|
||||
use crate::mariadb::{ColumnDefPacket, ConnContext, Connection, Framed, PacketHeader};
|
||||
use byteorder::{ByteOrder, LittleEndian};
|
||||
use bytes::Bytes;
|
||||
use failure::{err_msg, Error};
|
||||
|
||||
// Deserializing bytes and string do the same thing. Except that string also has a null terminated deserialzer
|
||||
use super::packets::packet_header::PacketHeader;
|
||||
// Decoding trait that is implemented by all packets
|
||||
pub trait Decode: Sized {
|
||||
fn decode(ctx: &mut DeContext) -> Result<Self, Error>;
|
||||
}
|
||||
|
||||
// A wrapper around a connection context to prevent
|
||||
// deserializers from touching the stream, yet still have
|
||||
// access to the connection context.
|
||||
// Mainly used to simply to simplify number of parameters for deserializing functions
|
||||
pub struct DeContext<'a> {
|
||||
pub ctx: &'a mut ConnContext,
|
||||
pub stream: Option<&'a mut Framed>,
|
||||
pub decoder: Decoder,
|
||||
pub columns: Option<u64>,
|
||||
pub column_defs: Option<Vec<ColumnDefPacket>>,
|
||||
}
|
||||
|
||||
impl<'a> DeContext<'a> {
|
||||
pub fn new(conn: &'a mut ConnContext, buf: Bytes) -> Self {
|
||||
DeContext {
|
||||
ctx: conn,
|
||||
stream: None,
|
||||
decoder: Decoder::new(buf),
|
||||
columns: None,
|
||||
column_defs: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_stream(conn: &'a mut ConnContext, stream: &'a mut Framed) -> Self {
|
||||
DeContext {
|
||||
ctx: conn,
|
||||
stream: Some(stream),
|
||||
decoder: Decoder::new(Bytes::new()),
|
||||
columns: None,
|
||||
column_defs: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn next_packet(&mut self) -> Result<(), failure::Error> {
|
||||
if let Some(stream) = &mut self.stream {
|
||||
self.decoder = Decoder::new(stream.next_packet().await?);
|
||||
|
||||
return Ok(());
|
||||
} else if self.decoder.buf.len() > 0 {
|
||||
// There is still data in the buffer
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
failure::bail!("Calling next_packet on DeContext with no stream provided")
|
||||
}
|
||||
}
|
||||
|
||||
// This is a simple wrapper around Bytes to make decoding easier
|
||||
// since the index is always tracked
|
||||
|
||||
@ -1,54 +0,0 @@
|
||||
use crate::mariadb::{ColumnDefPacket, ConnContext, Connection, Decoder, Framed};
|
||||
use bytes::Bytes;
|
||||
use failure::Error;
|
||||
|
||||
// A wrapper around a connection context to prevent
|
||||
// deserializers from touching the stream, yet still have
|
||||
// access to the connection context.
|
||||
// Mainly used to simply to simplify number of parameters for deserializing functions
|
||||
pub struct DeContext<'a> {
|
||||
pub ctx: &'a mut ConnContext,
|
||||
pub stream: Option<&'a mut Framed>,
|
||||
pub decoder: Decoder,
|
||||
pub columns: Option<u64>,
|
||||
pub column_defs: Option<Vec<ColumnDefPacket>>,
|
||||
}
|
||||
|
||||
impl<'a> DeContext<'a> {
|
||||
pub fn new(conn: &'a mut ConnContext, buf: Bytes) -> Self {
|
||||
DeContext {
|
||||
ctx: conn,
|
||||
stream: None,
|
||||
decoder: Decoder::new(buf),
|
||||
columns: None,
|
||||
column_defs: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_stream(conn: &'a mut ConnContext, stream: &'a mut Framed) -> Self {
|
||||
DeContext {
|
||||
ctx: conn,
|
||||
stream: Some(stream),
|
||||
decoder: Decoder::new(Bytes::new()),
|
||||
columns: None,
|
||||
column_defs: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn next_packet(&mut self) -> Result<(), failure::Error> {
|
||||
if let Some(stream) = &mut self.stream {
|
||||
self.decoder = Decoder::new(stream.next_packet().await?);
|
||||
|
||||
return Ok(());
|
||||
} else if self.decoder.buf.len() > 0 {
|
||||
// There is still data in the buffer
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
failure::bail!("Calling next_packet on DeContext with no stream provided")
|
||||
}
|
||||
}
|
||||
|
||||
pub trait Deserialize: Sized {
|
||||
fn deserialize(ctx: &mut DeContext) -> Result<Self, Error>;
|
||||
}
|
||||
@ -87,6 +87,9 @@ pub trait BufMut {
|
||||
#[inline]
|
||||
fn put_string_null(&mut self, string: &Bytes);
|
||||
|
||||
// TODO: Combine with previous method
|
||||
fn put_str_null(&mut self, string: &str);
|
||||
|
||||
// Encode a string<fix>; a string of fixed length
|
||||
#[inline]
|
||||
fn put_string_fix(&mut self, bytes: &Bytes, size: usize);
|
||||
@ -270,13 +273,21 @@ impl BufMut for Vec<u8> {
|
||||
}
|
||||
}
|
||||
|
||||
// Encode a string<null>; a null termianted string (C style)
|
||||
// Encode a string<null>; a null terminated string (C style)
|
||||
#[inline]
|
||||
fn put_string_null(&mut self, string: &Bytes) {
|
||||
self.extend_from_slice(string);
|
||||
self.push(0_u8);
|
||||
}
|
||||
|
||||
// TODO: Combine this method with the previous
|
||||
// Encode a string<null>; a null terminated string (C style)
|
||||
#[inline]
|
||||
fn put_str_null(&mut self, string: &str) {
|
||||
self.extend_from_slice(string.as_bytes());
|
||||
self.push(0_u8);
|
||||
}
|
||||
|
||||
// Encode a string<fix>; a string of fixed length
|
||||
#[inline]
|
||||
fn put_string_fix(&mut self, bytes: &Bytes, size: usize) {
|
||||
|
||||
@ -8,11 +8,9 @@
|
||||
// TODO: Handle when capability is set, but field is None
|
||||
|
||||
pub mod decode;
|
||||
pub mod deserialize;
|
||||
pub mod encode;
|
||||
pub mod error_codes;
|
||||
pub mod packets;
|
||||
pub mod server;
|
||||
pub mod types;
|
||||
|
||||
// Re-export all the things
|
||||
@ -25,16 +23,12 @@ pub use packets::{
|
||||
SetOptionOptions, ShutdownOptions,
|
||||
};
|
||||
|
||||
pub use decode::Decoder;
|
||||
|
||||
pub use deserialize::{DeContext, Deserialize};
|
||||
pub use decode::{DeContext, Decode, Decoder};
|
||||
|
||||
pub use encode::{BufMut, Encode};
|
||||
|
||||
pub use error_codes::ErrorCode;
|
||||
|
||||
pub use server::Message;
|
||||
|
||||
pub use types::{
|
||||
Capabilities, FieldDetailFlag, FieldType, ServerStatusFlag, SessionChangeType, StmtExecFlag,
|
||||
};
|
||||
|
||||
@ -8,8 +8,8 @@ pub struct ComStmtPrepareOk {
|
||||
pub warnings: i16,
|
||||
}
|
||||
|
||||
impl crate::mariadb::Deserialize for ComStmtPrepareOk {
|
||||
fn deserialize(ctx: &mut crate::mariadb::DeContext) -> Result<Self, failure::Error> {
|
||||
impl crate::mariadb::Decode for ComStmtPrepareOk {
|
||||
fn decode(ctx: &mut crate::mariadb::DeContext) -> Result<Self, failure::Error> {
|
||||
let decoder = &mut ctx.decoder;
|
||||
let length = decoder.decode_length()?;
|
||||
let seq_no = decoder.decode_int_u8();
|
||||
@ -40,7 +40,7 @@ mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
__bytes_builder,
|
||||
mariadb::{ConnContext, DeContext, Deserialize},
|
||||
mariadb::{ConnContext, DeContext, Decode},
|
||||
ConnectOptions,
|
||||
};
|
||||
use bytes::Bytes;
|
||||
@ -70,7 +70,7 @@ mod tests {
|
||||
let mut context = ConnContext::new();
|
||||
let mut ctx = DeContext::new(&mut context, buf);
|
||||
|
||||
let message = ComStmtPrepareOk::deserialize(&mut ctx)?;
|
||||
let message = ComStmtPrepareOk::decode(&mut ctx)?;
|
||||
|
||||
assert_eq!(message.stmt_id, 1);
|
||||
assert_eq!(message.columns, 10);
|
||||
|
||||
@ -1,5 +1,5 @@
|
||||
use crate::mariadb::{
|
||||
Capabilities, ColumnDefPacket, ComStmtPrepareOk, DeContext, Deserialize, EofPacket, Framed,
|
||||
Capabilities, ColumnDefPacket, ComStmtPrepareOk, DeContext, Decode, EofPacket, Framed,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
@ -11,14 +11,14 @@ pub struct ComStmtPrepareResp {
|
||||
|
||||
impl ComStmtPrepareResp {
|
||||
pub async fn deserialize<'a>(mut ctx: DeContext<'a>) -> Result<Self, failure::Error> {
|
||||
let ok = ComStmtPrepareOk::deserialize(&mut ctx)?;
|
||||
let ok = ComStmtPrepareOk::decode(&mut ctx)?;
|
||||
|
||||
let param_defs = if ok.params > 0 {
|
||||
let mut param_defs = Vec::new();
|
||||
|
||||
for _ in 0..ok.params {
|
||||
ctx.next_packet().await?;
|
||||
param_defs.push(ColumnDefPacket::deserialize(&mut ctx)?);
|
||||
param_defs.push(ColumnDefPacket::decode(&mut ctx)?);
|
||||
}
|
||||
|
||||
ctx.next_packet().await?;
|
||||
@ -28,7 +28,7 @@ impl ComStmtPrepareResp {
|
||||
.capabilities
|
||||
.contains(Capabilities::CLIENT_DEPRECATE_EOF)
|
||||
{
|
||||
EofPacket::deserialize(&mut ctx)?;
|
||||
EofPacket::decode(&mut ctx)?;
|
||||
}
|
||||
|
||||
Some(param_defs)
|
||||
@ -41,7 +41,7 @@ impl ComStmtPrepareResp {
|
||||
|
||||
for _ in 0..ok.columns {
|
||||
ctx.next_packet().await?;
|
||||
res_columns.push(ColumnDefPacket::deserialize(&mut ctx)?);
|
||||
res_columns.push(ColumnDefPacket::decode(&mut ctx)?);
|
||||
}
|
||||
|
||||
ctx.next_packet().await?;
|
||||
@ -51,7 +51,7 @@ impl ComStmtPrepareResp {
|
||||
.capabilities
|
||||
.contains(Capabilities::CLIENT_DEPRECATE_EOF)
|
||||
{
|
||||
EofPacket::deserialize(&mut ctx)?;
|
||||
EofPacket::decode(&mut ctx)?;
|
||||
}
|
||||
|
||||
Some(res_columns)
|
||||
@ -72,7 +72,7 @@ mod test {
|
||||
use super::*;
|
||||
use crate::{
|
||||
__bytes_builder,
|
||||
mariadb::{ConnContext, DeContext, Deserialize},
|
||||
mariadb::{ConnContext, DeContext, Decode},
|
||||
ConnectOptions,
|
||||
};
|
||||
|
||||
|
||||
@ -6,8 +6,8 @@ pub struct ResultRow {
|
||||
pub columns: Vec<Option<Bytes>>,
|
||||
}
|
||||
|
||||
impl crate::mariadb::Deserialize for ResultRow {
|
||||
fn deserialize(ctx: &mut crate::mariadb::DeContext) -> Result<Self, failure::Error> {
|
||||
impl crate::mariadb::Decode for ResultRow {
|
||||
fn decode(ctx: &mut crate::mariadb::DeContext) -> Result<Self, failure::Error> {
|
||||
let decoder = &mut ctx.decoder;
|
||||
|
||||
let length = decoder.decode_length()?;
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
use failure::Error;
|
||||
|
||||
use crate::mariadb::{DeContext, Deserialize};
|
||||
use crate::mariadb::{DeContext, Decode};
|
||||
|
||||
// The column packet is the first packet of a result set.
|
||||
// Inside of it it contains the number of columns in the result set
|
||||
@ -12,8 +12,8 @@ pub struct ColumnPacket {
|
||||
pub columns: Option<u64>,
|
||||
}
|
||||
|
||||
impl Deserialize for ColumnPacket {
|
||||
fn deserialize(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
impl Decode for ColumnPacket {
|
||||
fn decode(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
let decoder = &mut ctx.decoder;
|
||||
|
||||
let length = decoder.decode_length()?;
|
||||
@ -54,7 +54,7 @@ mod test {
|
||||
let mut context = ConnContext::new();
|
||||
let mut ctx = DeContext::new(&mut context, buf);
|
||||
|
||||
let message = ColumnPacket::deserialize(&mut ctx)?;
|
||||
let message = ColumnPacket::decode(&mut ctx)?;
|
||||
|
||||
assert_eq!(message.columns, None);
|
||||
|
||||
@ -78,7 +78,7 @@ mod test {
|
||||
let mut context = ConnContext::new();
|
||||
let mut ctx = DeContext::new(&mut context, buf);
|
||||
|
||||
let message = ColumnPacket::deserialize(&mut ctx)?;
|
||||
let message = ColumnPacket::decode(&mut ctx)?;
|
||||
|
||||
assert_eq!(message.columns, Some(0x010101));
|
||||
|
||||
@ -102,7 +102,7 @@ mod test {
|
||||
let mut context = ConnContext::new();
|
||||
let mut ctx = DeContext::new(&mut context, buf);
|
||||
|
||||
let message = ColumnPacket::deserialize(&mut ctx)?;
|
||||
let message = ColumnPacket::decode(&mut ctx)?;
|
||||
|
||||
assert_ne!(message.columns, Some(0x0100));
|
||||
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{DeContext, Deserialize, FieldDetailFlag, FieldType};
|
||||
use crate::mariadb::{DeContext, Decode, FieldDetailFlag, FieldType};
|
||||
use bytes::Bytes;
|
||||
use failure::Error;
|
||||
use std::convert::TryFrom;
|
||||
@ -21,8 +21,8 @@ pub struct ColumnDefPacket {
|
||||
pub decimals: u8,
|
||||
}
|
||||
|
||||
impl Deserialize for ColumnDefPacket {
|
||||
fn deserialize(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
impl Decode for ColumnDefPacket {
|
||||
fn decode(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
let decoder = &mut ctx.decoder;
|
||||
let length = decoder.decode_length()?;
|
||||
let seq_no = decoder.decode_int_u8();
|
||||
@ -120,7 +120,7 @@ mod test {
|
||||
let mut context = ConnContext::new();
|
||||
let mut ctx = DeContext::new(&mut context, buf);
|
||||
|
||||
let message = ColumnDefPacket::deserialize(&mut ctx)?;
|
||||
let message = ColumnDefPacket::decode(&mut ctx)?;
|
||||
|
||||
assert_eq!(&message.catalog[..], b"a");
|
||||
assert_eq!(&message.schema[..], b"b");
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{DeContext, Decoder, Deserialize, ErrorCode, ServerStatusFlag};
|
||||
use crate::mariadb::{DeContext, Decode, Decoder, ErrorCode, ServerStatusFlag};
|
||||
use bytes::Bytes;
|
||||
use failure::Error;
|
||||
use std::convert::TryFrom;
|
||||
@ -11,8 +11,8 @@ pub struct EofPacket {
|
||||
pub status: ServerStatusFlag,
|
||||
}
|
||||
|
||||
impl Deserialize for EofPacket {
|
||||
fn deserialize(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
impl Decode for EofPacket {
|
||||
fn decode(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
let decoder = &mut ctx.decoder;
|
||||
|
||||
let length = decoder.decode_length()?;
|
||||
@ -63,7 +63,7 @@ mod test {
|
||||
let mut context = ConnContext::new();
|
||||
let mut ctx = DeContext::new(&mut context, buf);
|
||||
|
||||
let _message = EofPacket::deserialize(&mut ctx)?;
|
||||
let _message = EofPacket::decode(&mut ctx)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -3,7 +3,7 @@ use std::convert::TryFrom;
|
||||
use bytes::Bytes;
|
||||
use failure::Error;
|
||||
|
||||
use crate::mariadb::{DeContext, Deserialize, ErrorCode};
|
||||
use crate::mariadb::{DeContext, Decode, ErrorCode};
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct ErrPacket {
|
||||
@ -19,8 +19,8 @@ pub struct ErrPacket {
|
||||
pub error_message: Option<Bytes>,
|
||||
}
|
||||
|
||||
impl Deserialize for ErrPacket {
|
||||
fn deserialize(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
impl Decode for ErrPacket {
|
||||
fn decode(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
let decoder = &mut ctx.decoder;
|
||||
let length = decoder.decode_length()?;
|
||||
let seq_no = decoder.decode_int_u8();
|
||||
@ -129,7 +129,7 @@ mod test {
|
||||
let mut context = ConnContext::new();
|
||||
let mut ctx = DeContext::new(&mut context, buf);
|
||||
|
||||
let _message = ErrPacket::deserialize(&mut ctx)?;
|
||||
let _message = ErrPacket::decode(&mut ctx)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -3,12 +3,12 @@ use bytes::Bytes;
|
||||
use failure::Error;
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct HandshakeResponsePacket {
|
||||
pub struct HandshakeResponsePacket<'a> {
|
||||
pub capabilities: Capabilities,
|
||||
pub max_packet_size: u32,
|
||||
pub collation: u8,
|
||||
pub extended_capabilities: Option<Capabilities>,
|
||||
pub username: Bytes,
|
||||
pub username: &'a str,
|
||||
pub auth_data: Option<Bytes>,
|
||||
pub auth_response_len: Option<u8>,
|
||||
pub auth_response: Option<Bytes>,
|
||||
@ -18,7 +18,7 @@ pub struct HandshakeResponsePacket {
|
||||
pub conn_attr: Option<Vec<(Bytes, Bytes)>>,
|
||||
}
|
||||
|
||||
impl Encode for HandshakeResponsePacket {
|
||||
impl<'a> Encode for HandshakeResponsePacket<'a> {
|
||||
fn encode(&self, buf: &mut Vec<u8>, ctx: &mut ConnContext) -> Result<(), Error> {
|
||||
buf.alloc_packet_header();
|
||||
buf.seq_no(1);
|
||||
@ -40,7 +40,7 @@ impl Encode for HandshakeResponsePacket {
|
||||
buf.put_byte_fix(&Bytes::from_static(&[0u8; 4]), 4);
|
||||
}
|
||||
|
||||
buf.put_string_null(&self.username);
|
||||
buf.put_str_null(self.username);
|
||||
|
||||
if !(ctx.capabilities & Capabilities::PLUGIN_AUTH_LENENC_CLIENT_DATA).is_empty() {
|
||||
if let Some(auth_data) = &self.auth_data {
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{Capabilities, DeContext, Deserialize, ServerStatusFlag};
|
||||
use crate::mariadb::{Capabilities, DeContext, Decode, ServerStatusFlag};
|
||||
use bytes::Bytes;
|
||||
use failure::{err_msg, Error};
|
||||
|
||||
@ -18,8 +18,8 @@ pub struct InitialHandshakePacket {
|
||||
pub auth_plugin_name: Option<Bytes>,
|
||||
}
|
||||
|
||||
impl Deserialize for InitialHandshakePacket {
|
||||
fn deserialize(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
impl Decode for InitialHandshakePacket {
|
||||
fn decode(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
let decoder = &mut ctx.decoder;
|
||||
let length = decoder.decode_length()?;
|
||||
let seq_no = decoder.decode_int_u8();
|
||||
@ -160,7 +160,7 @@ mod test {
|
||||
let mut context = ConnContext::new();
|
||||
let mut ctx = DeContext::new(&mut context, buf);
|
||||
|
||||
let _message = InitialHandshakePacket::deserialize(&mut ctx)?;
|
||||
let _message = InitialHandshakePacket::decode(&mut ctx)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -1,7 +1,7 @@
|
||||
use bytes::Bytes;
|
||||
use failure::{err_msg, Error};
|
||||
|
||||
use crate::mariadb::{DeContext, Deserialize, ServerStatusFlag};
|
||||
use crate::mariadb::{DeContext, Decode, ServerStatusFlag};
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct OkPacket {
|
||||
@ -16,8 +16,8 @@ pub struct OkPacket {
|
||||
pub value: Option<Bytes>,
|
||||
}
|
||||
|
||||
impl Deserialize for OkPacket {
|
||||
fn deserialize(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
impl Decode for OkPacket {
|
||||
fn decode(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
let decoder = &mut ctx.decoder;
|
||||
|
||||
// Packet header
|
||||
@ -100,7 +100,7 @@ mod test {
|
||||
let mut context = ConnContext::new();
|
||||
let mut ctx = DeContext::new(&mut context, buf);
|
||||
|
||||
let message = OkPacket::deserialize(&mut ctx)?;
|
||||
let message = OkPacket::decode(&mut ctx)?;
|
||||
|
||||
assert_eq!(message.affected_rows, None);
|
||||
assert_eq!(message.last_insert_id, None);
|
||||
|
||||
@ -1,4 +1,4 @@
|
||||
use crate::mariadb::{DeContext, Decoder, Deserialize, ErrorCode, ServerStatusFlag};
|
||||
use crate::mariadb::{DeContext, Decode, Decoder, ErrorCode, ServerStatusFlag};
|
||||
use bytes::Bytes;
|
||||
use failure::Error;
|
||||
use std::convert::TryFrom;
|
||||
@ -10,8 +10,8 @@ pub struct ResultRow {
|
||||
pub row: Vec<Bytes>,
|
||||
}
|
||||
|
||||
impl Deserialize for ResultRow {
|
||||
fn deserialize(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
impl Decode for ResultRow {
|
||||
fn decode(ctx: &mut DeContext) -> Result<Self, Error> {
|
||||
let decoder = &mut ctx.decoder;
|
||||
|
||||
let length = decoder.decode_length()?;
|
||||
@ -60,7 +60,7 @@ mod test {
|
||||
|
||||
ctx.columns = Some(1);
|
||||
|
||||
let _message = ResultRow::deserialize(&mut ctx)?;
|
||||
let _message = ResultRow::decode(&mut ctx)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@ -2,8 +2,8 @@ use bytes::Bytes;
|
||||
use failure::Error;
|
||||
|
||||
use crate::mariadb::{
|
||||
Capabilities, ColumnDefPacket, ColumnPacket, ConnContext, DeContext, Decoder, Deserialize,
|
||||
EofPacket, ErrPacket, Framed, Message, OkPacket, ResultRow,
|
||||
Capabilities, ColumnDefPacket, ColumnPacket, ConnContext, DeContext, Decode, Decoder,
|
||||
EofPacket, ErrPacket, Framed, OkPacket, ResultRow,
|
||||
};
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
@ -15,7 +15,7 @@ pub struct ResultSet {
|
||||
|
||||
impl ResultSet {
|
||||
pub async fn deserialize<'a>(mut ctx: DeContext<'a>) -> Result<Self, Error> {
|
||||
let column_packet = ColumnPacket::deserialize(&mut ctx)?;
|
||||
let column_packet = ColumnPacket::decode(&mut ctx)?;
|
||||
|
||||
println!("{:?}", column_packet);
|
||||
|
||||
@ -23,7 +23,7 @@ impl ResultSet {
|
||||
let mut column_defs = Vec::new();
|
||||
for _ in 0..columns {
|
||||
ctx.next_packet().await?;
|
||||
column_defs.push(ColumnDefPacket::deserialize(&mut ctx)?);
|
||||
column_defs.push(ColumnDefPacket::decode(&mut ctx)?);
|
||||
}
|
||||
column_defs
|
||||
} else {
|
||||
@ -40,7 +40,7 @@ impl ResultSet {
|
||||
.contains(Capabilities::CLIENT_DEPRECATE_EOF)
|
||||
{
|
||||
// If we get an eof packet we must update ctx to hold a new buffer of the next packet.
|
||||
let eof_packet = Some(EofPacket::deserialize(&mut ctx)?);
|
||||
let eof_packet = Some(EofPacket::decode(&mut ctx)?);
|
||||
println!("{:?}", eof_packet);
|
||||
ctx.next_packet().await?;
|
||||
eof_packet
|
||||
@ -63,7 +63,7 @@ impl ResultSet {
|
||||
break;
|
||||
} else {
|
||||
let index = ctx.decoder.index;
|
||||
match ResultRow::deserialize(&mut ctx) {
|
||||
match ResultRow::decode(&mut ctx) {
|
||||
Ok(v) => {
|
||||
rows.push(v);
|
||||
ctx.next_packet().await?;
|
||||
@ -82,9 +82,9 @@ impl ResultSet {
|
||||
.capabilities
|
||||
.contains(Capabilities::CLIENT_DEPRECATE_EOF)
|
||||
{
|
||||
OkPacket::deserialize(&mut ctx)?;
|
||||
OkPacket::decode(&mut ctx)?;
|
||||
} else {
|
||||
EofPacket::deserialize(&mut ctx)?;
|
||||
EofPacket::decode(&mut ctx)?;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -1,32 +0,0 @@
|
||||
// Reference: https://mariadb.com/kb/en/library/connection
|
||||
|
||||
use failure::Error;
|
||||
|
||||
use super::{
|
||||
deserialize::{DeContext, Deserialize},
|
||||
packets::{err::ErrPacket, initial::InitialHandshakePacket, ok::OkPacket},
|
||||
};
|
||||
|
||||
#[derive(Debug)]
|
||||
#[non_exhaustive]
|
||||
pub enum Message {
|
||||
InitialHandshakePacket(InitialHandshakePacket),
|
||||
OkPacket(OkPacket),
|
||||
ErrPacket(ErrPacket),
|
||||
}
|
||||
|
||||
impl Message {
|
||||
pub fn deserialize(ctx: &mut DeContext) -> Result<Option<Self>, Error> {
|
||||
let decoder = &mut ctx.decoder;
|
||||
let _packet_header = match decoder.peek_packet_header() {
|
||||
Ok(v) => v,
|
||||
Err(_) => return Ok(None),
|
||||
};
|
||||
|
||||
Ok(Some(match decoder.peek_tag() {
|
||||
0xFF => Message::ErrPacket(ErrPacket::deserialize(ctx)?),
|
||||
0x00 | 0xFE => Message::OkPacket(OkPacket::deserialize(ctx)?),
|
||||
_ => unimplemented!(),
|
||||
}))
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user