1
- use axum:: async_trait;
2
- use axum:: extract:: { Extension , FromRequestParts } ;
3
- use axum:: http:: request:: Parts ;
4
- use axum:: http:: StatusCode ;
1
+ use std:: { collections:: HashMap , convert:: Infallible , fmt:: Write , io, sync:: Arc } ;
2
+
3
+ use axum:: { async_trait, extract:: FromRequestParts , http:: request:: Parts } ;
5
4
use futures:: {
6
5
stream:: futures_unordered:: FuturesUnordered , FutureExt , StreamExt , TryStreamExt ,
7
6
} ;
8
7
use rand:: { rngs:: SmallRng , thread_rng, Rng , SeedableRng } ;
9
- use std:: sync:: Arc ;
10
- use std:: { collections:: HashMap , fmt:: Write , io} ;
11
8
use tokio:: pin;
12
9
use tokio_postgres:: { connect, types:: ToSql , Client , NoTls , Statement } ;
13
10
14
11
use crate :: models_pg:: { Fortune , World } ;
15
- use crate :: utils:: internal_error;
16
12
17
13
#[ derive( Debug ) ]
18
14
pub enum PgError {
@@ -49,7 +45,7 @@ impl PgConnection {
49
45
// Spawn connection
50
46
tokio:: spawn ( async move {
51
47
if let Err ( error) = conn. await {
52
- eprintln ! ( "Connection error: {}" , error ) ;
48
+ eprintln ! ( "Connection error: {error}" ) ;
53
49
}
54
50
} ) ;
55
51
@@ -63,14 +59,14 @@ impl PgConnection {
63
59
q. push_str ( "UPDATE world SET randomnumber = CASE id " ) ;
64
60
65
61
for _ in 1 ..=num {
66
- let _ = write ! ( q, "when ${} then ${} " , pl , pl + 1 ) ;
62
+ let _ = write ! ( q, "when ${pl } then ${} " , pl + 1 ) ;
67
63
pl += 2 ;
68
64
}
69
65
70
66
q. push_str ( "ELSE randomnumber END WHERE id IN (" ) ;
71
67
72
68
for _ in 1 ..=num {
73
- let _ = write ! ( q, "${}," , pl ) ;
69
+ let _ = write ! ( q, "${pl }," ) ;
74
70
pl += 1 ;
75
71
}
76
72
@@ -191,21 +187,13 @@ impl PgConnection {
191
187
pub struct DatabaseConnection ( pub Arc < PgConnection > ) ;
192
188
193
189
#[ async_trait]
194
- impl < S > FromRequestParts < S > for DatabaseConnection
195
- where
196
- S : Send + Sync ,
197
- {
198
- type Rejection = ( StatusCode , String ) ;
190
+ impl FromRequestParts < Arc < PgConnection > > for DatabaseConnection {
191
+ type Rejection = Infallible ;
199
192
200
193
async fn from_request_parts (
201
- parts : & mut Parts ,
202
- state : & S ,
194
+ _parts : & mut Parts ,
195
+ pg_connection : & Arc < PgConnection > ,
203
196
) -> Result < Self , Self :: Rejection > {
204
- let Extension ( pg_connection) =
205
- Extension :: < Arc < PgConnection > > :: from_request_parts ( parts, state)
206
- . await
207
- . map_err ( internal_error) ?;
208
-
209
- Ok ( Self ( pg_connection) )
197
+ Ok ( Self ( pg_connection. clone ( ) ) )
210
198
}
211
199
}
0 commit comments