Skip to content

Commit 82d6825

Browse files
committed
updates for async-sessions v4
1 parent 1bf5106 commit 82d6825

File tree

3 files changed

+51
-24
lines changed

3 files changed

+51
-24
lines changed

.cargo/config.toml

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[env]
2+
RUST_TEST_THREADS = "1"

Cargo.toml

+8-4
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,15 @@ keywords = ["sessions", "tide", "async-session", "redis"]
1212
categories = ["web-programming::http-server", "web-programming", "database"]
1313

1414
[dependencies.redis]
15-
version = "0.21.0"
16-
features = ["aio", "async-std-comp"]
15+
version = "0.22.3"
16+
features = ["aio"]
1717

1818
[dependencies]
19-
async-session = "3.0.0"
19+
async-session = { git = "https://github.com/http-rs/async-session", branch = "overhaul-session-and-session-store", default-features = false }
20+
base64 = "0.21.0"
21+
serde_json = "1.0.93"
22+
thiserror = "1.0.38"
2023

2124
[dev-dependencies]
22-
async-std = { version = "1.9.0", features = ["attributes"] }
25+
redis = { version = "0.22.3", features = ["async-std-comp"] }
26+
async-std = { version = "1.12.0", features = ["attributes"] }

src/lib.rs

+41-20
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
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();
@@ -25,9 +25,27 @@
2525
unused_qualifications
2626
)]
2727

28-
use async_session::{async_trait, serde_json, Result, Session, SessionStore};
28+
use async_session::{async_trait, Session, SessionStore};
2929
use 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)]
3351
pub 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]
117136
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> {
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?;
149170
let key = self.prefix_key(session.id().to_string());
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,7 +235,7 @@ 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));
@@ -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

Comments
 (0)