WIP: Work on PasswordMessage and StartupMessage

This commit is contained in:
Ryan Leckey 2019-06-21 22:57:14 -07:00
parent d3121c60cf
commit 6c02124eab
4 changed files with 61 additions and 1 deletions

View File

@ -7,6 +7,8 @@ mod encode;
mod message;
mod ready_for_query;
mod response;
mod startup_message;
mod password_message;
pub use self::{
decode::Decode,
@ -14,4 +16,6 @@ pub use self::{
message::Message,
ready_for_query::{ReadyForQuery, TransactionStatus},
response::{Response, ResponseBuilder, Severity},
startup_message::StartupMessage,
password_message::PasswordMessage,
};

View File

@ -40,7 +40,6 @@ impl Decode for Message {
let src = src.slice(pos, pos + len - 4);
Ok(match token {
// FIXME: These tokens are duplicated here and in the respective encode functions
b'N' | b'E' => Message::Response(Response::decode(src)?),
b'Z' => Message::ReadyForQuery(ReadyForQuery::decode(src)?),

View File

@ -0,0 +1,49 @@
use bytes::Bytes;
use std::io;
use crate::{Encode, Decode};
pub struct PasswordMessage {
password: Bytes,
}
impl PasswordMessage {
pub fn cleartext(s: impl AsRef<str>) -> Self {
// TODO
unimplemented!()
}
pub fn md5(s: impl AsRef<str>) -> Self {
// TODO
unimplemented!()
}
/// The password (encrypted, if requested).
pub fn password(&self) -> &[u8] {
&self.password
}
}
impl Decode for PasswordMessage {
fn decode(src: Bytes) -> io::Result<Self> where
Self: Sized {
// There is only one field, the password, and it's not like we can
// decrypt it if it was encrypted
Ok(PasswordMessage { password: src })
}
}
impl Encode for PasswordMessage {
fn size_hint(&self) -> usize {
self.password.len() + 5
}
fn encode(&self, buf: &mut Vec<u8>) -> io::Result<()> {
buf.push(b'p');
buf.copy_from_slice((self.password.len() + 4).to_be_bytes());
buf.copy_from_slice(&self.password);
Ok(())
}
}
// TODO: Encode and Decode tests

View File

@ -0,0 +1,8 @@
use bytes::Bytes;
use std::io;
use crate::{Encode, Decode};
pub struct StartupMessage {
version: u32,
params: Bytes,
}