From 58269f5a64e9ec82d2bdf21d22c636c4ed65df02 Mon Sep 17 00:00:00 2001 From: Dmitriy Pleshevskiy Date: Thu, 21 Oct 2021 23:56:39 +0300 Subject: [PATCH] chore: rename error type and reexport error types Closes #2 --- examples/web/src/error.rs | 7 +++---- src/asyn.rs | 8 ++++---- src/bb8_postgres.rs | 22 +++++++++++----------- src/error.rs | 35 +++++++++++++++-------------------- src/lib.rs | 4 +++- src/r2d2_postgres.rs | 24 +++++++++++------------- src/r2d2_sqlite.rs | 23 +++++++++++------------ src/syn.rs | 20 ++++++++++---------- 8 files changed, 68 insertions(+), 75 deletions(-) diff --git a/examples/web/src/error.rs b/examples/web/src/error.rs index ce9d087..8775b03 100644 --- a/examples/web/src/error.rs +++ b/examples/web/src/error.rs @@ -1,4 +1,3 @@ -use ood_persistence::error::PersistenceError; use std::error; use std::fmt; @@ -8,7 +7,7 @@ pub type ApiResult = Result; #[derive(Debug)] pub enum Error { - PersistenceError(PersistenceError), + PersistenceError(ood_persistence::Error), Rest(RestKind), Serde(serde_json::Error), } @@ -25,8 +24,8 @@ impl fmt::Display for Error { impl std::error::Error for Error {} -impl From for Error { - fn from(err: PersistenceError) -> Self { +impl From for Error { + fn from(err: ood_persistence::Error) -> Self { Self::PersistenceError(err) } } diff --git a/src/asyn.rs b/src/asyn.rs index b691f8c..cd9322b 100644 --- a/src/asyn.rs +++ b/src/asyn.rs @@ -14,7 +14,7 @@ pub trait PersistencePool: Send + Sync { /// # Errors /// /// Returns PersistenceError if pool cannot get a connection. - async fn get_connection(&self) -> error::Result; + async fn get_connection(&self) -> crate::Result; } /// Connection client knows about the inner connection, and also knows how to create transactions. @@ -40,7 +40,7 @@ pub trait ConnectionClient { /// /// Returns PersistenceError if connection cannot update to transaction. #[cfg(feature = "nightly")] - async fn start_transaction(&mut self) -> error::Result>; + async fn start_transaction(&mut self) -> crate::Result>; } /// Transaction client is updated connection client that can additionally commit and rollback data @@ -55,12 +55,12 @@ pub trait TransactionClient: ConnectionClient { /// # Errors /// /// Returns PersistenceError if transaction cannot commit - async fn commit(self) -> error::Result<()>; + async fn commit(self) -> crate::Result<()>; /// Rolls the transaction back, discarding all changes made within it. /// /// # Errors /// /// Returns PersistenceError if transaction cannot rolls back. - async fn rollback(self) -> error::Result<()>; + async fn rollback(self) -> crate::Result<()>; } diff --git a/src/bb8_postgres.rs b/src/bb8_postgres.rs index 90b0f43..d2460a5 100644 --- a/src/bb8_postgres.rs +++ b/src/bb8_postgres.rs @@ -42,11 +42,11 @@ where impl<'p> PersistencePool for NoTlsPersistence<'p> { type Conn = NoTlsConnection<'p>; - async fn get_connection(&self) -> error::Result { + async fn get_connection(&self) -> crate::Result { self.0 .get() .await - .map_err(|_| error::PersistenceError::GetConnection) + .map_err(|_| crate::Error::GetConnection) .map(Connection) } } @@ -68,16 +68,16 @@ impl<'me> ConnectionClient for NoTlsConnection<'me> { } #[cfg(feature = "nightly")] - async fn start_transaction(&mut self) -> error::Result> { + async fn start_transaction(&mut self) -> crate::Result> { self.0 .transaction() .await - .map_err(|_| error::PersistenceError::UpgradeToTransaction) + .map_err(|_| crate::Error::UpgradeToTransaction) .map(Transaction) } } -/// Transaction wrap over tokio_postgres transaction. +/// Transaction wrap over `tokio_postgres` transaction. /// /// **Note:** requires nightly rust channel and enabling the `nightly` feature. #[cfg(feature = "nightly")] @@ -94,11 +94,11 @@ impl<'me> ConnectionClient for Transaction<'me> { &mut self.0 } - async fn start_transaction(&mut self) -> error::Result> { + async fn start_transaction(&mut self) -> crate::Result> { self.0 .transaction() .await - .map_err(|_| error::PersistenceError::UpgradeToTransaction) + .map_err(|_| crate::Error::UpgradeToTransaction) .map(Transaction) } } @@ -106,17 +106,17 @@ impl<'me> ConnectionClient for Transaction<'me> { #[cfg(feature = "nightly")] #[async_trait] impl<'me> TransactionClient for Transaction<'me> { - async fn commit(self) -> error::Result<()> { + async fn commit(self) -> crate::Result<()> { self.0 .commit() .await - .map_err(|_| error::PersistenceError::CommitTransaction) + .map_err(|_| crate::Error::CommitTransaction) } - async fn rollback(self) -> error::Result<()> { + async fn rollback(self) -> crate::Result<()> { self.0 .rollback() .await - .map_err(|_| error::PersistenceError::RollbackTransaction) + .map_err(|_| crate::Error::RollbackTransaction) } } diff --git a/src/error.rs b/src/error.rs index 2193114..c528e09 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,4 +1,3 @@ -use std::error; use std::fmt; #[cfg(feature = "bb8_postgres")] @@ -12,11 +11,11 @@ use r2d2_sqlite::rusqlite::Error as RusqliteError; /// A helper type for any result with persistence error. /// /// Use this type in your repository or in something else that implements methods for your persistence. -pub type Result = std::result::Result; +pub type Result = std::result::Result; /// All supported kinds of persistence error #[derive(Debug)] -pub enum PersistenceError { +pub enum Error { /// Returns if we cannot get a connection from pool. GetConnection, /// Returns if we cannot upgrade connection to transaction. @@ -28,41 +27,37 @@ pub enum PersistenceError { /// Returns if we cannot rolls back transaction. #[cfg(feature = "nightly")] RollbackTransaction, - /// Rest database errors contains here. - DbError(Box), + /// Rest persistence errors contains here. + PersistenceError(Box), } -impl fmt::Display for PersistenceError { +impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - PersistenceError::GetConnection => f.write_str("Cannot get connection"), + Error::GetConnection => f.write_str("Cannot get connection"), #[cfg(feature = "nightly")] - PersistenceError::UpgradeToTransaction => { - f.write_str("Cannot upgrade connection to transaction") - } + Error::UpgradeToTransaction => f.write_str("Cannot upgrade connection to transaction"), #[cfg(feature = "nightly")] - PersistenceError::CommitTransaction => { - f.write_str("Cannot commit changes of transaction") - } + Error::CommitTransaction => f.write_str("Cannot commit changes of transaction"), #[cfg(feature = "nightly")] - PersistenceError::RollbackTransaction => f.write_str("Cannot rolls transaction back"), - PersistenceError::DbError(err) => write!(f, "DbError: {}", err), + Error::RollbackTransaction => f.write_str("Cannot rolls transaction back"), + Error::PersistenceError(err) => write!(f, "DbError: {}", err), } } } -impl error::Error for PersistenceError {} +impl std::error::Error for Error {} #[cfg(any(feature = "r2d2_postgres", feature = "bb8_postgres"))] -impl From for PersistenceError { +impl From for Error { fn from(err: PostgresError) -> Self { - Self::DbError(Box::new(err)) + Self::PersistenceError(Box::new(err)) } } #[cfg(feature = "r2d2_sqlite")] -impl From for PersistenceError { +impl From for Error { fn from(err: RusqliteError) -> Self { - Self::DbError(Box::new(err)) + Self::PersistenceError(Box::new(err)) } } diff --git a/src/lib.rs b/src/lib.rs index 52158ec..9f1ea48 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -28,7 +28,7 @@ //! See examples directory. //! #![forbid(unsafe_code, non_ascii_idents)] -#![deny(clippy::all)] +#![deny(clippy::pedantic)] #![warn(missing_docs)] #![cfg_attr(feature = "nightly", feature(generic_associated_types))] @@ -87,3 +87,5 @@ pub mod r2d2_sqlite; /// This module contains implementations for errors and result, that this /// crate uses pub mod error; + +pub use error::{Error, Result}; diff --git a/src/r2d2_postgres.rs b/src/r2d2_postgres.rs index 7a36f48..8be97ef 100644 --- a/src/r2d2_postgres.rs +++ b/src/r2d2_postgres.rs @@ -1,4 +1,3 @@ -use crate::error; #[cfg(feature = "nightly")] use crate::syn::TransactionClient; use crate::syn::{ConnectionClient, PersistencePool}; @@ -24,6 +23,7 @@ pub type NoTlsInnerConn = InnerConn; pub type NoTlsPool = Pool; /// It creates new persistence of r2d2 postgres implementation. +#[must_use] pub fn new(pool: &Pool) -> Persistence where M: r2d2::ManageConnection, @@ -40,10 +40,10 @@ where impl PersistencePool for NoTlsPersistence<'_> { type Conn = NoTlsConnection; - fn get_connection(&self) -> error::Result { + fn get_connection(&self) -> crate::Result { self.0 .get() - .map_err(|_| error::PersistenceError::GetConnection) + .map_err(|_| crate::Error::GetConnection) .map(Connection) } } @@ -64,10 +64,10 @@ impl ConnectionClient for NoTlsConnection { } #[cfg(feature = "nightly")] - fn start_transaction(&mut self) -> error::Result> { + fn start_transaction(&mut self) -> crate::Result> { self.0 .transaction() - .map_err(|_| error::PersistenceError::UpgradeToTransaction) + .map_err(|_| crate::Error::UpgradeToTransaction) .map(Transaction) } } @@ -88,25 +88,23 @@ impl<'me> ConnectionClient for Transaction<'me> { &mut self.0 } - fn start_transaction(&mut self) -> error::Result> { + fn start_transaction(&mut self) -> crate::Result> { self.0 .transaction() - .map_err(|_| error::PersistenceError::UpgradeToTransaction) + .map_err(|_| crate::Error::UpgradeToTransaction) .map(Transaction) } } #[cfg(feature = "nightly")] impl TransactionClient for Transaction<'_> { - fn commit(self) -> error::Result<()> { - self.0 - .commit() - .map_err(|_| error::PersistenceError::CommitTransaction) + fn commit(self) -> crate::Result<()> { + self.0.commit().map_err(|_| crate::Error::CommitTransaction) } - fn rollback(self) -> error::Result<()> { + fn rollback(self) -> crate::Result<()> { self.0 .rollback() - .map_err(|_| error::PersistenceError::RollbackTransaction) + .map_err(|_| crate::Error::RollbackTransaction) } } diff --git a/src/r2d2_sqlite.rs b/src/r2d2_sqlite.rs index 821a055..42b37c8 100644 --- a/src/r2d2_sqlite.rs +++ b/src/r2d2_sqlite.rs @@ -12,6 +12,7 @@ pub type InnerConn = PooledConnection; pub type InnerTrx<'t> = rusqlite::Transaction<'t>; /// It creates new persistence of r2d2 sqlite implementation. +#[must_use] pub fn new(pool: &Pool) -> Persistence { Persistence(pool) } @@ -23,10 +24,10 @@ pub struct Persistence<'p>(&'p Pool); impl PersistencePool for Persistence<'_> { type Conn = Connection; - fn get_connection(&self) -> error::Result { + fn get_connection(&self) -> crate::Result { self.0 .get() - .map_err(|_| error::PersistenceError::GetConnection) + .map_err(|_| crate::Error::GetConnection) .map(Connection) } } @@ -45,10 +46,10 @@ impl ConnectionClient for Connection { } #[cfg(feature = "nightly")] - fn start_transaction(&mut self) -> error::Result> { + fn start_transaction(&mut self) -> crate::Result> { self.0 .transaction() - .map_err(|_| error::PersistenceError::UpgradeToTransaction) + .map_err(|_| crate::Error::UpgradeToTransaction) .map(Transaction) } } @@ -60,7 +61,7 @@ impl ConnectionClient for Connection { /// # Limits /// /// It doesn't support nested transaction, because the transaction in `rusqlite` -/// requires DerefMut, which cannot be implemented at the moment. 😣 +/// requires `DerefMut`, which cannot be implemented at the moment. 😣 pub struct Transaction<'me>(InnerTrx<'me>); impl<'me> ConnectionClient for Transaction<'me> { @@ -74,7 +75,7 @@ impl<'me> ConnectionClient for Transaction<'me> { } #[cfg(feature = "nightly")] - fn start_transaction(&mut self) -> error::Result> { + fn start_transaction(&mut self) -> crate::Result> { // At the moment we cannot implement nested transaction because // the transaction in `rusqlite` requires DerefMut, which cannot be // implemented yet 😣 @@ -87,14 +88,12 @@ impl<'me> ConnectionClient for Transaction<'me> { } impl TransactionClient for Transaction<'_> { - fn commit(self) -> error::Result<()> { - self.0 - .commit() - .map_err(|_| error::PersistenceError::CommitTransaction) + fn commit(self) -> crate::Result<()> { + self.0.commit().map_err(|_| crate::Error::CommitTransaction) } - fn rollback(self) -> error::Result<()> { + fn rollback(self) -> crate::Result<()> { self.0 .rollback() - .map_err(|_| error::PersistenceError::RollbackTransaction) + .map_err(|_| crate::Error::RollbackTransaction) } } diff --git a/src/syn.rs b/src/syn.rs index c5e9b00..0691c8d 100644 --- a/src/syn.rs +++ b/src/syn.rs @@ -5,15 +5,15 @@ use crate::error; /// Any implementation (database, file storage, memory or others) must be able to get a connection /// to manipulate it afterwards. pub trait PersistencePool { - /// ConnectionClient implementation that persistence can can return. + /// `ConnectionClient` implementation that persistence can can return. type Conn: ConnectionClient; /// Returns new connection. /// /// # Errors /// - /// Returns PersistenceError if pool cannot get a connection. - fn get_connection(&self) -> error::Result; + /// Returns `PersistenceError` if pool cannot get a connection. + fn get_connection(&self) -> crate::Result; } /// Connection client knows about the inner connection, and also knows how to create transactions. @@ -21,7 +21,7 @@ pub trait ConnectionClient { /// Inner connection type InnerConn; - /// TransactionClient implementation in which the connection can be updated. + /// `TransactionClient` implementation in which the connection can be updated. /// /// **Note:** requires nightly rust channel and enabling the `nightly` feature. #[cfg(feature = "nightly")] @@ -36,9 +36,9 @@ pub trait ConnectionClient { /// /// # Errors /// - /// Returns PersistenceError if connection cannot update to transaction. + /// Returns `PersistenceError` if connection cannot update to transaction. #[cfg(feature = "nightly")] - fn start_transaction(&mut self) -> error::Result>; + fn start_transaction(&mut self) -> crate::Result>; } /// Transaction client is updated connection client that can additionally commit and rollback data @@ -51,13 +51,13 @@ pub trait TransactionClient: ConnectionClient { /// /// # Errors /// - /// Returns PersistenceError if transaction cannot commit - fn commit(self) -> error::Result<()>; + /// Returns `PersistenceError` if transaction cannot commit + fn commit(self) -> crate::Result<()>; /// Rolls the transaction back, discarding all changes made within it. /// /// # Errors /// - /// Returns PersistenceError if transaction cannot rolls back. - fn rollback(self) -> error::Result<()>; + /// Returns `PersistenceError` if transaction cannot rolls back. + fn rollback(self) -> crate::Result<()>; }