Skip to content

Commit 5985931

Browse files
authored
Merge pull request #17 from TiloPapke/master
Updating dependencies to use libary with current mongodb libary
2 parents ecd4b33 + d54b507 commit 5985931

File tree

4 files changed

+24
-19
lines changed

4 files changed

+24
-19
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ target/
22
tmp/
33
Cargo.lock
44
.DS_Store
5+
.vscode/launch.json
6+
.cargo/config.toml

Cargo.toml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@ authors = [
2323
[features]
2424

2525
[dependencies]
26-
mongodb = { version = "1.1.1", default-features = false, features = ["async-std-runtime"] }
27-
async-session = "2.0.0"
26+
mongodb = { version = "2.2.1", default-features = false, features = ["async-std-runtime", "bson-chrono-0_4"] }
27+
# can not go higher due to dev-dependencie "tide" which requires "async-session" in version 2.0.1
28+
async-session = "2.0.1"
2829

2930
[dev-dependencies]
30-
async-std = { version = "1.8.0", features = ["attributes"] }
31-
rand = {version = "0.7.3"}
32-
lazy_static = "1"
33-
tide = "0.15"
31+
async-std = { version = "1.11.0", features = ["attributes"] }
32+
rand = {version = "0.8.5"}
33+
lazy_static = "1.4.0"
34+
tide = "0.16"

src/lib.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717

1818
use async_session::chrono::{Duration, Utc};
1919
use async_session::{async_trait, Result, Session, SessionStore};
20-
use mongodb::bson;
21-
use mongodb::bson::{doc, Bson};
20+
use mongodb::{bson, Collection};
21+
use mongodb::bson::{doc, Bson, Document};
2222
use mongodb::options::{ReplaceOptions, SelectionCriteria};
2323
use mongodb::Client;
2424

@@ -89,7 +89,7 @@ impl MongodbSessionStore {
8989
/// # Ok(()) }) }
9090
/// ```
9191
pub async fn initialize(&self) -> Result {
92-
&self.index_on_expiry_at().await?;
92+
let _ = &self.index_on_expiry_at().await?;
9393
Ok(())
9494
}
9595

@@ -162,7 +162,7 @@ impl SessionStore for MongodbSessionStore {
162162

163163
async fn load_session(&self, cookie_value: String) -> Result<Option<Session>> {
164164
let id = Session::id_from_cookie_value(&cookie_value)?;
165-
let coll = self.client.database(&self.db).collection(&self.coll_name);
165+
let coll:Collection<Document> = self.client.database(&self.db).collection(&self.coll_name);
166166
let filter = doc! { "session_id": id };
167167
match coll.find_one(filter, None).await? {
168168
None => Ok(None),
@@ -175,7 +175,7 @@ impl SessionStore for MongodbSessionStore {
175175
// https://docs.mongodb.com/manual/core/index-ttl/#timing-of-the-delete-operation
176176
// This prevents those documents being returned
177177
if let Some(expiry_at) = doc.get("expireAt").and_then(Bson::as_datetime) {
178-
if expiry_at < &Utc::now() {
178+
if expiry_at < &Utc::now().into() {
179179
return Ok(None);
180180
}
181181
}
@@ -185,14 +185,14 @@ impl SessionStore for MongodbSessionStore {
185185
}
186186

187187
async fn destroy_session(&self, session: Session) -> Result {
188-
let coll = self.client.database(&self.db).collection(&self.coll_name);
188+
let coll:Collection<Document> = self.client.database(&self.db).collection(&self.coll_name);
189189
coll.delete_one(doc! { "session_id": session.id() }, None)
190190
.await?;
191191
Ok(())
192192
}
193193

194194
async fn clear_store(&self) -> Result {
195-
let coll = self.client.database(&self.db).collection(&self.coll_name);
195+
let coll:Collection<Document> = self.client.database(&self.db).collection(&self.coll_name);
196196
coll.drop(None).await?;
197197
self.initialize().await?;
198198
Ok(())

tests/test.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,16 @@ mod tests {
1010
lazy_static! {
1111
static ref HOST: String = env::var("HOST").unwrap_or_else(|_| "127.0.0.1".to_string());
1212
static ref PORT: String = env::var("PORT").unwrap_or_else(|_| "27017".to_string());
13+
static ref DATABASE: String = env::var("DATABASE").unwrap_or_else(|_| "db_name".to_string());
14+
static ref COLLECTION: String = env::var("COLLECTION").unwrap_or_else(|_| "collection".to_string());
1315
static ref CONNECTION_STRING: String =
1416
format!("mongodb://{}:{}/", HOST.as_str(), PORT.as_str());
1517
}
1618

1719
#[test]
1820
fn test_from_client() -> async_session::Result {
1921
async_std::task::block_on(async {
20-
let client_options = match ClientOptions::parse(&CONNECTION_STRING).await {
22+
let client_options = match ClientOptions::parse(&*CONNECTION_STRING).await {
2123
Ok(c) => c,
2224
Err(e) => panic!("Client Options Failed: {}", e),
2325
};
@@ -26,8 +28,8 @@ mod tests {
2628
Ok(c) => c,
2729
Err(e) => panic!("Client Creation Failed: {}", e),
2830
};
29-
30-
let store = MongodbSessionStore::from_client(client, "db_name", "collection");
31+
32+
let store = MongodbSessionStore::from_client(client, &DATABASE, &COLLECTION);
3133
let mut rng = rand::thread_rng();
3234
let n2: u16 = rng.gen();
3335
let key = format!("key-{}", n2);
@@ -47,7 +49,7 @@ mod tests {
4749
fn test_new() -> async_session::Result {
4850
async_std::task::block_on(async {
4951
let store =
50-
MongodbSessionStore::new(&CONNECTION_STRING, "db_name", "collection").await?;
52+
MongodbSessionStore::new(&CONNECTION_STRING, &DATABASE, &COLLECTION).await?;
5153

5254
let mut rng = rand::thread_rng();
5355
let n2: u16 = rng.gen();
@@ -68,7 +70,7 @@ mod tests {
6870
fn test_with_expire() -> async_session::Result {
6971
async_std::task::block_on(async {
7072
let store =
71-
MongodbSessionStore::new(&CONNECTION_STRING, "db_name", "collection").await?;
73+
MongodbSessionStore::new(&CONNECTION_STRING, &DATABASE, &COLLECTION).await?;
7274

7375
store.initialize().await?;
7476

@@ -94,7 +96,7 @@ mod tests {
9496
use std::time::Duration;
9597
async_std::task::block_on(async {
9698
let store =
97-
MongodbSessionStore::new(&CONNECTION_STRING, "db_name", "collection").await?;
99+
MongodbSessionStore::new(&CONNECTION_STRING, &DATABASE, &COLLECTION).await?;
98100

99101
store.initialize().await?;
100102

0 commit comments

Comments
 (0)