From 017ee38725acc0610a189bedb6735a74b8e3e626 Mon Sep 17 00:00:00 2001 From: Ryan Leckey Date: Fri, 27 Dec 2019 20:55:30 -0800 Subject: [PATCH] De-duplicate error impls and forward MySqlConnection/PgConnection to crate root --- sqlx-core/src/error.rs | 43 +++++++++++++++++++++++++++++---- sqlx-core/src/mysql/error.rs | 43 +-------------------------------- sqlx-core/src/mysql/mod.rs | 26 +++++++++++++------- sqlx-core/src/postgres/error.rs | 27 +++------------------ src/lib.rs | 4 +-- 5 files changed, 61 insertions(+), 82 deletions(-) diff --git a/sqlx-core/src/error.rs b/sqlx-core/src/error.rs index 8a458c94..6c3b2a59 100644 --- a/sqlx-core/src/error.rs +++ b/sqlx-core/src/error.rs @@ -147,15 +147,25 @@ pub trait DatabaseError: Display + Debug + Send + Sync { /// The primary, human-readable error message. fn message(&self) -> &str; - fn details(&self) -> Option<&str>; + fn details(&self) -> Option<&str> { + None + } - fn hint(&self) -> Option<&str>; + fn hint(&self) -> Option<&str> { + None + } - fn table_name(&self) -> Option<&str>; + fn table_name(&self) -> Option<&str> { + None + } - fn column_name(&self) -> Option<&str>; + fn column_name(&self) -> Option<&str> { + None + } - fn constraint_name(&self) -> Option<&str>; + fn constraint_name(&self) -> Option<&str> { + None + } } /// Used by the `protocol_error!()` macro for a lazily evaluated conversion to @@ -170,3 +180,26 @@ macro_rules! protocol_err ( $crate::error::ProtocolError { args: format_args!($($args)*) } } ); + +macro_rules! impl_fmt_error { + ($err:ty) => { + impl std::fmt::Debug for $err { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.debug_struct("DatabaseError") + .field("message", &self.message()) + .field("details", &self.details()) + .field("hint", &self.hint()) + .field("table_name", &self.table_name()) + .field("column_name", &self.column_name()) + .field("constraint_name", &self.constraint_name()) + .finish() + } + } + + impl std::fmt::Display for $err { + fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { + f.pad(self.message()) + } + } + } +} diff --git a/sqlx-core/src/mysql/error.rs b/sqlx-core/src/mysql/error.rs index 0c5a00dc..8ff1cb5d 100644 --- a/sqlx-core/src/mysql/error.rs +++ b/sqlx-core/src/mysql/error.rs @@ -1,5 +1,3 @@ -use std::fmt::{self, Debug, Display}; - use crate::error::DatabaseError; use crate::mysql::protocol::ErrPacket; @@ -9,45 +7,6 @@ impl DatabaseError for MySqlError { fn message(&self) -> &str { &*self.0.error_message } - - fn details(&self) -> Option<&str> { - None - } - - fn hint(&self) -> Option<&str> { - None - } - - fn table_name(&self) -> Option<&str> { - None - } - - fn column_name(&self) -> Option<&str> { - None - } - - fn constraint_name(&self) -> Option<&str> { - None - } } -// TODO: De-duplicate these two impls with Postgres (macro?) - -impl Debug for MySqlError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.debug_struct("DatabaseError") - .field("message", &self.message()) - .field("details", &self.details()) - .field("hint", &self.hint()) - .field("table_name", &self.table_name()) - .field("column_name", &self.column_name()) - .field("constraint_name", &self.constraint_name()) - .finish() - } -} - -impl Display for MySqlError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.pad(self.message()) - } -} +impl_fmt_error!(MySqlError); diff --git a/sqlx-core/src/mysql/mod.rs b/sqlx-core/src/mysql/mod.rs index 0a6542a9..fd5d75f6 100644 --- a/sqlx-core/src/mysql/mod.rs +++ b/sqlx-core/src/mysql/mod.rs @@ -1,14 +1,5 @@ //! **MySQL** database and connection types. -use std::convert::TryInto; - -pub use arguments::MySqlArguments; -pub use connection::MySqlConnection; -pub use database::MySql; -pub use row::MySqlRow; - -use crate::url::Url; - mod arguments; mod connection; mod database; @@ -19,6 +10,23 @@ mod protocol; mod row; mod types; +pub use database::MySql; + +pub use arguments::MySqlArguments; + +pub use connection::MySqlConnection; + +pub use error::MySqlError; + +pub use row::MySqlRow; + +/// An alias for [`Pool`], specialized for **MySQL**. +pub type MySqlPool = super::Pool; + +use std::convert::TryInto; + +use crate::url::Url; + // used in tests and hidden code in examples #[doc(hidden)] pub async fn connect(url: T) -> crate::Result diff --git a/sqlx-core/src/postgres/error.rs b/sqlx-core/src/postgres/error.rs index e2147991..84e30b2b 100644 --- a/sqlx-core/src/postgres/error.rs +++ b/sqlx-core/src/postgres/error.rs @@ -1,9 +1,9 @@ +use crate::error::DatabaseError; use crate::postgres::protocol::Response; -use std::fmt::{self, Debug, Display}; pub struct PgError(pub(super) Box); -impl crate::error::DatabaseError for PgError { +impl DatabaseError for PgError { fn message(&self) -> &str { &self.0.message } @@ -29,25 +29,4 @@ impl crate::error::DatabaseError for PgError { } } -impl Debug for PgError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use crate::error::DatabaseError; - - f.debug_struct("DatabaseError") - .field("message", &self.message()) - .field("details", &self.details()) - .field("hint", &self.hint()) - .field("table_name", &self.table_name()) - .field("column_name", &self.column_name()) - .field("constraint_name", &self.constraint_name()) - .finish() - } -} - -impl Display for PgError { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - use crate::error::DatabaseError; - - f.pad(self.message()) - } -} +impl_fmt_error!(PgError); diff --git a/src/lib.rs b/src/lib.rs index 33df12ae..044d969f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,10 +13,10 @@ pub use sqlx_core::{query, query_as}; pub use sqlx_core::query_as_mapped; #[cfg(feature = "mysql")] -pub use sqlx_core::mysql::{self, MySql}; +pub use sqlx_core::mysql::{self, MySql, MySqlConnection, MySqlPool}; #[cfg(feature = "postgres")] -pub use sqlx_core::postgres::{self, Postgres}; +pub use sqlx_core::postgres::{self, Postgres, PgConnection, PgPool}; #[cfg(feature = "macros")] #[doc(hidden)]