9
9
//! let mut session = Session::new();
10
10
//! session.insert("key", "value")?;
11
11
//!
12
- //! let cookie_value = store.store_session(session).await.unwrap();
13
- //! let session = store.load_session(cookie_value).await.unwrap();
12
+ //! let cookie_value = store.store_session(session).await? .unwrap();
13
+ //! let session = store.load_session(cookie_value).await? .unwrap();
14
14
//! assert_eq!(&session.get::<String>("key").unwrap(), "value");
15
15
//! # Ok(()) }) }
16
16
//! ```
@@ -115,32 +115,33 @@ impl RedisSessionStore {
115
115
116
116
#[ async_trait]
117
117
impl SessionStore for RedisSessionStore {
118
- async fn load_session ( & self , cookie_value : String ) -> Option < Session > {
119
- let id = Session :: id_from_cookie_value ( & cookie_value) . ok ( ) ?;
120
- let mut connection = self . connection ( ) . await . ok ( ) ?;
121
- let record: Option < String > = connection. get ( self . prefix_key ( id) ) . await . ok ( ) ?;
118
+ async fn load_session ( & self , cookie_value : String ) -> Result < Option < Session > > {
119
+ let id = Session :: id_from_cookie_value ( & cookie_value) ?;
120
+ let mut connection = self . connection ( ) . await ?;
121
+ let record: Option < String > = connection. get ( self . prefix_key ( id) ) . await ?;
122
122
match record {
123
- Some ( value) => serde_json:: from_str ( & value) . ok ( ) ? ,
124
- None => None ,
123
+ Some ( value) => Ok ( serde_json:: from_str ( & value) ? ) ,
124
+ None => Ok ( None ) ,
125
125
}
126
126
}
127
127
128
- async fn store_session ( & self , session : Session ) -> Option < String > {
128
+ async fn store_session ( & self , session : Session ) -> Result < Option < String > > {
129
129
let id = self . prefix_key ( session. id ( ) ) ;
130
- let string = serde_json:: to_string ( & session) . ok ( ) ?;
130
+ let string = serde_json:: to_string ( & session) ?;
131
131
132
- let mut connection = self . connection ( ) . await . ok ( ) ?;
132
+ let mut connection = self . connection ( ) . await ?;
133
133
134
134
match session. expires_in ( ) {
135
- None => connection. set ( id, string) . await . ok ( ) ?,
135
+ None => connection. set ( id, string) . await ?,
136
136
137
- Some ( expiry) => connection
138
- . set_ex ( id, string, expiry. as_secs ( ) as usize )
139
- . await
140
- . ok ( ) ?,
137
+ Some ( expiry) => {
138
+ connection
139
+ . set_ex ( id, string, expiry. as_secs ( ) as usize )
140
+ . await ?
141
+ }
141
142
} ;
142
143
143
- session. into_cookie_value ( )
144
+ Ok ( session. into_cookie_value ( ) )
144
145
}
145
146
146
147
async fn destroy_session ( & self , session : Session ) -> Result {
@@ -183,9 +184,9 @@ mod tests {
183
184
let mut session = Session :: new ( ) ;
184
185
session. insert ( "key" , "value" ) ?;
185
186
let cloned = session. clone ( ) ;
186
- let cookie_value = store. store_session ( session) . await . unwrap ( ) ;
187
+ let cookie_value = store. store_session ( session) . await ? . unwrap ( ) ;
187
188
188
- let loaded_session = store. load_session ( cookie_value) . await . unwrap ( ) ;
189
+ let loaded_session = store. load_session ( cookie_value) . await ? . unwrap ( ) ;
189
190
assert_eq ! ( cloned. id( ) , loaded_session. id( ) ) ;
190
191
assert_eq ! ( "value" , & loaded_session. get:: <String >( "key" ) . unwrap( ) ) ;
191
192
@@ -199,13 +200,13 @@ mod tests {
199
200
let mut session = Session :: new ( ) ;
200
201
201
202
session. insert ( "key" , "value" ) ?;
202
- let cookie_value = store. store_session ( session) . await . unwrap ( ) ;
203
+ let cookie_value = store. store_session ( session) . await ? . unwrap ( ) ;
203
204
204
- let mut session = store. load_session ( cookie_value. clone ( ) ) . await . unwrap ( ) ;
205
+ let mut session = store. load_session ( cookie_value. clone ( ) ) . await ? . unwrap ( ) ;
205
206
session. insert ( "key" , "other value" ) ?;
206
- assert_eq ! ( None , store. store_session( session) . await ) ;
207
+ assert_eq ! ( None , store. store_session( session) . await ? ) ;
207
208
208
- let session = store. load_session ( cookie_value. clone ( ) ) . await . unwrap ( ) ;
209
+ let session = store. load_session ( cookie_value. clone ( ) ) . await ? . unwrap ( ) ;
209
210
assert_eq ! ( & session. get:: <String >( "key" ) . unwrap( ) , "other value" ) ;
210
211
211
212
assert_eq ! ( 1 , store. count( ) . await . unwrap( ) ) ;
@@ -218,18 +219,18 @@ mod tests {
218
219
let mut session = Session :: new ( ) ;
219
220
session. expire_in ( Duration :: from_secs ( 5 ) ) ;
220
221
let original_expires = session. expiry ( ) . unwrap ( ) . clone ( ) ;
221
- let cookie_value = store. store_session ( session) . await . unwrap ( ) ;
222
+ let cookie_value = store. store_session ( session) . await ? . unwrap ( ) ;
222
223
223
- let mut session = store. load_session ( cookie_value. clone ( ) ) . await . unwrap ( ) ;
224
+ let mut session = store. load_session ( cookie_value. clone ( ) ) . await ? . unwrap ( ) ;
224
225
let ttl = store. ttl_for_session ( & session) . await ?;
225
226
assert ! ( ttl > 3 && ttl < 5 ) ;
226
227
227
228
assert_eq ! ( session. expiry( ) . unwrap( ) , & original_expires) ;
228
229
session. expire_in ( Duration :: from_secs ( 10 ) ) ;
229
230
let new_expires = session. expiry ( ) . unwrap ( ) . clone ( ) ;
230
- store. store_session ( session) . await ;
231
+ store. store_session ( session) . await ? ;
231
232
232
- let session = store. load_session ( cookie_value. clone ( ) ) . await . unwrap ( ) ;
233
+ let session = store. load_session ( cookie_value. clone ( ) ) . await ? . unwrap ( ) ;
233
234
let ttl = store. ttl_for_session ( & session) . await ?;
234
235
assert ! ( ttl > 8 && ttl < 10 ) ;
235
236
assert_eq ! ( session. expiry( ) . unwrap( ) , & new_expires) ;
@@ -250,18 +251,18 @@ mod tests {
250
251
session. insert ( "key" , "value" ) ?;
251
252
let cloned = session. clone ( ) ;
252
253
253
- let cookie_value = store. store_session ( session) . await . unwrap ( ) ;
254
+ let cookie_value = store. store_session ( session) . await ? . unwrap ( ) ;
254
255
255
256
assert ! ( store. ttl_for_session( & cloned) . await ? > 1 ) ;
256
257
257
- let loaded_session = store. load_session ( cookie_value. clone ( ) ) . await . unwrap ( ) ;
258
+ let loaded_session = store. load_session ( cookie_value. clone ( ) ) . await ? . unwrap ( ) ;
258
259
assert_eq ! ( cloned. id( ) , loaded_session. id( ) ) ;
259
260
assert_eq ! ( "value" , & loaded_session. get:: <String >( "key" ) . unwrap( ) ) ;
260
261
261
262
assert ! ( !loaded_session. is_expired( ) ) ;
262
263
263
264
task:: sleep ( Duration :: from_secs ( 2 ) ) . await ;
264
- assert_eq ! ( None , store. load_session( cookie_value) . await ) ;
265
+ assert_eq ! ( None , store. load_session( cookie_value) . await ? ) ;
265
266
266
267
Ok ( ( ) )
267
268
}
@@ -270,14 +271,14 @@ mod tests {
270
271
async fn destroying_a_single_session ( ) -> Result {
271
272
let store = test_store ( ) . await ;
272
273
for _ in 0 ..3i8 {
273
- store. store_session ( Session :: new ( ) ) . await ;
274
+ store. store_session ( Session :: new ( ) ) . await ? ;
274
275
}
275
276
276
- let cookie = store. store_session ( Session :: new ( ) ) . await . unwrap ( ) ;
277
+ let cookie = store. store_session ( Session :: new ( ) ) . await ? . unwrap ( ) ;
277
278
assert_eq ! ( 4 , store. count( ) . await ?) ;
278
- let session = store. load_session ( cookie. clone ( ) ) . await . unwrap ( ) ;
279
+ let session = store. load_session ( cookie. clone ( ) ) . await ? . unwrap ( ) ;
279
280
store. destroy_session ( session. clone ( ) ) . await . unwrap ( ) ;
280
- assert_eq ! ( None , store. load_session( cookie) . await ) ;
281
+ assert_eq ! ( None , store. load_session( cookie) . await ? ) ;
281
282
assert_eq ! ( 3 , store. count( ) . await ?) ;
282
283
283
284
// attempting to destroy the session again is not an error
@@ -289,7 +290,7 @@ mod tests {
289
290
async fn clearing_the_whole_store ( ) -> Result {
290
291
let store = test_store ( ) . await ;
291
292
for _ in 0 ..3i8 {
292
- store. store_session ( Session :: new ( ) ) . await ;
293
+ store. store_session ( Session :: new ( ) ) . await ? ;
293
294
}
294
295
295
296
assert_eq ! ( 3 , store. count( ) . await ?) ;
@@ -307,19 +308,19 @@ mod tests {
307
308
store. clear_store ( ) . await ?;
308
309
309
310
for _ in 0 ..3i8 {
310
- store. store_session ( Session :: new ( ) ) . await ;
311
+ store. store_session ( Session :: new ( ) ) . await ? ;
311
312
}
312
313
313
314
let mut session = Session :: new ( ) ;
314
315
315
316
session. insert ( "key" , "value" ) ?;
316
- let cookie_value = store. store_session ( session) . await . unwrap ( ) ;
317
+ let cookie_value = store. store_session ( session) . await ? . unwrap ( ) ;
317
318
318
- let mut session = store. load_session ( cookie_value. clone ( ) ) . await . unwrap ( ) ;
319
+ let mut session = store. load_session ( cookie_value. clone ( ) ) . await ? . unwrap ( ) ;
319
320
session. insert ( "key" , "other value" ) ?;
320
- assert_eq ! ( None , store. store_session( session) . await ) ;
321
+ assert_eq ! ( None , store. store_session( session) . await ? ) ;
321
322
322
- let session = store. load_session ( cookie_value. clone ( ) ) . await . unwrap ( ) ;
323
+ let session = store. load_session ( cookie_value. clone ( ) ) . await ? . unwrap ( ) ;
323
324
assert_eq ! ( & session. get:: <String >( "key" ) . unwrap( ) , "other value" ) ;
324
325
325
326
assert_eq ! ( 4 , store. count( ) . await . unwrap( ) ) ;
@@ -329,7 +330,7 @@ mod tests {
329
330
330
331
assert_eq ! ( 0 , other_store. count( ) . await . unwrap( ) ) ;
331
332
for _ in 0 ..3i8 {
332
- other_store. store_session ( Session :: new ( ) ) . await ;
333
+ other_store. store_session ( Session :: new ( ) ) . await ? ;
333
334
}
334
335
335
336
other_store. clear_store ( ) . await ?;
0 commit comments