Minor clean up

This commit is contained in:
Ryan Leckey 2019-06-28 21:36:46 -07:00
parent b81eda3673
commit aa0b27af35
12 changed files with 64 additions and 24 deletions

View file

@ -10,7 +10,13 @@ pub struct ConnectOptions<'a> {
impl<'a> Default for ConnectOptions<'a> {
#[inline]
fn default() -> Self {
Self { host: "localhost", port: 5432, user: None, database: None, password: None }
Self {
host: "localhost",
port: 5432,
user: None,
database: None,
password: None,
}
}
}

View file

@ -17,8 +17,7 @@ const ESTABLISH: &[u8] = b"\
S\0\0\0#session_authorization\0postgres\0\
S\0\0\0#standard_conforming_strings\0on\0\
S\0\0\0\x11TimeZone\0UTC\0\
K\0\0\0\x0c\0\0'\xc6\x89\
R\xc5+\
K\0\0\0\x0c\0\0'\xc6\x89R\xc5+\
Z\0\0\0\x05I";
#[runtime::main]

View file

@ -3,7 +3,7 @@ extern crate criterion;
use bytes::Bytes;
use criterion::{black_box, Criterion};
use sqlx_postgres_protocol::{Decode, ParameterStatus, BackendKeyData, Response};
use sqlx_postgres_protocol::{BackendKeyData, Decode, ParameterStatus, Response};
fn criterion_benchmark(c: &mut Criterion) {
const NOTICE_RESPONSE: &[u8] = b"SNOTICE\0VNOTICE\0C42710\0Mextension \"uuid-ossp\" already exists, skipping\0Fextension.c\0L1656\0RCreateExtension\0\0";
@ -18,7 +18,8 @@ fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("decode BackendKeyData", |b| {
b.iter(|| {
let _ = BackendKeyData::decode(black_box(Bytes::from_static(BACKEND_KEY_DATA))).unwrap();
let _ =
BackendKeyData::decode(black_box(Bytes::from_static(BACKEND_KEY_DATA))).unwrap();
})
});

View file

@ -27,14 +27,17 @@ impl Decode for BackendKeyData {
let process_id = BigEndian::read_u32(&src[..4]);
let secret_key = BigEndian::read_u32(&src[4..]);
Ok(Self { process_id, secret_key })
Ok(Self {
process_id,
secret_key,
})
}
}
#[cfg(test)]
mod tests {
use super::{BackendKeyData};
use crate::{Decode};
use super::BackendKeyData;
use crate::Decode;
use bytes::Bytes;
use std::io;

View file

@ -35,8 +35,8 @@ impl Decode for ParameterStatus {
#[cfg(test)]
mod tests {
use super::{ParameterStatus};
use crate::{Decode};
use super::ParameterStatus;
use crate::Decode;
use bytes::Bytes;
use std::io;

View file

@ -11,7 +11,9 @@ pub struct PasswordMessage {
impl PasswordMessage {
/// Create a `PasswordMessage` with an unecrypted password.
pub fn cleartext(password: &str) -> Self {
Self { password: Bytes::from(password) }
Self {
password: Bytes::from(password),
}
}
/// Create a `PasswordMessage` by hasing the password, user, and salt together using MD5.
@ -32,7 +34,9 @@ impl PasswordMessage {
password.extend_from_slice(b"md5");
password.extend_from_slice(salted.as_bytes());
Self { password: Bytes::from(password) }
Self {
password: Bytes::from(password),
}
}
/// The password (encrypted, if requested).

View file

@ -67,7 +67,9 @@ mod tests {
#[test]
fn it_encodes_ready_for_query() -> io::Result<()> {
let message = ReadyForQuery { status: TransactionStatus::Error };
let message = ReadyForQuery {
status: TransactionStatus::Error,
};
let mut dst = Vec::with_capacity(message.size_hint());
message.encode(&mut dst)?;

View file

@ -314,12 +314,19 @@ impl Decode for Response {
}
b'P' => {
position = Some(field_value.parse().map_err(|_| io::ErrorKind::InvalidData)?);
position = Some(
field_value
.parse()
.map_err(|_| io::ErrorKind::InvalidData)?,
);
}
b'p' => {
internal_position =
Some(field_value.parse().map_err(|_| io::ErrorKind::InvalidData)?);
internal_position = Some(
field_value
.parse()
.map_err(|_| io::ErrorKind::InvalidData)?,
);
}
b'q' => {
@ -355,7 +362,11 @@ impl Decode for Response {
}
b'L' => {
line = Some(field_value.parse().map_err(|_| io::ErrorKind::InvalidData)?);
line = Some(
field_value
.parse()
.map_err(|_| io::ErrorKind::InvalidData)?,
);
}
b'R' => {
@ -718,7 +729,10 @@ mod tests {
let message = Response::decode(src)?;
assert_eq!(message.severity(), Severity::Notice);
assert_eq!(message.message(), "extension \"uuid-ossp\" already exists, skipping");
assert_eq!(
message.message(),
"extension \"uuid-ossp\" already exists, skipping"
);
assert_eq!(message.code(), "42710");
assert_eq!(message.file(), Some("extension.c"));
assert_eq!(message.line(), Some(1656));

View file

@ -47,7 +47,10 @@ pub struct StartupMessageBuilder {
impl Default for StartupMessageBuilder {
fn default() -> Self {
StartupMessageBuilder { version: (3, 0), params: BytesMut::with_capacity(156) }
StartupMessageBuilder {
version: (3, 0),
params: BytesMut::with_capacity(156),
}
}
}
@ -76,7 +79,10 @@ impl StartupMessageBuilder {
self.params.reserve(1);
self.params.put_u8(0);
StartupMessage { version: self.version, params: self.params.freeze() }
StartupMessage {
version: self.version,
params: self.params.freeze(),
}
}
}

View file

@ -48,7 +48,10 @@ pub async fn establish<'a, 'b: 'a>(
Message::Authentication(Authentication::CleartextPassword) => {
// FIXME: Should error early (before send) if the user did not supply a password
conn.send(PasswordMessage::cleartext(options.password.unwrap_or_default())).await?;
conn.send(PasswordMessage::cleartext(
options.password.unwrap_or_default(),
))
.await?;
}
Message::Authentication(Authentication::Md5Password { salt }) => {

View file

@ -117,8 +117,6 @@ async fn receiver(
}
while len > 0 {
log::trace!("rbuf: {:?}", rbuf);
let size = rbuf.len();
let message = Message::decode(&mut rbuf)?;
len -= size - rbuf.len();

View file

@ -7,7 +7,11 @@ async fn main() -> Result<(), failure::Error> {
env_logger::try_init()?;
let conn = Connection::establish(
ConnectOptions::new().port(5433).user("postgres").password("password"),
ConnectOptions::new()
.host("127.0.0.1")
.port(5433)
.user("postgres")
.password("password"),
)
.await?;