1
1
//! # async-redis-session
2
2
//! ```rust
3
- //! use async_redis_session::RedisSessionStore;
3
+ //! use async_redis_session::{ RedisSessionStore, Error} ;
4
4
//! use async_session::{Session, SessionStore};
5
5
//!
6
- //! # fn main() -> async_session:: Result { async_std::task::block_on(async {
6
+ //! # fn main() -> Result<(), Error> { async_std::task::block_on(async {
7
7
//! let store = RedisSessionStore::new("redis://127.0.0.1/")?;
8
8
//!
9
9
//! let mut session = Session::new();
25
25
unused_qualifications
26
26
) ]
27
27
28
- use async_session:: { async_trait, serde_json , Result , Session , SessionStore } ;
28
+ use async_session:: { async_trait, Session , SessionStore } ;
29
29
use redis:: { aio:: Connection , AsyncCommands , Client , IntoConnectionInfo , RedisResult } ;
30
30
31
+ /// Errors that can arise in the operation of the session stores
32
+ /// included in this crate
33
+ #[ derive( thiserror:: Error , Debug ) ]
34
+ #[ non_exhaustive]
35
+ pub enum Error {
36
+ /// an error that comes from sqlx
37
+ #[ error( transparent) ]
38
+ Redis ( #[ from] redis:: RedisError ) ,
39
+
40
+ /// an error that comes from serde_json
41
+ #[ error( transparent) ]
42
+ SerdeJson ( #[ from] serde_json:: Error ) ,
43
+
44
+ /// an error that comes from base64
45
+ #[ error( transparent) ]
46
+ Base64 ( #[ from] base64:: DecodeError ) ,
47
+ }
48
+
31
49
/// # RedisSessionStore
32
50
#[ derive( Clone , Debug ) ]
33
51
pub struct RedisSessionStore {
@@ -77,12 +95,12 @@ impl RedisSessionStore {
77
95
self
78
96
}
79
97
80
- async fn ids ( & self ) -> Result < Vec < String > > {
98
+ async fn ids ( & self ) -> Result < Vec < String > , Error > {
81
99
Ok ( self . connection ( ) . await ?. keys ( self . prefix_key ( "*" ) ) . await ?)
82
100
}
83
101
84
102
/// returns the number of sessions in this store
85
- pub async fn count ( & self ) -> Result < usize > {
103
+ pub async fn count ( & self ) -> Result < usize , Error > {
86
104
if self . prefix . is_none ( ) {
87
105
let mut connection = self . connection ( ) . await ?;
88
106
Ok ( redis:: cmd ( "DBSIZE" ) . query_async ( & mut connection) . await ?)
@@ -92,7 +110,7 @@ impl RedisSessionStore {
92
110
}
93
111
94
112
#[ cfg( test) ]
95
- async fn ttl_for_session ( & self , session : & Session ) -> Result < usize > {
113
+ async fn ttl_for_session ( & self , session : & Session ) -> Result < usize , Error > {
96
114
Ok ( self
97
115
. connection ( )
98
116
. await ?
@@ -101,21 +119,24 @@ impl RedisSessionStore {
101
119
}
102
120
103
121
fn prefix_key ( & self , key : impl AsRef < str > ) -> String {
122
+ let key = key. as_ref ( ) ;
104
123
if let Some ( ref prefix) = self . prefix {
105
- format ! ( "{}{}" , prefix , key . as_ref ( ) )
124
+ format ! ( "{prefix}{key}" )
106
125
} else {
107
- key. as_ref ( ) . into ( )
126
+ key. to_string ( )
108
127
}
109
128
}
110
129
111
130
async fn connection ( & self ) -> RedisResult < Connection > {
112
- self . client . get_async_std_connection ( ) . await
131
+ self . client . get_async_connection ( ) . await
113
132
}
114
133
}
115
134
116
135
#[ async_trait]
117
136
impl SessionStore for RedisSessionStore {
118
- async fn load_session ( & self , cookie_value : String ) -> Result < Option < Session > > {
137
+ type Error = Error ;
138
+
139
+ async fn load_session ( & self , cookie_value : String ) -> Result < Option < Session > , Self :: Error > {
119
140
let id = Session :: id_from_cookie_value ( & cookie_value) ?;
120
141
let mut connection = self . connection ( ) . await ?;
121
142
let record: Option < String > = connection. get ( self . prefix_key ( id) ) . await ?;
@@ -125,7 +146,7 @@ impl SessionStore for RedisSessionStore {
125
146
}
126
147
}
127
148
128
- async fn store_session ( & self , session : Session ) -> Result < Option < String > > {
149
+ async fn store_session ( & self , session : Session ) -> Result < Option < String > , Self :: Error > {
129
150
let id = self . prefix_key ( session. id ( ) ) ;
130
151
let string = serde_json:: to_string ( & session) ?;
131
152
@@ -144,14 +165,14 @@ impl SessionStore for RedisSessionStore {
144
165
Ok ( session. into_cookie_value ( ) )
145
166
}
146
167
147
- async fn destroy_session ( & self , session : Session ) -> Result {
168
+ async fn destroy_session ( & self , session : Session ) -> Result < ( ) , Self :: Error > {
148
169
let mut connection = self . connection ( ) . await ?;
149
170
let key = self . prefix_key ( session. id ( ) . to_string ( ) ) ;
150
171
connection. del ( key) . await ?;
151
172
Ok ( ( ) )
152
173
}
153
174
154
- async fn clear_store ( & self ) -> Result {
175
+ async fn clear_store ( & self ) -> Result < ( ) , Self :: Error > {
155
176
let mut connection = self . connection ( ) . await ?;
156
177
157
178
if self . prefix . is_none ( ) {
@@ -179,7 +200,7 @@ mod tests {
179
200
}
180
201
181
202
#[ async_std:: test]
182
- async fn creating_a_new_session_with_no_expiry ( ) -> Result {
203
+ async fn creating_a_new_session_with_no_expiry ( ) -> Result < ( ) , Error > {
183
204
let store = test_store ( ) . await ;
184
205
let mut session = Session :: new ( ) ;
185
206
session. insert ( "key" , "value" ) ?;
@@ -195,7 +216,7 @@ mod tests {
195
216
}
196
217
197
218
#[ async_std:: test]
198
- async fn updating_a_session ( ) -> Result {
219
+ async fn updating_a_session ( ) -> Result < ( ) , Error > {
199
220
let store = test_store ( ) . await ;
200
221
let mut session = Session :: new ( ) ;
201
222
@@ -214,7 +235,7 @@ mod tests {
214
235
}
215
236
216
237
#[ async_std:: test]
217
- async fn updating_a_session_extending_expiry ( ) -> Result {
238
+ async fn updating_a_session_extending_expiry ( ) -> Result < ( ) , Error > {
218
239
let store = test_store ( ) . await ;
219
240
let mut session = Session :: new ( ) ;
220
241
session. expire_in ( Duration :: from_secs ( 5 ) ) ;
@@ -244,7 +265,7 @@ mod tests {
244
265
}
245
266
246
267
#[ async_std:: test]
247
- async fn creating_a_new_session_with_expiry ( ) -> Result {
268
+ async fn creating_a_new_session_with_expiry ( ) -> Result < ( ) , Error > {
248
269
let store = test_store ( ) . await ;
249
270
let mut session = Session :: new ( ) ;
250
271
session. expire_in ( Duration :: from_secs ( 3 ) ) ;
@@ -268,7 +289,7 @@ mod tests {
268
289
}
269
290
270
291
#[ async_std:: test]
271
- async fn destroying_a_single_session ( ) -> Result {
292
+ async fn destroying_a_single_session ( ) -> Result < ( ) , Error > {
272
293
let store = test_store ( ) . await ;
273
294
for _ in 0 ..3i8 {
274
295
store. store_session ( Session :: new ( ) ) . await ?;
@@ -287,7 +308,7 @@ mod tests {
287
308
}
288
309
289
310
#[ async_std:: test]
290
- async fn clearing_the_whole_store ( ) -> Result {
311
+ async fn clearing_the_whole_store ( ) -> Result < ( ) , Error > {
291
312
let store = test_store ( ) . await ;
292
313
for _ in 0 ..3i8 {
293
314
store. store_session ( Session :: new ( ) ) . await ?;
@@ -301,7 +322,7 @@ mod tests {
301
322
}
302
323
303
324
#[ async_std:: test]
304
- async fn prefixes ( ) -> Result {
325
+ async fn prefixes ( ) -> Result < ( ) , Error > {
305
326
test_store ( ) . await ; // clear the db
306
327
307
328
let store = RedisSessionStore :: new ( "redis://127.0.0.1" ) ?. with_prefix ( "sessions/" ) ;
0 commit comments