From 0a5b527d79281da33f9dc41a03ff8f98b447802b Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Tue, 14 Jan 2020 10:35:50 -0800 Subject: [PATCH] fix import errors and run rustfmt --- sqlx-core/src/error.rs | 1 - sqlx-core/src/io/mod.rs | 2 +- sqlx-core/src/io/tls.rs | 1 + sqlx-core/src/mysql/connection.rs | 6 +- sqlx-core/src/mysql/protocol/mod.rs | 4 +- sqlx-core/src/mysql/protocol/ssl_request.rs | 5 +- sqlx-core/src/postgres/connection.rs | 77 +++++++++++-------- .../src/postgres/protocol/ssl_request.rs | 4 +- sqlx-core/src/url.rs | 12 +-- 9 files changed, 60 insertions(+), 52 deletions(-) diff --git a/sqlx-core/src/error.rs b/sqlx-core/src/error.rs index 960023d5a..f3d0244a2 100644 --- a/sqlx-core/src/error.rs +++ b/sqlx-core/src/error.rs @@ -243,4 +243,3 @@ macro_rules! impl_fmt_error { } }; } - diff --git a/sqlx-core/src/io/mod.rs b/sqlx-core/src/io/mod.rs index b4c954301..304c11b65 100644 --- a/sqlx-core/src/io/mod.rs +++ b/sqlx-core/src/io/mod.rs @@ -12,7 +12,7 @@ pub use self::{ buf_mut::BufMut, buf_stream::BufStream, byte_str::ByteStr, - tls::MaybeTlsStream + tls::MaybeTlsStream, }; #[cfg(test)] diff --git a/sqlx-core/src/io/tls.rs b/sqlx-core/src/io/tls.rs index 798c26606..7743ebdcc 100644 --- a/sqlx-core/src/io/tls.rs +++ b/sqlx-core/src/io/tls.rs @@ -29,6 +29,7 @@ impl MaybeTlsStream { }) } + #[allow(dead_code)] pub fn is_tls(&self) -> bool { match self.inner { Inner::NotTls(_) => false, diff --git a/sqlx-core/src/mysql/connection.rs b/sqlx-core/src/mysql/connection.rs index d3b51ac01..df9537aae 100644 --- a/sqlx-core/src/mysql/connection.rs +++ b/sqlx-core/src/mysql/connection.rs @@ -1,7 +1,7 @@ use std::convert::TryInto; use std::io; -use async_std::net::{Shutdown, TcpStream}; +use async_std::net::Shutdown; use byteorder::{ByteOrder, LittleEndian}; use futures_core::future::BoxFuture; use sha1::Sha1; @@ -17,8 +17,6 @@ use crate::mysql::protocol::{ use crate::mysql::rsa; use crate::mysql::util::xor_eq; use crate::url::Url; -use std::borrow::Cow; -use std::path::Path; // Size before a packet is split const MAX_PACKET_SIZE: u32 = 1024; @@ -347,7 +345,7 @@ impl MySqlConnection { clear_text.push_str(password); clear_text.push('\0'); - return Ok(clear_text.into_boxed_bytes()); + return Ok(clear_text.into_bytes().into_boxed_slice()); } // client sends a public key request diff --git a/sqlx-core/src/mysql/protocol/mod.rs b/sqlx-core/src/mysql/protocol/mod.rs index 9da46ec63..3087d4e17 100644 --- a/sqlx-core/src/mysql/protocol/mod.rs +++ b/sqlx-core/src/mysql/protocol/mod.rs @@ -39,9 +39,9 @@ mod com_stmt_prepare_ok; mod eof; mod err; mod handshake_response; -mod ssl_request; mod ok; mod row; +mod ssl_request; pub use auth_switch::AuthSwitch; pub use column_count::ColumnCount; @@ -51,5 +51,5 @@ pub use eof::EofPacket; pub use err::ErrPacket; pub use handshake_response::HandshakeResponse; pub use ok::OkPacket; -pub use ssl_request::SslRequest; pub use row::Row; +pub use ssl_request::SslRequest; diff --git a/sqlx-core/src/mysql/protocol/ssl_request.rs b/sqlx-core/src/mysql/protocol/ssl_request.rs index 346827630..fea5ef8e3 100644 --- a/sqlx-core/src/mysql/protocol/ssl_request.rs +++ b/sqlx-core/src/mysql/protocol/ssl_request.rs @@ -15,7 +15,10 @@ pub struct SslRequest { impl Encode for SslRequest { fn encode(&self, buf: &mut Vec, capabilities: Capabilities) { // SSL must be set or else it makes no sense to ask for an upgrade - assert!(capabilities.contains(Capabilities::SSL), "SSL bit must be set for Capabilities"); + assert!( + capabilities.contains(Capabilities::SSL), + "SSL bit must be set for Capabilities" + ); // client capabilities : int<4> buf.put_u32::(capabilities.bits() as u32); diff --git a/sqlx-core/src/postgres/connection.rs b/sqlx-core/src/postgres/connection.rs index 689a39272..d18b5cc5d 100644 --- a/sqlx-core/src/postgres/connection.rs +++ b/sqlx-core/src/postgres/connection.rs @@ -1,8 +1,5 @@ use std::convert::TryInto; -use std::path::Path; -use async_native_tls::Certificate; -use async_std::fs; use async_std::net::Shutdown; use byteorder::NetworkEndian; use futures_core::future::BoxFuture; @@ -13,13 +10,13 @@ use sha2::{Digest, Sha256}; use crate::cache::StatementCache; use crate::connection::Connection; use crate::io::{Buf, BufStream, MaybeTlsStream}; -use crate::postgres::PgError; use crate::postgres::protocol::{ - self, Authentication, Decode, Encode, hi, Message, SaslInitialResponse, SaslResponse, + self, hi, Authentication, Decode, Encode, Message, SaslInitialResponse, SaslResponse, StatementId, }; -use crate::Result; +use crate::postgres::PgError; use crate::url::Url; +use crate::Result; /// An asynchronous connection to a [Postgres] database. /// @@ -88,9 +85,13 @@ pub struct PgConnection { impl PgConnection { #[cfg(feature = "tls")] - async fn try_ssl(&mut self, url: &Url, invalid_certs: bool, invalid_hostnames: bool) -> crate::Result { - use async_native_tls::{TlsConnector, Certificate}; - use std::env; + async fn try_ssl( + &mut self, + url: &Url, + invalid_certs: bool, + invalid_hostnames: bool, + ) -> crate::Result { + use async_native_tls::TlsConnector; protocol::SslRequest::encode(self.stream.buffer_mut()); @@ -99,8 +100,10 @@ impl PgConnection { match self.stream.peek(1).await? { Some(b"N") => return Ok(false), Some(b"S") => (), - Some(other) => return Err(tls_err!("unexpected single-byte response: 0x{:02X}", other[0]).into()), - None => return Err(tls_err!("server unexpectedly closed connection").into()) + Some(other) => { + return Err(tls_err!("unexpected single-byte response: 0x{:02X}", other[0]).into()) + } + None => return Err(tls_err!("server unexpectedly closed connection").into()), } let mut connector = TlsConnector::new() @@ -112,7 +115,7 @@ impl PgConnection { Ok(cert) => { connector = connector.add_root_certificate(cert); } - Err(e) => log::warn!("failed to read Postgres root certificate: {}", e) + Err(e) => log::warn!("failed to read Postgres root certificate: {}", e), } } @@ -162,7 +165,7 @@ impl PgConnection { protocol::PasswordMessage::ClearText( url.password().unwrap_or_default(), ) - .encode(self.stream.buffer_mut()); + .encode(self.stream.buffer_mut()); self.stream.flush().await?; } @@ -173,7 +176,7 @@ impl PgConnection { user: username, salt, } - .encode(self.stream.buffer_mut()); + .encode(self.stream.buffer_mut()); self.stream.flush().await?; } @@ -216,7 +219,7 @@ impl PgConnection { "requires unimplemented authentication method: {:?}", auth ) - .into()); + .into()); } } } @@ -338,27 +341,37 @@ impl PgConnection { "disable" | "allow" => (), #[cfg(feature = "tls")] - "prefer" => if !self_.try_ssl(&url, true, true).await? { - log::warn!("server does not support TLS, falling back to unsecured connection") - }, + "prefer" => { + if !self_.try_ssl(&url, true, true).await? { + log::warn!("server does not support TLS, falling back to unsecured connection") + } + } #[cfg(not(feature = "tls"))] "prefer" => log::info!("compiled without TLS, skipping upgrade"), #[cfg(feature = "tls")] - "require" | "verify-ca" | "verify-full" => if !self_.try_ssl( - &url, - ssl_mode == "require", // false for both verify-ca and verify-full - ssl_mode != "verify-full" // false for only verify-full - ).await? { - return Err(tls_err!("Postgres server does not support TLS").into()) + "require" | "verify-ca" | "verify-full" => { + if !self_ + .try_ssl( + &url, + ssl_mode == "require", // false for both verify-ca and verify-full + ssl_mode != "verify-full", // false for only verify-full + ) + .await? + { + return Err(tls_err!("Postgres server does not support TLS").into()); + } } #[cfg(not(feature = "tls"))] - "require" | "verify-ca" | "verify-full" => return Err( - tls_err!("sslmode {:?} unsupported; SQLx was compiled without `tls` feature", - ssl_mode).into() - ), + "require" | "verify-ca" | "verify-full" => { + return Err(tls_err!( + "sslmode {:?} unsupported; SQLx was compiled without `tls` feature", + ssl_mode + ) + .into()) + } _ => return Err(tls_err!("unknown `sslmode` value: {:?}", ssl_mode).into()), } @@ -370,9 +383,9 @@ impl PgConnection { impl Connection for PgConnection { fn open(url: T) -> BoxFuture<'static, Result> - where - T: TryInto, - Self: Sized, + where + T: TryInto, + Self: Sized, { Box::pin(PgConnection::open(url.try_into())) } @@ -388,7 +401,7 @@ async fn read_root_certificate(url: &Url) -> crate::Result(8); // 1234 in high 16 bits, 5679 in low 16 - buf.put_u32::( - (1234 << 16) | 5679, - ); + buf.put_u32::((1234 << 16) | 5679); } } diff --git a/sqlx-core/src/url.rs b/sqlx-core/src/url.rs index f7914dbbd..624571227 100644 --- a/sqlx-core/src/url.rs +++ b/sqlx-core/src/url.rs @@ -1,5 +1,5 @@ -use std::convert::{TryFrom, TryInto}; use std::borrow::Cow; +use std::convert::{TryFrom, TryInto}; pub struct Url(url::Url); @@ -67,12 +67,8 @@ impl Url { } pub fn get_param(&self, key: &str) -> Option> { - self.0.query_pairs().find_map(|(key_, val)| { - if key == key_ { - Some(val) - } else { - None - } - }) + self.0 + .query_pairs() + .find_map(|(key_, val)| if key == key_ { Some(val) } else { None }) } }