Skip to content

Commit 430020d

Browse files
committed
update to 2.0.0
1 parent 0ad2273 commit 430020d

File tree

2 files changed

+43
-45
lines changed

2 files changed

+43
-45
lines changed

Cargo.toml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ edition = "2018"
66

77
[dependencies]
88
redis = { version = "0.16.0", features = ["aio"] }
9-
async-session = "1.0.2"
9+
async-session = "2.0.0"
1010

1111
[dev-dependencies]
1212
async-std = { version = "1.6.2", features = ["attributes"] }
13-
14-
[patch.crates-io]
15-
async-session = { git = "https://github.com/jbr/async-session", branch = "tide" }

src/lib.rs

Lines changed: 42 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
//! let mut session = Session::new();
1010
//! session.insert("key", "value")?;
1111
//!
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();
1414
//! assert_eq!(&session.get::<String>("key").unwrap(), "value");
1515
//! # Ok(()) }) }
1616
//! ```
@@ -115,32 +115,33 @@ impl RedisSessionStore {
115115

116116
#[async_trait]
117117
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?;
122122
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),
125125
}
126126
}
127127

128-
async fn store_session(&self, session: Session) -> Option<String> {
128+
async fn store_session(&self, session: Session) -> Result<Option<String>> {
129129
let id = self.prefix_key(session.id());
130-
let string = serde_json::to_string(&session).ok()?;
130+
let string = serde_json::to_string(&session)?;
131131

132-
let mut connection = self.connection().await.ok()?;
132+
let mut connection = self.connection().await?;
133133

134134
match session.expires_in() {
135-
None => connection.set(id, string).await.ok()?,
135+
None => connection.set(id, string).await?,
136136

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+
}
141142
};
142143

143-
session.into_cookie_value()
144+
Ok(session.into_cookie_value())
144145
}
145146

146147
async fn destroy_session(&self, session: Session) -> Result {
@@ -183,9 +184,9 @@ mod tests {
183184
let mut session = Session::new();
184185
session.insert("key", "value")?;
185186
let cloned = session.clone();
186-
let cookie_value = store.store_session(session).await.unwrap();
187+
let cookie_value = store.store_session(session).await?.unwrap();
187188

188-
let loaded_session = store.load_session(cookie_value).await.unwrap();
189+
let loaded_session = store.load_session(cookie_value).await?.unwrap();
189190
assert_eq!(cloned.id(), loaded_session.id());
190191
assert_eq!("value", &loaded_session.get::<String>("key").unwrap());
191192

@@ -199,13 +200,13 @@ mod tests {
199200
let mut session = Session::new();
200201

201202
session.insert("key", "value")?;
202-
let cookie_value = store.store_session(session).await.unwrap();
203+
let cookie_value = store.store_session(session).await?.unwrap();
203204

204-
let mut session = store.load_session(cookie_value.clone()).await.unwrap();
205+
let mut session = store.load_session(cookie_value.clone()).await?.unwrap();
205206
session.insert("key", "other value")?;
206-
assert_eq!(None, store.store_session(session).await);
207+
assert_eq!(None, store.store_session(session).await?);
207208

208-
let session = store.load_session(cookie_value.clone()).await.unwrap();
209+
let session = store.load_session(cookie_value.clone()).await?.unwrap();
209210
assert_eq!(&session.get::<String>("key").unwrap(), "other value");
210211

211212
assert_eq!(1, store.count().await.unwrap());
@@ -218,18 +219,18 @@ mod tests {
218219
let mut session = Session::new();
219220
session.expire_in(Duration::from_secs(5));
220221
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();
222223

223-
let mut session = store.load_session(cookie_value.clone()).await.unwrap();
224+
let mut session = store.load_session(cookie_value.clone()).await?.unwrap();
224225
let ttl = store.ttl_for_session(&session).await?;
225226
assert!(ttl > 3 && ttl < 5);
226227

227228
assert_eq!(session.expiry().unwrap(), &original_expires);
228229
session.expire_in(Duration::from_secs(10));
229230
let new_expires = session.expiry().unwrap().clone();
230-
store.store_session(session).await;
231+
store.store_session(session).await?;
231232

232-
let session = store.load_session(cookie_value.clone()).await.unwrap();
233+
let session = store.load_session(cookie_value.clone()).await?.unwrap();
233234
let ttl = store.ttl_for_session(&session).await?;
234235
assert!(ttl > 8 && ttl < 10);
235236
assert_eq!(session.expiry().unwrap(), &new_expires);
@@ -250,18 +251,18 @@ mod tests {
250251
session.insert("key", "value")?;
251252
let cloned = session.clone();
252253

253-
let cookie_value = store.store_session(session).await.unwrap();
254+
let cookie_value = store.store_session(session).await?.unwrap();
254255

255256
assert!(store.ttl_for_session(&cloned).await? > 1);
256257

257-
let loaded_session = store.load_session(cookie_value.clone()).await.unwrap();
258+
let loaded_session = store.load_session(cookie_value.clone()).await?.unwrap();
258259
assert_eq!(cloned.id(), loaded_session.id());
259260
assert_eq!("value", &loaded_session.get::<String>("key").unwrap());
260261

261262
assert!(!loaded_session.is_expired());
262263

263264
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?);
265266

266267
Ok(())
267268
}
@@ -270,14 +271,14 @@ mod tests {
270271
async fn destroying_a_single_session() -> Result {
271272
let store = test_store().await;
272273
for _ in 0..3i8 {
273-
store.store_session(Session::new()).await;
274+
store.store_session(Session::new()).await?;
274275
}
275276

276-
let cookie = store.store_session(Session::new()).await.unwrap();
277+
let cookie = store.store_session(Session::new()).await?.unwrap();
277278
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();
279280
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?);
281282
assert_eq!(3, store.count().await?);
282283

283284
// attempting to destroy the session again is not an error
@@ -289,7 +290,7 @@ mod tests {
289290
async fn clearing_the_whole_store() -> Result {
290291
let store = test_store().await;
291292
for _ in 0..3i8 {
292-
store.store_session(Session::new()).await;
293+
store.store_session(Session::new()).await?;
293294
}
294295

295296
assert_eq!(3, store.count().await?);
@@ -307,19 +308,19 @@ mod tests {
307308
store.clear_store().await?;
308309

309310
for _ in 0..3i8 {
310-
store.store_session(Session::new()).await;
311+
store.store_session(Session::new()).await?;
311312
}
312313

313314
let mut session = Session::new();
314315

315316
session.insert("key", "value")?;
316-
let cookie_value = store.store_session(session).await.unwrap();
317+
let cookie_value = store.store_session(session).await?.unwrap();
317318

318-
let mut session = store.load_session(cookie_value.clone()).await.unwrap();
319+
let mut session = store.load_session(cookie_value.clone()).await?.unwrap();
319320
session.insert("key", "other value")?;
320-
assert_eq!(None, store.store_session(session).await);
321+
assert_eq!(None, store.store_session(session).await?);
321322

322-
let session = store.load_session(cookie_value.clone()).await.unwrap();
323+
let session = store.load_session(cookie_value.clone()).await?.unwrap();
323324
assert_eq!(&session.get::<String>("key").unwrap(), "other value");
324325

325326
assert_eq!(4, store.count().await.unwrap());
@@ -329,7 +330,7 @@ mod tests {
329330

330331
assert_eq!(0, other_store.count().await.unwrap());
331332
for _ in 0..3i8 {
332-
other_store.store_session(Session::new()).await;
333+
other_store.store_session(Session::new()).await?;
333334
}
334335

335336
other_store.clear_store().await?;

0 commit comments

Comments
 (0)