11//! # async-redis-session
22//! ```rust
3- //! use async_redis_session::RedisSessionStore;
3+ //! use async_redis_session::{ RedisSessionStore, Error} ;
44//! use async_session::{Session, SessionStore};
55//!
6- //! # fn main() -> async_session:: Result { async_std::task::block_on(async {
6+ //! # fn main() -> Result<(), Error> { async_std::task::block_on(async {
77//! let store = RedisSessionStore::new("redis://127.0.0.1/")?;
88//!
99//! let mut session = Session::new();
2525 unused_qualifications
2626) ]
2727
28- use async_session:: { async_trait, serde_json , Result , Session , SessionStore } ;
28+ use async_session:: { async_trait, Session , SessionStore } ;
2929use redis:: { aio:: Connection , AsyncCommands , Client , IntoConnectionInfo , RedisResult } ;
3030
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+
3149/// # RedisSessionStore
3250#[ derive( Clone , Debug ) ]
3351pub struct RedisSessionStore {
@@ -77,12 +95,12 @@ impl RedisSessionStore {
7795 self
7896 }
7997
80- async fn ids ( & self ) -> Result < Vec < String > > {
98+ async fn ids ( & self ) -> Result < Vec < String > , Error > {
8199 Ok ( self . connection ( ) . await ?. keys ( self . prefix_key ( "*" ) ) . await ?)
82100 }
83101
84102 /// 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 > {
86104 if self . prefix . is_none ( ) {
87105 let mut connection = self . connection ( ) . await ?;
88106 Ok ( redis:: cmd ( "DBSIZE" ) . query_async ( & mut connection) . await ?)
@@ -92,7 +110,7 @@ impl RedisSessionStore {
92110 }
93111
94112 #[ 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 > {
96114 Ok ( self
97115 . connection ( )
98116 . await ?
@@ -101,21 +119,24 @@ impl RedisSessionStore {
101119 }
102120
103121 fn prefix_key ( & self , key : impl AsRef < str > ) -> String {
122+ let key = key. as_ref ( ) ;
104123 if let Some ( ref prefix) = self . prefix {
105- format ! ( "{}{}" , prefix , key . as_ref ( ) )
124+ format ! ( "{prefix}{key}" )
106125 } else {
107- key. as_ref ( ) . into ( )
126+ key. to_string ( )
108127 }
109128 }
110129
111130 async fn connection ( & self ) -> RedisResult < Connection > {
112- self . client . get_async_std_connection ( ) . await
131+ self . client . get_async_connection ( ) . await
113132 }
114133}
115134
116135#[ async_trait]
117136impl 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 > {
119140 let id = Session :: id_from_cookie_value ( & cookie_value) ?;
120141 let mut connection = self . connection ( ) . await ?;
121142 let record: Option < String > = connection. get ( self . prefix_key ( id) ) . await ?;
@@ -125,7 +146,7 @@ impl SessionStore for RedisSessionStore {
125146 }
126147 }
127148
128- async fn store_session ( & self , session : Session ) -> Result < Option < String > > {
149+ async fn store_session ( & self , session : Session ) -> Result < Option < String > , Self :: Error > {
129150 let id = self . prefix_key ( session. id ( ) ) ;
130151 let string = serde_json:: to_string ( & session) ?;
131152
@@ -144,14 +165,14 @@ impl SessionStore for RedisSessionStore {
144165 Ok ( session. into_cookie_value ( ) )
145166 }
146167
147- async fn destroy_session ( & self , session : Session ) -> Result {
168+ async fn destroy_session ( & self , session : Session ) -> Result < ( ) , Self :: Error > {
148169 let mut connection = self . connection ( ) . await ?;
149- let key = self . prefix_key ( session. id ( ) . to_string ( ) ) ;
170+ let key = self . prefix_key ( session. id ( ) ) ;
150171 connection. del ( key) . await ?;
151172 Ok ( ( ) )
152173 }
153174
154- async fn clear_store ( & self ) -> Result {
175+ async fn clear_store ( & self ) -> Result < ( ) , Self :: Error > {
155176 let mut connection = self . connection ( ) . await ?;
156177
157178 if self . prefix . is_none ( ) {
@@ -179,7 +200,7 @@ mod tests {
179200 }
180201
181202 #[ 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 > {
183204 let store = test_store ( ) . await ;
184205 let mut session = Session :: new ( ) ;
185206 session. insert ( "key" , "value" ) ?;
@@ -195,7 +216,7 @@ mod tests {
195216 }
196217
197218 #[ async_std:: test]
198- async fn updating_a_session ( ) -> Result {
219+ async fn updating_a_session ( ) -> Result < ( ) , Error > {
199220 let store = test_store ( ) . await ;
200221 let mut session = Session :: new ( ) ;
201222
@@ -214,11 +235,11 @@ mod tests {
214235 }
215236
216237 #[ async_std:: test]
217- async fn updating_a_session_extending_expiry ( ) -> Result {
238+ async fn updating_a_session_extending_expiry ( ) -> Result < ( ) , Error > {
218239 let store = test_store ( ) . await ;
219240 let mut session = Session :: new ( ) ;
220241 session. expire_in ( Duration :: from_secs ( 5 ) ) ;
221- let original_expires = session. expiry ( ) . unwrap ( ) . clone ( ) ;
242+ let original_expires = * session. expiry ( ) . unwrap ( ) ;
222243 let cookie_value = store. store_session ( session) . await ?. unwrap ( ) ;
223244
224245 let mut session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
@@ -227,7 +248,7 @@ mod tests {
227248
228249 assert_eq ! ( session. expiry( ) . unwrap( ) , & original_expires) ;
229250 session. expire_in ( Duration :: from_secs ( 10 ) ) ;
230- let new_expires = session. expiry ( ) . unwrap ( ) . clone ( ) ;
251+ let new_expires = * session. expiry ( ) . unwrap ( ) ;
231252 store. store_session ( session) . await ?;
232253
233254 let session = store. load_session ( cookie_value. clone ( ) ) . await ?. unwrap ( ) ;
@@ -244,7 +265,7 @@ mod tests {
244265 }
245266
246267 #[ async_std:: test]
247- async fn creating_a_new_session_with_expiry ( ) -> Result {
268+ async fn creating_a_new_session_with_expiry ( ) -> Result < ( ) , Error > {
248269 let store = test_store ( ) . await ;
249270 let mut session = Session :: new ( ) ;
250271 session. expire_in ( Duration :: from_secs ( 3 ) ) ;
@@ -268,7 +289,7 @@ mod tests {
268289 }
269290
270291 #[ async_std:: test]
271- async fn destroying_a_single_session ( ) -> Result {
292+ async fn destroying_a_single_session ( ) -> Result < ( ) , Error > {
272293 let store = test_store ( ) . await ;
273294 for _ in 0 ..3i8 {
274295 store. store_session ( Session :: new ( ) ) . await ?;
@@ -287,7 +308,7 @@ mod tests {
287308 }
288309
289310 #[ async_std:: test]
290- async fn clearing_the_whole_store ( ) -> Result {
311+ async fn clearing_the_whole_store ( ) -> Result < ( ) , Error > {
291312 let store = test_store ( ) . await ;
292313 for _ in 0 ..3i8 {
293314 store. store_session ( Session :: new ( ) ) . await ?;
@@ -301,7 +322,7 @@ mod tests {
301322 }
302323
303324 #[ async_std:: test]
304- async fn prefixes ( ) -> Result {
325+ async fn prefixes ( ) -> Result < ( ) , Error > {
305326 test_store ( ) . await ; // clear the db
306327
307328 let store = RedisSessionStore :: new ( "redis://127.0.0.1" ) ?. with_prefix ( "sessions/" ) ;
0 commit comments