Skip to content

Commit e2a5080

Browse files
committed
Merge branch 'main' into RUST-1529-no-ff
2 parents f5a65af + b4308cf commit e2a5080

File tree

8 files changed

+93
-35
lines changed

8 files changed

+93
-35
lines changed

Cargo.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
# MongoDB Rust Driver
2+
23
[![Crates.io](https://img.shields.io/crates/v/mongodb.svg)](https://crates.io/crates/mongodb) [![docs.rs](https://docs.rs/mongodb/badge.svg)](https://docs.rs/mongodb) [![License](https://img.shields.io/badge/license-Apache%202.0-blue.svg)](https://github.com/mongodb/mongo-rust-driver/blob/main/LICENSE)
34

45
This is the officially supported MongoDB Rust driver, a client side library that can be used to interact with MongoDB deployments in Rust applications. It uses the [`bson`](https://docs.rs/bson/latest) crate for BSON support. The driver contains a fully async API that requires [`tokio`](https://docs.rs/tokio). The driver also has a sync API that may be enabled via feature flags. The MongoDB Rust driver follows [semantic versioning](https://semver.org/) for its releases.
56

67
For more details, including features, runnable examples, troubleshooting resources, and more, please see the [official documentation](https://www.mongodb.com/docs/drivers/rust/current/).
78

89
## Installation
10+
911
### Requirements
12+
1013
- Rust 1.82.0+ (See the [MSRV policy](#minimum-supported-rust-version-msrv-policy) for more information)
1114
- MongoDB 4.0+
1215

@@ -15,7 +18,9 @@ For more details, including features, runnable examples, troubleshooting resourc
1518
The driver tests against Linux, MacOS, and Windows in CI.
1619

1720
### Importing
21+
1822
The driver is available on [crates.io](https://crates.io/crates/mongodb). To use the driver in your application, simply add it to your project's `Cargo.toml`.
23+
1924
```toml
2025
[dependencies]
2126
mongodb = "3.2.3"
@@ -24,35 +29,45 @@ mongodb = "3.2.3"
2429
Version 1 of this crate has reached end of life and will no longer be receiving any updates or bug fixes, so all users are recommended to always depend on the latest 2.x release. See the [2.0.0 release notes](https://github.com/mongodb/mongo-rust-driver/releases/tag/v2.0.0) for migration information if upgrading from a 1.x version.
2530

2631
#### Enabling the sync API
32+
2733
The driver also provides a blocking sync API. To enable this, add the `"sync"` feature to your `Cargo.toml`:
34+
2835
```toml
2936
[dependencies.mongodb]
3037
version = "3.2.3"
3138
features = ["sync"]
3239
```
40+
3341
**Note:** The sync-specific types can be imported from `mongodb::sync` (e.g. `mongodb::sync::Client`).
3442

3543
### All Feature Flags
3644

37-
| Feature | Description |
38-
|:-----------------------------|:-----------------------------|
39-
| `dns-resolver` | Enable DNS resolution to allow `mongodb+srv` URI handling. **Enabled by default.** |
40-
| `rustls-tls` | Use [`rustls`](https://docs.rs/rustls/latest/rustls/) for TLS connection handling. **Enabled by default.** |
41-
| `openssl-tls` | Use [`openssl`](https://docs.rs/openssl/latest/openssl/) for TLS connection handling. |
42-
| `sync` | Expose the synchronous API (`mongodb::sync`). |
43-
| `aws-auth` | Enable support for the MONGODB-AWS authentication mechanism. |
44-
| `zlib-compression` | Enable support for compressing messages with [`zlib`](https://zlib.net/). |
45-
| `zstd-compression` | Enable support for compressing messages with [`zstd`](http://facebook.github.io/zstd/). |
46-
| `snappy-compression` | Enable support for compressing messages with [`snappy`](http://google.github.io/snappy/). |
47-
| `in-use-encryption` | Enable support for client-side field level encryption and queryable encryption. Note that re-exports from the `mongocrypt` crate may change in backwards-incompatible ways while that crate is below version 1.0. |
48-
| `tracing-unstable` | Enable support for emitting [`tracing`](https://docs.rs/tracing/latest/tracing/) events. This API is unstable and may be subject to breaking changes in minor releases. |
49-
| `compat-3-0-0` | Required for future compatibility if default features are disabled. |
45+
| Feature | Description |
46+
| :------------------- | :---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
47+
| `dns-resolver` | Enable DNS resolution to allow `mongodb+srv` URI handling. **Enabled by default.** |
48+
| `rustls-tls` | Use [`rustls`](https://docs.rs/rustls/latest/rustls/) for TLS connection handling. **Enabled by default.** |
49+
| `openssl-tls` | Use [`openssl`](https://docs.rs/openssl/latest/openssl/) for TLS connection handling. |
50+
| `sync` | Expose the synchronous API (`mongodb::sync`). |
51+
| `aws-auth` | Enable support for the MONGODB-AWS authentication mechanism. |
52+
| `zlib-compression` | Enable support for compressing messages with [`zlib`](https://zlib.net/). |
53+
| `zstd-compression` | Enable support for compressing messages with [`zstd`](http://facebook.github.io/zstd/). |
54+
| `snappy-compression` | Enable support for compressing messages with [`snappy`](http://google.github.io/snappy/). |
55+
| `in-use-encryption` | Enable support for client-side field level encryption and queryable encryption. Note that re-exports from the `mongocrypt` crate may change in backwards-incompatible ways while that crate is below version 1.0. |
56+
| `tracing-unstable` | Enable support for emitting [`tracing`](https://docs.rs/tracing/latest/tracing/) events. This API is unstable and may be subject to breaking changes in minor releases. |
57+
| `compat-3-0-0` | Required for future compatibility if default features are disabled. |
5058

5159
## Web Framework Examples
60+
5261
### Actix
62+
5363
The driver can be used easily with the Actix web framework by storing a `Client` in Actix application data. A full example application for using MongoDB with Actix can be found [here](https://github.com/actix/examples/tree/master/databases/mongodb).
5464

65+
### Axum
66+
67+
A simple CRUD API example using Axum and MongoDB can be found [here](https://github.com/tokio-rs/axum/tree/main/examples/mongodb).
68+
5569
### Rocket
70+
5671
The Rocket web framework provides built-in support for MongoDB via the Rust driver. The documentation for the [`rocket_db_pools`](https://api.rocket.rs/v0.5/rocket_db_pools/index.html) crate contains instructions for using MongoDB with your Rocket application.
5772

5873
## Note on connecting to Atlas deployments
@@ -78,7 +93,9 @@ the driver's futures directly. This will ensure the driver's futures will always
7893
while also allowing the application to continue in the event of a timeout.
7994

8095
## Bug Reporting / Feature Requests
96+
8197
To file a bug report or submit a feature request, please open a ticket on our [Jira project](https://jira.mongodb.org/browse/RUST):
98+
8299
- Create an account and login at [jira.mongodb.org](https://jira.mongodb.org)
83100
- Navigate to the RUST project at [jira.mongodb.org/browse/RUST](https://jira.mongodb.org/browse/RUST)
84101
- Click **Create Issue** - If the ticket you are filing is a bug report, please include as much detail as possible about the issue and how to reproduce it.
@@ -90,29 +107,38 @@ Before filing a ticket, please use the search functionality of Jira to see if a
90107
We encourage and would happily accept contributions in the form of GitHub pull requests. Before opening one, be sure to run the tests locally; check out the [testing section](#running-the-tests) for information on how to do that. Once you open a pull request, your branch will be run against the same testing matrix that we use for our [continuous integration](#continuous-integration) system, so it is usually sufficient to only run the integration tests locally against a standalone. Remember to always run the linter tests before opening a pull request.
91108

92109
## Running the tests
110+
93111
### Integration and unit tests
112+
94113
In order to run the tests (which are mostly integration tests), you must have access to a MongoDB deployment. You may specify a [MongoDB connection string](https://www.mongodb.com/docs/manual/reference/connection-string/) in the `MONGODB_URI` environment variable, and the tests will use it to connect to the deployment. If `MONGODB_URI` is unset, the tests will attempt to connect to a local deployment on port 27017.
95114

96115
**Note:** The integration tests will clear out the databases/collections they need to use, but they do not clean up after themselves.
97116

98117
To actually run the tests, you can use `cargo` like you would in any other crate:
118+
99119
```bash
100120
cargo test --verbose # runs against localhost:27017
101121
export MONGODB_URI="mongodb://localhost:123"
102122
cargo test --verbose # runs against localhost:123
103123
```
104124

105125
#### Auth tests
126+
106127
The authentication tests will only be included in the test run if certain requirements are met:
128+
107129
- The deployment must have `--auth` enabled
108130
- Credentials must be specified in `MONGODB_URI`
109131
- The credentials specified in `MONGODB_URI` must be valid and have root privileges on the deployment
132+
110133
```bash
111134
export MONGODB_URI="mongodb://user:pass@localhost:27017"
112135
cargo test --verbose # auth tests included
113136
```
137+
114138
#### Topology-specific tests
139+
115140
Certain tests will only be run against certain topologies. To ensure that the entire test suite is run, make sure to run the tests separately against standalone, replicated, and sharded deployments.
141+
116142
```bash
117143
export MONGODB_URI="mongodb://my-standalone-host:27017" # mongod running on 27017
118144
cargo test --verbose
@@ -123,28 +149,36 @@ cargo test --verbose
123149
```
124150

125151
#### Run the tests with TLS/SSL
152+
126153
To run the tests with TLS/SSL enabled, you must enable it on the deployment and in `MONGODB_URI`.
154+
127155
```bash
128156
export MONGODB_URI="mongodb://localhost:27017/?tls=true&tlsCertificateKeyFile=cert.pem&tlsCAFile=ca.pem"
129157
cargo test --verbose
130158
```
159+
131160
**Note:** When you open a pull request, your code will be run against a comprehensive testing matrix, so it is usually not necessary to run the integration tests against all combinations of topology/auth/TLS locally.
132161

133162
### Linter Tests
163+
134164
Our linter tests use the nightly version of `rustfmt` to verify that the source is formatted properly and the stable version of `clippy` to statically detect any common mistakes.
135165
You can use `rustup` to install them both:
166+
136167
```bash
137168
rustup component add clippy --toolchain stable
138169
rustup component add rustfmt --toolchain nightly
139170
```
171+
140172
Our linter tests also use `rustdoc` to verify that all necessary documentation is present and properly formatted. `rustdoc` is included in the standard Rust distribution.
141173

142174
To run the linter tests, run the `check-clippy.sh`, `check-rustfmt.sh`, and `check-rustdoc.sh` scripts in the `.evergreen` directory. To run all three, use the `check-all.sh` script.
175+
143176
```bash
144177
bash .evergreen/check-all.sh
145178
```
146179

147180
## Continuous Integration
181+
148182
Commits to main are run automatically on [evergreen](https://evergreen.mongodb.com/waterfall/mongo-rust-driver).
149183

150184
## Minimum supported Rust version (MSRV) policy

src/coll/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use serde_with::skip_serializing_none;
66
use typed_builder::TypedBuilder;
77

88
use crate::{
9-
bson::{doc, serde_helpers, Bson, Document, RawBson, RawDocumentBuf},
9+
bson::{doc, Bson, Document, RawBson, RawDocumentBuf},
1010
concern::{ReadConcern, WriteConcern},
1111
error::Result,
1212
options::Collation,
@@ -1186,7 +1186,7 @@ impl Serialize for CommitQuorum {
11861186
S: Serializer,
11871187
{
11881188
match self {
1189-
CommitQuorum::Nodes(n) => serde_helpers::serialize_u32_as_i32(n, serializer),
1189+
CommitQuorum::Nodes(n) => serde_util::serialize_u32_as_i32(n, serializer),
11901190
CommitQuorum::VotingMembers => serializer.serialize_str("votingMembers"),
11911191
CommitQuorum::Majority => serializer.serialize_str("majority"),
11921192
CommitQuorum::Custom(s) => serializer.serialize_str(s),

src/concern.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use serde_with::skip_serializing_none;
1010
use typed_builder::TypedBuilder;
1111

1212
use crate::{
13-
bson::{doc, serde_helpers, Timestamp},
13+
bson::{doc, Timestamp},
1414
error::{ErrorKind, Result},
1515
serde_util,
1616
};
@@ -244,7 +244,7 @@ impl Serialize for Acknowledgment {
244244
{
245245
match self {
246246
Acknowledgment::Majority => serializer.serialize_str("majority"),
247-
Acknowledgment::Nodes(n) => serde_helpers::serialize_u32_as_i32(n, serializer),
247+
Acknowledgment::Nodes(n) => serde_util::serialize_u32_as_i32(n, serializer),
248248
Acknowledgment::Custom(name) => serializer.serialize_str(name),
249249
}
250250
}

src/gridfs.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use crate::{
1414
checked::Checked,
1515
error::Error,
1616
options::{CollectionOptions, ReadConcern, SelectionCriteria, WriteConcern},
17+
serde_util,
1718
Collection,
1819
Database,
1920
};
@@ -31,7 +32,7 @@ pub(crate) struct Chunk<'a> {
3132
#[serde(rename = "_id")]
3233
id: ObjectId,
3334
files_id: Bson,
34-
#[serde(serialize_with = "crate::bson::serde_helpers::serialize_u32_as_i32")]
35+
#[serde(serialize_with = "serde_util::serialize_u32_as_i32")]
3536
n: u32,
3637
#[serde(borrow)]
3738
data: RawBinaryRef<'a>,
@@ -54,7 +55,7 @@ pub struct FilesCollectionDocument {
5455
/// The size of the file's chunks in bytes.
5556
#[serde(
5657
rename = "chunkSize",
57-
serialize_with = "crate::bson::serde_helpers::serialize_u32_as_i32"
58+
serialize_with = "serde_util::serialize_u32_as_i32"
5859
)]
5960
pub chunk_size_bytes: u32,
6061

src/index/options.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
use std::time::Duration;
22

3-
use crate::{
4-
bson::{serde_helpers, Document},
5-
collation::Collation,
6-
serde_util,
7-
};
3+
use crate::{bson::Document, collation::Collation, serde_util};
84

95
use serde::{Deserialize, Deserializer, Serialize, Serializer};
106
use typed_builder::TypedBuilder;
@@ -157,7 +153,7 @@ impl Serialize for IndexVersion {
157153
IndexVersion::V0 => serializer.serialize_i32(0),
158154
IndexVersion::V1 => serializer.serialize_i32(1),
159155
IndexVersion::V2 => serializer.serialize_i32(2),
160-
IndexVersion::Custom(i) => serde_helpers::serialize_u32_as_i32(i, serializer),
156+
IndexVersion::Custom(i) => serde_util::serialize_u32_as_i32(i, serializer),
161157
}
162158
}
163159
}
@@ -203,7 +199,7 @@ impl Serialize for TextIndexVersion {
203199
TextIndexVersion::V1 => serializer.serialize_i32(1),
204200
TextIndexVersion::V2 => serializer.serialize_i32(2),
205201
TextIndexVersion::V3 => serializer.serialize_i32(3),
206-
TextIndexVersion::Custom(i) => serde_helpers::serialize_u32_as_i32(i, serializer),
202+
TextIndexVersion::Custom(i) => serde_util::serialize_u32_as_i32(i, serializer),
207203
}
208204
}
209205
}
@@ -244,7 +240,7 @@ impl Serialize for Sphere2DIndexVersion {
244240
match self {
245241
Sphere2DIndexVersion::V2 => serializer.serialize_i32(2),
246242
Sphere2DIndexVersion::V3 => serializer.serialize_i32(3),
247-
Sphere2DIndexVersion::Custom(i) => serde_helpers::serialize_u32_as_i32(i, serializer),
243+
Sphere2DIndexVersion::Custom(i) => serde_util::serialize_u32_as_i32(i, serializer),
248244
}
249245
}
250246
}

src/results.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use serde::{Deserialize, Serialize};
88
use serde_with::skip_serializing_none;
99

1010
use crate::{
11-
bson::{serde_helpers, Binary, Bson, Document, RawDocumentBuf},
11+
bson::{Binary, Bson, Document, RawDocumentBuf},
1212
change_stream::event::ResumeToken,
1313
db::options::CreateCollectionOptions,
1414
serde_util,
@@ -53,11 +53,11 @@ pub struct InsertManyResult {
5353
#[non_exhaustive]
5454
pub struct UpdateResult {
5555
/// The number of documents that matched the filter.
56-
#[serde(serialize_with = "crate::bson::serde_helpers::serialize_u64_as_i64")]
56+
#[serde(serialize_with = "serde_util::serialize_u64_as_i64")]
5757
pub matched_count: u64,
5858

5959
/// The number of documents that were modified by the operation.
60-
#[serde(serialize_with = "crate::bson::serde_helpers::serialize_u64_as_i64")]
60+
#[serde(serialize_with = "serde_util::serialize_u64_as_i64")]
6161
pub modified_count: u64,
6262

6363
/// The `_id` field of the upserted document.
@@ -71,7 +71,7 @@ pub struct UpdateResult {
7171
#[non_exhaustive]
7272
pub struct DeleteResult {
7373
/// The number of documents deleted by the operation.
74-
#[serde(serialize_with = "crate::bson::serde_helpers::serialize_u64_as_i64")]
74+
#[serde(serialize_with = "serde_util::serialize_u64_as_i64")]
7575
pub deleted_count: u64,
7676
}
7777

@@ -182,7 +182,7 @@ pub struct DatabaseSpecification {
182182
/// The amount of disk space in bytes that is consumed by the database.
183183
#[serde(
184184
deserialize_with = "serde_util::deserialize_u64_from_bson_number",
185-
serialize_with = "serde_helpers::serialize_u64_as_i64"
185+
serialize_with = "serde_util::serialize_u64_as_i64"
186186
)]
187187
pub size_on_disk: u64,
188188

src/serde_util.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::{
77
bson_util::get_u64,
88
error::{Error, Result},
99
options::WriteConcern,
10+
serde_util,
1011
};
1112

1213
pub(crate) mod duration_option_as_int_seconds {
@@ -73,7 +74,7 @@ pub(crate) fn serialize_u32_option_as_i32<S: Serializer>(
7374
serializer: S,
7475
) -> std::result::Result<S::Ok, S::Error> {
7576
match val {
76-
Some(ref val) => crate::bson::serde_helpers::serialize_u32_as_i32(val, serializer),
77+
Some(ref val) => serde_util::serialize_u32_as_i32(val, serializer),
7778
None => serializer.serialize_none(),
7879
}
7980
}
@@ -101,7 +102,7 @@ pub(crate) fn serialize_u64_option_as_i64<S: Serializer>(
101102
serializer: S,
102103
) -> std::result::Result<S::Ok, S::Error> {
103104
match val {
104-
Some(ref v) => crate::bson::serde_helpers::serialize_u64_as_i64(v, serializer),
105+
Some(ref v) => serde_util::serialize_u64_as_i64(v, serializer),
105106
None => serializer.serialize_none(),
106107
}
107108
}
@@ -225,3 +226,29 @@ pub(crate) fn serialize_bool_or_true<S: Serializer>(
225226
let val = val.unwrap_or(true);
226227
serializer.serialize_bool(val)
227228
}
229+
230+
pub(crate) fn serialize_u32_as_i32<S: Serializer>(
231+
n: &u32,
232+
serializer: S,
233+
) -> std::result::Result<S::Ok, S::Error> {
234+
match i32::try_from(*n) {
235+
Ok(n) => n.serialize(serializer),
236+
Err(_) => Err(serde::ser::Error::custom(format!(
237+
"cannot serialize u32 {} as i32",
238+
n
239+
))),
240+
}
241+
}
242+
243+
pub(crate) fn serialize_u64_as_i64<S: Serializer>(
244+
n: &u64,
245+
serializer: S,
246+
) -> std::result::Result<S::Ok, S::Error> {
247+
match i64::try_from(*n) {
248+
Ok(n) => n.serialize(serializer),
249+
Err(_) => Err(serde::ser::Error::custom(format!(
250+
"cannot serialize u64 {} as i64",
251+
n
252+
))),
253+
}
254+
}

0 commit comments

Comments
 (0)