1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
use std::{
    error::Error,
    fmt::{Display, Formatter, Result as FmtResult},
    future::Future,
    ops::Deref,
    pin::Pin,
};

use async_trait::async_trait;
use deadpool_postgres::{
    tokio_postgres::{Client, Error as TokioPostgresError},
    Object, Pool, PoolError, Transaction,
};
use tracing::trace;

// Types

pub type PostgresResult<T> = Result<T, PostgresError>;

// PostgresError

/// An that can occur when interacting with Postgres.
#[derive(Debug)]
pub struct PostgresError(Box<dyn Error + Send + Sync>);

impl Display for PostgresError {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        write!(f, "{}", self.0)
    }
}

impl Error for PostgresError {
    fn source(&self) -> Option<&(dyn Error + 'static)> {
        Some(self.0.as_ref())
    }
}

impl From<PoolError> for PostgresError {
    fn from(err: PoolError) -> Self {
        Self(Box::new(err))
    }
}

impl From<TokioPostgresError> for PostgresError {
    fn from(err: TokioPostgresError) -> Self {
        Self(Box::new(err))
    }
}

// PostgresClient

/// A client for interacting with Postgres.
///
/// **This is supported on `feature=postgres` only.**
///
/// [Example](https://github.com/leroyguillaume/mockable/tree/main/examples/postgres.rs).
#[async_trait]
pub trait PostgresClient: Send + Sync + ToPostgresClient {
    /// Returns the underlying [`Client`](https://docs.rs/tokio-postgres/latest/tokio_postgres/struct.Client.html) instance.
    fn into_client(self: Box<Self>) -> Object;

    /// Opens a new transaction.
    ///
    /// See [`Client::transaction`](https://docs.rs/tokio-postgres/latest/tokio_postgres/struct.Client.html#method.transaction) for more information.
    async fn transaction(&mut self) -> PostgresResult<Box<dyn PostgresTransaction + '_>>;

    /// Returns self reference.
    fn upcast(&self) -> &dyn ToPostgresClient;
}

// PostgresPool

/// A pool of Postgres clients.
///
/// **This is supported on `feature=postgres` only.**
///
/// [Example](https://github.com/leroyguillaume/mockable/tree/main/examples/postgres.rs).
#[async_trait]
pub trait PostgresPool: Send + Sync {
    /// Returns a client from the pool.
    async fn get(&self) -> PostgresResult<Box<dyn PostgresClient>>;
}

// PostgresTransaction

/// A Postgres transaction.
///
/// **This is supported on `feature=postgres` only.**
#[async_trait]
pub trait PostgresTransaction<'a>: Send + Sync + ToPostgresClient {
    /// Commits the transaction.
    async fn commit(self: Box<Self>) -> PostgresResult<()>;

    /// Returns the underlying [`Transaction`](https://docs.rs/tokio-postgres/latest/tokio_postgres/struct.Transaction.html) instance.
    fn into_transaction(self: Box<Self>) -> Transaction<'a>;

    /// Rolls back the transaction.
    async fn rollback(self: Box<Self>) -> PostgresResult<()>;

    /// Returns a reference to the underlying [`Transaction`](https://docs.rs/tokio-postgres/latest/tokio_postgres/struct.Transaction.html) instance.
    fn to_transaction(&self) -> &Transaction<'a>;

    /// Returns self reference.
    fn upcast(&self) -> &dyn ToPostgresClient;
}

// ToPostgresClient

/// A trait for converting a type into a [`PostgresClient`](trait.PostgresClient.html).
pub trait ToPostgresClient {
    /// Returns a reference to the underlying [`Client`](https://docs.rs/tokio-postgres/latest/tokio_postgres/struct.Client.html) instance.
    fn to_client(&self) -> &Client;
}

// DefaultPostgresClient

/// Default implementation of [`PostgresClient`](trait.PostgresClient.html).
///
/// **This is supported on `feature=postgres` only.**
///
/// [Example](https://github.com/leroyguillaume/mockable/tree/main/examples/postgres.rs).
pub struct DefaultPostgresClient(Object);

impl DefaultPostgresClient {
    /// Create a new `DefaultPostgresClient`.
    pub fn new(client: Object) -> Self {
        Self(client)
    }
}

#[async_trait]
impl PostgresClient for DefaultPostgresClient {
    fn into_client(self: Box<Self>) -> Object {
        self.0
    }

    async fn transaction(&mut self) -> PostgresResult<Box<dyn PostgresTransaction + '_>> {
        trace!("opening transaction");
        let tx = self.0.transaction().await?;
        Ok(Box::new(DefaultPostgresTransaction(tx)))
    }

    fn upcast(&self) -> &dyn ToPostgresClient {
        self
    }
}

impl ToPostgresClient for DefaultPostgresClient {
    fn to_client(&self) -> &Client {
        self.0.deref().deref()
    }
}

// DefaultPostgresPool

/// Default implementation of [`PostgresPool`](trait.PostgresPool.html).
///
/// **This is supported on `feature=postgres` only.**
///
/// [Example](https://github.com/leroyguillaume/mockable/tree/main/examples/postgres.rs).
pub struct DefaultPostgresPool(Pool);

impl DefaultPostgresPool {
    /// Create a new `DefaultPostgresPool`.
    pub fn new(pool: Pool) -> Self {
        Self(pool)
    }
}

#[async_trait]
impl PostgresPool for DefaultPostgresPool {
    async fn get(&self) -> PostgresResult<Box<dyn PostgresClient>> {
        trace!("getting client from pool");
        let client = self.0.get().await?;
        Ok(Box::new(DefaultPostgresClient(client)))
    }
}

// DefaultPostgresTransaction

/// Default implementation of [`PostgresTransaction`](trait.PostgresTransaction.html).
///
/// **This is supported on `feature=postgres` only.**
pub struct DefaultPostgresTransaction<'a>(Transaction<'a>);

impl<'a> DefaultPostgresTransaction<'a> {
    /// Create a new `DefaultPostgresTransaction`.
    pub fn new(transaction: Transaction<'a>) -> Self {
        Self(transaction)
    }
}

#[async_trait]
impl<'a> PostgresTransaction<'a> for DefaultPostgresTransaction<'a> {
    fn to_transaction(&self) -> &Transaction<'a> {
        &self.0
    }

    async fn commit(self: Box<Self>) -> PostgresResult<()> {
        trace!("committing transaction");
        self.0.commit().await?;
        Ok(())
    }

    fn into_transaction(self: Box<Self>) -> Transaction<'a> {
        self.0
    }

    async fn rollback(self: Box<Self>) -> PostgresResult<()> {
        trace!("rolling back transaction");
        self.0.rollback().await?;
        Ok(())
    }

    fn upcast(&self) -> &dyn ToPostgresClient {
        self
    }
}

impl ToPostgresClient for DefaultPostgresTransaction<'_> {
    fn to_client(&self) -> &Client {
        self.0.client()
    }
}

// MockPostgresClient

/// Mock implementation of [`PostgresClient`](trait.PostgresClient.html).
///
/// **This is supported on `feature=postgres,mock` only.**
///
/// Note this mock is not `mockall` compatible because of lifetime issues.
///
/// [Example](https://github.com/leroyguillaume/mockable/tree/main/examples/postgres.rs).
#[cfg(feature = "mock")]
#[derive(Default)]
pub struct MockPostgresClient {
    /// Mock implementation of [`PostgresClient::transaction`](trait.PostgresClient.html#method.transaction).
    pub transaction: crate::Mock<MockPostgresTransaction>,
}

#[cfg(feature = "mock")]
#[async_trait]
impl PostgresClient for MockPostgresClient {
    /// **This method is unimplemented.**
    fn into_client(self: Box<Self>) -> Object {
        unimplemented!()
    }

    async fn transaction(&mut self) -> PostgresResult<Box<dyn PostgresTransaction + '_>> {
        Ok(Box::new(self.transaction.call()))
    }

    fn upcast(&self) -> &dyn ToPostgresClient {
        self
    }
}

#[cfg(feature = "mock")]
impl ToPostgresClient for MockPostgresClient {
    /// **This method is unimplemented.**
    fn to_client(&self) -> &Client {
        unimplemented!()
    }
}

// MockPostgresPool

#[cfg(feature = "mock")]
mockall::mock! {
    /// Mock implementation of [`PostgresPool`](trait.PostgresPool.html).
    ///
    /// **This is supported on `feature=postgres,mock` only.**
    ///
    /// [Example](https://github.com/leroyguillaume/mockable/tree/main/examples/postgres.rs).
    pub PostgresPool {}

    #[async_trait]
    impl PostgresPool for PostgresPool {
        async fn get(&self) -> PostgresResult<Box<dyn PostgresClient>>;
    }
}

// MockPostgresTransaction

/// Mock implementation of [`PostgresTransaction`](trait.PostgresTransaction.html).
///
/// **This is supported on `feature=postgres,mock` only.**
///
/// Note this mock is not `mockall` compatible because of lifetime issues.
///
/// [Example](https://github.com/leroyguillaume/mockable/tree/main/examples/postgres.rs).
#[cfg(feature = "mock")]
#[derive(Default)]
pub struct MockPostgresTransaction {
    /// Mock implementation of [`PostgresTransaction::commit`](trait.PostgresTransaction.html#method.commit).
    pub commit: crate::Mock<PostgresResult<()>>,
    /// Mock implementation of [`PostgresTransaction::rollback`](trait.PostgresTransaction.html#method.rollback).
    pub rollback: crate::Mock<PostgresResult<()>>,
}

#[cfg(feature = "mock")]
#[async_trait]
impl<'a> PostgresTransaction<'a> for MockPostgresTransaction {
    /// **This method is unimplemented.**
    fn to_transaction(&self) -> &Transaction<'a> {
        unimplemented!()
    }

    async fn commit(self: Box<Self>) -> PostgresResult<()> {
        self.commit.call()
    }

    /// **This method is unimplemented.**
    fn into_transaction(self: Box<Self>) -> Transaction<'a> {
        unimplemented!()
    }

    async fn rollback(self: Box<Self>) -> PostgresResult<()> {
        self.rollback.call()
    }

    fn upcast(&self) -> &dyn ToPostgresClient {
        self
    }
}

#[cfg(feature = "mock")]
impl ToPostgresClient for MockPostgresTransaction {
    /// **This method is unimplemented.**
    fn to_client(&self) -> &Client {
        unimplemented!()
    }
}

// transactional

/// Runs a function in a transaction.
///
/// **This is supported on `feature=postgres` only.**
///
/// [Example](https://github.com/leroyguillaume/mockable/tree/main/examples/postgres.rs).
pub async fn transactional<
    'a,
    T,
    E,
    F: for<'b> Fn(&'b dyn PostgresTransaction) -> Pin<Box<dyn Future<Output = Result<T, E>> + 'b>>,
>(
    client: &'a mut dyn PostgresClient,
    f: F,
) -> PostgresResult<Result<T, E>> {
    let tx = client.transaction().await?;
    match f(tx.as_ref()).await {
        Ok(val) => {
            tx.commit().await?;
            Ok(Ok(val))
        }
        Err(err) => {
            tx.rollback().await?;
            Ok(Err(err))
        }
    }
}