chore: rename error type and reexport error types

Closes #2
This commit is contained in:
Dmitriy Pleshevskiy 2021-10-21 23:56:39 +03:00
parent 829fecb7aa
commit 58269f5a64
8 changed files with 68 additions and 75 deletions

View File

@ -1,4 +1,3 @@
use ood_persistence::error::PersistenceError;
use std::error; use std::error;
use std::fmt; use std::fmt;
@ -8,7 +7,7 @@ pub type ApiResult<T> = Result<T, Error>;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
PersistenceError(PersistenceError), PersistenceError(ood_persistence::Error),
Rest(RestKind), Rest(RestKind),
Serde(serde_json::Error), Serde(serde_json::Error),
} }
@ -25,8 +24,8 @@ impl fmt::Display for Error {
impl std::error::Error for Error {} impl std::error::Error for Error {}
impl From<PersistenceError> for Error { impl From<ood_persistence::Error> for Error {
fn from(err: PersistenceError) -> Self { fn from(err: ood_persistence::Error) -> Self {
Self::PersistenceError(err) Self::PersistenceError(err)
} }
} }

View File

@ -14,7 +14,7 @@ pub trait PersistencePool: Send + Sync {
/// # Errors /// # Errors
/// ///
/// Returns PersistenceError if pool cannot get a connection. /// Returns PersistenceError if pool cannot get a connection.
async fn get_connection(&self) -> error::Result<Self::Conn>; async fn get_connection(&self) -> crate::Result<Self::Conn>;
} }
/// Connection client knows about the inner connection, and also knows how to create transactions. /// 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. /// Returns PersistenceError if connection cannot update to transaction.
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
async fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>>; async fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>>;
} }
/// Transaction client is updated connection client that can additionally commit and rollback data /// Transaction client is updated connection client that can additionally commit and rollback data
@ -55,12 +55,12 @@ pub trait TransactionClient: ConnectionClient {
/// # Errors /// # Errors
/// ///
/// Returns PersistenceError if transaction cannot commit /// 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. /// Rolls the transaction back, discarding all changes made within it.
/// ///
/// # Errors /// # Errors
/// ///
/// Returns PersistenceError if transaction cannot rolls back. /// Returns PersistenceError if transaction cannot rolls back.
async fn rollback(self) -> error::Result<()>; async fn rollback(self) -> crate::Result<()>;
} }

View File

@ -42,11 +42,11 @@ where
impl<'p> PersistencePool for NoTlsPersistence<'p> { impl<'p> PersistencePool for NoTlsPersistence<'p> {
type Conn = NoTlsConnection<'p>; type Conn = NoTlsConnection<'p>;
async fn get_connection(&self) -> error::Result<Self::Conn> { async fn get_connection(&self) -> crate::Result<Self::Conn> {
self.0 self.0
.get() .get()
.await .await
.map_err(|_| error::PersistenceError::GetConnection) .map_err(|_| crate::Error::GetConnection)
.map(Connection) .map(Connection)
} }
} }
@ -68,16 +68,16 @@ impl<'me> ConnectionClient for NoTlsConnection<'me> {
} }
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
async fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>> { async fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
self.0 self.0
.transaction() .transaction()
.await .await
.map_err(|_| error::PersistenceError::UpgradeToTransaction) .map_err(|_| crate::Error::UpgradeToTransaction)
.map(Transaction) .map(Transaction)
} }
} }
/// Transaction wrap over tokio_postgres transaction. /// Transaction wrap over `tokio_postgres` transaction.
/// ///
/// **Note:** requires nightly rust channel and enabling the `nightly` feature. /// **Note:** requires nightly rust channel and enabling the `nightly` feature.
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
@ -94,11 +94,11 @@ impl<'me> ConnectionClient for Transaction<'me> {
&mut self.0 &mut self.0
} }
async fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>> { async fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
self.0 self.0
.transaction() .transaction()
.await .await
.map_err(|_| error::PersistenceError::UpgradeToTransaction) .map_err(|_| crate::Error::UpgradeToTransaction)
.map(Transaction) .map(Transaction)
} }
} }
@ -106,17 +106,17 @@ impl<'me> ConnectionClient for Transaction<'me> {
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
#[async_trait] #[async_trait]
impl<'me> TransactionClient for Transaction<'me> { impl<'me> TransactionClient for Transaction<'me> {
async fn commit(self) -> error::Result<()> { async fn commit(self) -> crate::Result<()> {
self.0 self.0
.commit() .commit()
.await .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 self.0
.rollback() .rollback()
.await .await
.map_err(|_| error::PersistenceError::RollbackTransaction) .map_err(|_| crate::Error::RollbackTransaction)
} }
} }

View File

@ -1,4 +1,3 @@
use std::error;
use std::fmt; use std::fmt;
#[cfg(feature = "bb8_postgres")] #[cfg(feature = "bb8_postgres")]
@ -12,11 +11,11 @@ use r2d2_sqlite::rusqlite::Error as RusqliteError;
/// A helper type for any result with persistence error. /// 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. /// Use this type in your repository or in something else that implements methods for your persistence.
pub type Result<T> = std::result::Result<T, PersistenceError>; pub type Result<T> = std::result::Result<T, Error>;
/// All supported kinds of persistence error /// All supported kinds of persistence error
#[derive(Debug)] #[derive(Debug)]
pub enum PersistenceError { pub enum Error {
/// Returns if we cannot get a connection from pool. /// Returns if we cannot get a connection from pool.
GetConnection, GetConnection,
/// Returns if we cannot upgrade connection to transaction. /// Returns if we cannot upgrade connection to transaction.
@ -28,41 +27,37 @@ pub enum PersistenceError {
/// Returns if we cannot rolls back transaction. /// Returns if we cannot rolls back transaction.
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
RollbackTransaction, RollbackTransaction,
/// Rest database errors contains here. /// Rest persistence errors contains here.
DbError(Box<dyn std::error::Error>), PersistenceError(Box<dyn std::error::Error>),
} }
impl fmt::Display for PersistenceError { impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { match self {
PersistenceError::GetConnection => f.write_str("Cannot get connection"), Error::GetConnection => f.write_str("Cannot get connection"),
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
PersistenceError::UpgradeToTransaction => { Error::UpgradeToTransaction => f.write_str("Cannot upgrade connection to transaction"),
f.write_str("Cannot upgrade connection to transaction")
}
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
PersistenceError::CommitTransaction => { Error::CommitTransaction => f.write_str("Cannot commit changes of transaction"),
f.write_str("Cannot commit changes of transaction")
}
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
PersistenceError::RollbackTransaction => f.write_str("Cannot rolls transaction back"), Error::RollbackTransaction => f.write_str("Cannot rolls transaction back"),
PersistenceError::DbError(err) => write!(f, "DbError: {}", err), 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"))] #[cfg(any(feature = "r2d2_postgres", feature = "bb8_postgres"))]
impl From<PostgresError> for PersistenceError { impl From<PostgresError> for Error {
fn from(err: PostgresError) -> Self { fn from(err: PostgresError) -> Self {
Self::DbError(Box::new(err)) Self::PersistenceError(Box::new(err))
} }
} }
#[cfg(feature = "r2d2_sqlite")] #[cfg(feature = "r2d2_sqlite")]
impl From<RusqliteError> for PersistenceError { impl From<RusqliteError> for Error {
fn from(err: RusqliteError) -> Self { fn from(err: RusqliteError) -> Self {
Self::DbError(Box::new(err)) Self::PersistenceError(Box::new(err))
} }
} }

View File

@ -28,7 +28,7 @@
//! See examples directory. //! See examples directory.
//! //!
#![forbid(unsafe_code, non_ascii_idents)] #![forbid(unsafe_code, non_ascii_idents)]
#![deny(clippy::all)] #![deny(clippy::pedantic)]
#![warn(missing_docs)] #![warn(missing_docs)]
#![cfg_attr(feature = "nightly", feature(generic_associated_types))] #![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 /// This module contains implementations for errors and result, that this
/// crate uses /// crate uses
pub mod error; pub mod error;
pub use error::{Error, Result};

View File

@ -1,4 +1,3 @@
use crate::error;
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
use crate::syn::TransactionClient; use crate::syn::TransactionClient;
use crate::syn::{ConnectionClient, PersistencePool}; use crate::syn::{ConnectionClient, PersistencePool};
@ -24,6 +23,7 @@ pub type NoTlsInnerConn = InnerConn<NoTlsManager>;
pub type NoTlsPool = Pool<NoTlsManager>; pub type NoTlsPool = Pool<NoTlsManager>;
/// It creates new persistence of r2d2 postgres implementation. /// It creates new persistence of r2d2 postgres implementation.
#[must_use]
pub fn new<M>(pool: &Pool<M>) -> Persistence<M> pub fn new<M>(pool: &Pool<M>) -> Persistence<M>
where where
M: r2d2::ManageConnection, M: r2d2::ManageConnection,
@ -40,10 +40,10 @@ where
impl PersistencePool for NoTlsPersistence<'_> { impl PersistencePool for NoTlsPersistence<'_> {
type Conn = NoTlsConnection; type Conn = NoTlsConnection;
fn get_connection(&self) -> error::Result<Self::Conn> { fn get_connection(&self) -> crate::Result<Self::Conn> {
self.0 self.0
.get() .get()
.map_err(|_| error::PersistenceError::GetConnection) .map_err(|_| crate::Error::GetConnection)
.map(Connection) .map(Connection)
} }
} }
@ -64,10 +64,10 @@ impl ConnectionClient for NoTlsConnection {
} }
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>> { fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
self.0 self.0
.transaction() .transaction()
.map_err(|_| error::PersistenceError::UpgradeToTransaction) .map_err(|_| crate::Error::UpgradeToTransaction)
.map(Transaction) .map(Transaction)
} }
} }
@ -88,25 +88,23 @@ impl<'me> ConnectionClient for Transaction<'me> {
&mut self.0 &mut self.0
} }
fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>> { fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
self.0 self.0
.transaction() .transaction()
.map_err(|_| error::PersistenceError::UpgradeToTransaction) .map_err(|_| crate::Error::UpgradeToTransaction)
.map(Transaction) .map(Transaction)
} }
} }
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
impl TransactionClient for Transaction<'_> { impl TransactionClient for Transaction<'_> {
fn commit(self) -> error::Result<()> { fn commit(self) -> crate::Result<()> {
self.0 self.0.commit().map_err(|_| crate::Error::CommitTransaction)
.commit()
.map_err(|_| error::PersistenceError::CommitTransaction)
} }
fn rollback(self) -> error::Result<()> { fn rollback(self) -> crate::Result<()> {
self.0 self.0
.rollback() .rollback()
.map_err(|_| error::PersistenceError::RollbackTransaction) .map_err(|_| crate::Error::RollbackTransaction)
} }
} }

View File

@ -12,6 +12,7 @@ pub type InnerConn = PooledConnection<Manager>;
pub type InnerTrx<'t> = rusqlite::Transaction<'t>; pub type InnerTrx<'t> = rusqlite::Transaction<'t>;
/// It creates new persistence of r2d2 sqlite implementation. /// It creates new persistence of r2d2 sqlite implementation.
#[must_use]
pub fn new(pool: &Pool<Manager>) -> Persistence { pub fn new(pool: &Pool<Manager>) -> Persistence {
Persistence(pool) Persistence(pool)
} }
@ -23,10 +24,10 @@ pub struct Persistence<'p>(&'p Pool<Manager>);
impl PersistencePool for Persistence<'_> { impl PersistencePool for Persistence<'_> {
type Conn = Connection; type Conn = Connection;
fn get_connection(&self) -> error::Result<Self::Conn> { fn get_connection(&self) -> crate::Result<Self::Conn> {
self.0 self.0
.get() .get()
.map_err(|_| error::PersistenceError::GetConnection) .map_err(|_| crate::Error::GetConnection)
.map(Connection) .map(Connection)
} }
} }
@ -45,10 +46,10 @@ impl ConnectionClient for Connection {
} }
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>> { fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
self.0 self.0
.transaction() .transaction()
.map_err(|_| error::PersistenceError::UpgradeToTransaction) .map_err(|_| crate::Error::UpgradeToTransaction)
.map(Transaction) .map(Transaction)
} }
} }
@ -60,7 +61,7 @@ impl ConnectionClient for Connection {
/// # Limits /// # Limits
/// ///
/// It doesn't support nested transaction, because the transaction in `rusqlite` /// 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>); pub struct Transaction<'me>(InnerTrx<'me>);
impl<'me> ConnectionClient for Transaction<'me> { impl<'me> ConnectionClient for Transaction<'me> {
@ -74,7 +75,7 @@ impl<'me> ConnectionClient for Transaction<'me> {
} }
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>> { fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>> {
// At the moment we cannot implement nested transaction because // At the moment we cannot implement nested transaction because
// the transaction in `rusqlite` requires DerefMut, which cannot be // the transaction in `rusqlite` requires DerefMut, which cannot be
// implemented yet 😣 // implemented yet 😣
@ -87,14 +88,12 @@ impl<'me> ConnectionClient for Transaction<'me> {
} }
impl TransactionClient for Transaction<'_> { impl TransactionClient for Transaction<'_> {
fn commit(self) -> error::Result<()> { fn commit(self) -> crate::Result<()> {
self.0 self.0.commit().map_err(|_| crate::Error::CommitTransaction)
.commit()
.map_err(|_| error::PersistenceError::CommitTransaction)
} }
fn rollback(self) -> error::Result<()> { fn rollback(self) -> crate::Result<()> {
self.0 self.0
.rollback() .rollback()
.map_err(|_| error::PersistenceError::RollbackTransaction) .map_err(|_| crate::Error::RollbackTransaction)
} }
} }

View File

@ -5,15 +5,15 @@ use crate::error;
/// Any implementation (database, file storage, memory or others) must be able to get a connection /// Any implementation (database, file storage, memory or others) must be able to get a connection
/// to manipulate it afterwards. /// to manipulate it afterwards.
pub trait PersistencePool { pub trait PersistencePool {
/// ConnectionClient implementation that persistence can can return. /// `ConnectionClient` implementation that persistence can can return.
type Conn: ConnectionClient; type Conn: ConnectionClient;
/// Returns new connection. /// Returns new connection.
/// ///
/// # Errors /// # Errors
/// ///
/// Returns PersistenceError if pool cannot get a connection. /// Returns `PersistenceError` if pool cannot get a connection.
fn get_connection(&self) -> error::Result<Self::Conn>; fn get_connection(&self) -> crate::Result<Self::Conn>;
} }
/// Connection client knows about the inner connection, and also knows how to create transactions. /// Connection client knows about the inner connection, and also knows how to create transactions.
@ -21,7 +21,7 @@ pub trait ConnectionClient {
/// Inner connection /// Inner connection
type InnerConn; 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. /// **Note:** requires nightly rust channel and enabling the `nightly` feature.
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
@ -36,9 +36,9 @@ pub trait ConnectionClient {
/// ///
/// # Errors /// # Errors
/// ///
/// Returns PersistenceError if connection cannot update to transaction. /// Returns `PersistenceError` if connection cannot update to transaction.
#[cfg(feature = "nightly")] #[cfg(feature = "nightly")]
fn start_transaction(&mut self) -> error::Result<Self::Trx<'_>>; fn start_transaction(&mut self) -> crate::Result<Self::Trx<'_>>;
} }
/// Transaction client is updated connection client that can additionally commit and rollback data /// Transaction client is updated connection client that can additionally commit and rollback data
@ -51,13 +51,13 @@ pub trait TransactionClient: ConnectionClient {
/// ///
/// # Errors /// # Errors
/// ///
/// Returns PersistenceError if transaction cannot commit /// Returns `PersistenceError` if transaction cannot commit
fn commit(self) -> error::Result<()>; fn commit(self) -> crate::Result<()>;
/// Rolls the transaction back, discarding all changes made within it. /// Rolls the transaction back, discarding all changes made within it.
/// ///
/// # Errors /// # Errors
/// ///
/// Returns PersistenceError if transaction cannot rolls back. /// Returns `PersistenceError` if transaction cannot rolls back.
fn rollback(self) -> error::Result<()>; fn rollback(self) -> crate::Result<()>;
} }