Skip to content

Commit 233200f

Browse files
authored
RUST-1406 Update driver to match errors (#1394)
1 parent 65ce423 commit 233200f

File tree

15 files changed

+91
-55
lines changed

15 files changed

+91
-55
lines changed

Cargo.lock

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

macros/src/option.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,7 @@ pub fn option_setters(
9292
});
9393
// Append setter fns to `impl` block item list
9494
for OptInfo { name, attrs, type_ } in opt_info {
95-
if args
96-
.skip
97-
.as_ref()
98-
.map_or(false, |skip| skip.contains(&name))
99-
{
95+
if args.skip.as_ref().is_some_and(|skip| skip.contains(&name)) {
10096
continue;
10197
}
10298
let (accept, value) = if type_.is_ident("String")

src/action/run_command.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use std::time::Duration;
33
use crate::bson::{Bson, Document, RawDocumentBuf};
44

55
use crate::{
6+
bson_compat::RawResult,
67
client::session::TransactionState,
78
coll::options::CursorType,
89
db::options::{RunCommandOptions, RunCursorCommandOptions},
@@ -154,7 +155,7 @@ impl crate::sync::Database {
154155
#[must_use]
155156
pub struct RunCommand<'a> {
156157
db: &'a Database,
157-
command: crate::bson::raw::Result<RawDocumentBuf>,
158+
command: RawResult<RawDocumentBuf>,
158159
options: Option<RunCommandOptions>,
159160
session: Option<&'a mut ClientSession>,
160161
}
@@ -214,7 +215,7 @@ impl<'a> Action for RunCommand<'a> {
214215
#[must_use]
215216
pub struct RunCursorCommand<'a, Session = ImplicitSession> {
216217
db: &'a Database,
217-
command: crate::bson::raw::Result<RawDocumentBuf>,
218+
command: RawResult<RawDocumentBuf>,
218219
options: Option<RunCursorCommandOptions>,
219220
session: Session,
220221
}

src/bson_compat.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[cfg(feature = "bson-3")]
12
pub(crate) trait RawDocumentBufExt {
23
fn append_ref<'a>(
34
&mut self,
@@ -6,6 +7,7 @@ pub(crate) trait RawDocumentBufExt {
67
);
78
}
89

10+
#[cfg(feature = "bson-3")]
911
impl RawDocumentBufExt for crate::bson::RawDocumentBuf {
1012
fn append_ref<'a>(
1113
&mut self,
@@ -15,3 +17,15 @@ impl RawDocumentBufExt for crate::bson::RawDocumentBuf {
1517
self.append(key, value)
1618
}
1719
}
20+
21+
#[cfg(feature = "bson-3")]
22+
pub(crate) use crate::bson::error::Result as RawResult;
23+
24+
#[cfg(not(feature = "bson-3"))]
25+
pub(crate) use crate::bson::raw::Result as RawResult;
26+
27+
#[cfg(feature = "bson-3")]
28+
pub(crate) use crate::bson::error::Error as RawError;
29+
30+
#[cfg(not(feature = "bson-3"))]
31+
pub(crate) use crate::bson::raw::Error as RawError;

src/change_stream/event.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ use std::convert::TryInto;
44

55
use crate::{cursor::CursorSpecification, options::ChangeStreamOptions};
66

7-
#[cfg(test)]
8-
use crate::bson::Bson;
97
use crate::bson::{DateTime, Document, RawBson, RawDocumentBuf, Timestamp};
8+
#[cfg(test)]
9+
use crate::{bson::Bson, bson_compat::RawError};
1010
use serde::{Deserialize, Serialize};
1111

1212
/// An opaque token used for resuming an interrupted
@@ -43,7 +43,7 @@ impl ResumeToken {
4343
}
4444

4545
#[cfg(test)]
46-
pub(crate) fn parsed(self) -> std::result::Result<Bson, crate::bson::raw::Error> {
46+
pub(crate) fn parsed(self) -> std::result::Result<Bson, RawError> {
4747
self.0.try_into()
4848
}
4949
}

src/client/auth/oidc.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -976,7 +976,11 @@ pub(super) fn validate_credential(credential: &Credential) -> Result<()> {
976976
)));
977977
}
978978
#[cfg(test)]
979-
if environment == Ok(TEST_ENVIRONMENT_VALUE_STR) && credential.username.is_some() {
979+
if environment
980+
.as_ref()
981+
.is_ok_and(|ev| *ev == TEST_ENVIRONMENT_VALUE_STR)
982+
&& credential.username.is_some()
983+
{
980984
return Err(Error::invalid_argument(format!(
981985
"username must not be set for {} authentication in the {} {}",
982986
MONGODB_OIDC_STR, TEST_ENVIRONMENT_VALUE_STR, ENVIRONMENT_PROP_STR,

src/cmap/establish/handshake/test.rs

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,15 @@ async fn metadata_no_options() {
3232
.collect::<Vec<_>>(),
3333
vec!["name", "version"]
3434
);
35-
assert_eq!(driver.get_str("name"), Ok("mongo-rust-driver"));
36-
assert_eq!(driver.get_str("version"), Ok(env!("CARGO_PKG_VERSION")));
35+
assert_eq!(driver.get_str("name").unwrap(), "mongo-rust-driver");
36+
assert_eq!(
37+
driver.get_str("version").unwrap(),
38+
env!("CARGO_PKG_VERSION")
39+
);
3740

3841
let os = metadata.get_document("os").unwrap();
39-
assert_eq!(os.get_str("type"), Ok(std::env::consts::OS));
40-
assert_eq!(os.get_str("architecture"), Ok(std::env::consts::ARCH));
42+
assert_eq!(os.get_str("type").unwrap(), std::env::consts::OS);
43+
assert_eq!(os.get_str("architecture").unwrap(), std::env::consts::ARCH);
4144
}
4245

4346
#[tokio::test]
@@ -68,8 +71,8 @@ async fn metadata_with_options() {
6871
let command = handshaker.build_command(None).await.unwrap().0;
6972
let metadata = command.body.get_document("client").unwrap();
7073
assert_eq!(
71-
metadata.get_document("application"),
72-
Ok(rawdoc! { "name": app_name }.deref())
74+
metadata.get_document("application").unwrap(),
75+
rawdoc! { "name": app_name }.deref()
7376
);
7477

7578
let driver = metadata.get_document("driver").unwrap();
@@ -81,15 +84,15 @@ async fn metadata_with_options() {
8184
vec!["name", "version"]
8285
);
8386
assert_eq!(
84-
driver.get_str("name"),
85-
Ok(format!("mongo-rust-driver|{}", name).as_str())
87+
driver.get_str("name").unwrap(),
88+
format!("mongo-rust-driver|{}", name).as_str()
8689
);
8790
assert_eq!(
88-
driver.get_str("version"),
89-
Ok(format!("{}|{}", env!("CARGO_PKG_VERSION"), version).as_str())
91+
driver.get_str("version").unwrap(),
92+
format!("{}|{}", env!("CARGO_PKG_VERSION"), version).as_str()
9093
);
9194

9295
let os = metadata.get_document("os").unwrap();
93-
assert_eq!(os.get_str("type"), Ok(std::env::consts::OS));
94-
assert_eq!(os.get_str("architecture"), Ok(std::env::consts::ARCH));
96+
assert_eq!(os.get_str("type").unwrap(), std::env::consts::OS);
97+
assert_eq!(os.get_str("architecture").unwrap(), std::env::consts::ARCH);
9598
}

src/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -560,8 +560,8 @@ impl From<crate::bson::ser::Error> for ErrorKind {
560560
}
561561
}
562562

563-
impl From<crate::bson::raw::Error> for ErrorKind {
564-
fn from(err: crate::bson::raw::Error) -> Self {
563+
impl From<crate::bson_compat::RawError> for ErrorKind {
564+
fn from(err: crate::bson_compat::RawError) -> Self {
565565
Self::InvalidResponse {
566566
message: err.to_string(),
567567
}

src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ pub mod options;
2121
pub use ::mongocrypt;
2222

2323
pub mod action;
24-
#[cfg(feature = "bson-3")]
2524
pub(crate) mod bson_compat;
2625
mod bson_util;
2726
pub mod change_stream;

src/sdam/test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ async fn hello_ok_true() {
192192
event_stream
193193
.next_match(Duration::from_millis(2000), |event| {
194194
if let Event::Sdam(SdamEvent::ServerHeartbeatSucceeded(e)) = event {
195-
assert_eq!(e.reply.get_bool("helloOk"), Ok(true));
195+
assert!(e.reply.get_bool("helloOk").unwrap());
196196
assert!(e.reply.get(LEGACY_HELLO_COMMAND_NAME_LOWERCASE).is_some());
197197
assert!(e.reply.get("isWritablePrimary").is_none());
198198
return true;

src/test/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -980,5 +980,5 @@ async fn ipv6_connect() {
980980
.run_command(doc! { "ping": 1 })
981981
.await
982982
.unwrap();
983-
assert_eq!(result.get_f64("ok"), Ok(1.0));
983+
assert_eq!(result.get_f64("ok").unwrap(), 1.0);
984984
}

src/test/csfle/prose.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1750,8 +1750,8 @@ mod range_explicit_encryption {
17501750
assert_eq!(actual.len(), expected.len());
17511751
for (idx, num) in expected.iter().enumerate() {
17521752
assert_eq!(
1753-
actual[idx].get(&key),
1754-
Ok(Some(bson_numbers[num].as_raw_bson_ref()))
1753+
actual[idx].get(&key).unwrap(),
1754+
Some(bson_numbers[num].as_raw_bson_ref())
17551755
);
17561756
}
17571757
};

src/test/db.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ async fn test_run_command() {
451451
.unwrap();
452452
let v: Vec<Result<Document>> = cursor.collect().await;
453453
assert_eq!(v.len(), 1);
454-
assert_eq!(v[0].as_ref().unwrap().get_str("foo"), Ok("bar"));
454+
assert_eq!(v[0].as_ref().unwrap().get_str("foo").unwrap(), "bar");
455455
}
456456

457457
// Test run_raw_cursor_command
@@ -463,6 +463,6 @@ async fn test_run_command() {
463463
let cursor = database.run_raw_cursor_command(cmd).await.unwrap();
464464
let v: Vec<Result<Document>> = cursor.collect().await;
465465
assert_eq!(v.len(), 1);
466-
assert_eq!(v[0].as_ref().unwrap().get_str("foo"), Ok("bar"));
466+
assert_eq!(v[0].as_ref().unwrap().get_str("foo").unwrap(), "bar");
467467
}
468468
}

src/test/index_management/search_index.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ async fn search_index_create_list() {
3636
let found = 'outer: loop {
3737
let mut cursor = coll0.list_search_indexes().await.unwrap();
3838
while let Some(d) = cursor.try_next().await.unwrap() {
39-
if d.get_str("name") == Ok("test-search-index") && d.get_bool("queryable") == Ok(true) {
39+
if d.get_str("name").is_ok_and(|n| n == "test-search-index")
40+
&& d.get_bool("queryable").unwrap_or(false)
41+
{
4042
break 'outer d;
4143
}
4244
}
@@ -47,8 +49,8 @@ async fn search_index_create_list() {
4749
};
4850

4951
assert_eq!(
50-
found.get_document("latestDefinition"),
51-
Ok(&doc! { "mappings": { "dynamic": false } })
52+
found.get_document("latestDefinition").unwrap(),
53+
&doc! { "mappings": { "dynamic": false } }
5254
);
5355
}
5456

@@ -84,11 +86,12 @@ async fn search_index_create_multiple() {
8486
loop {
8587
let mut cursor = coll0.list_search_indexes().await.unwrap();
8688
while let Some(d) = cursor.try_next().await.unwrap() {
87-
if d.get_str("name") == Ok("test-search-index-1") && d.get_bool("queryable") == Ok(true)
89+
if d.get_str("name").is_ok_and(|n| n == "test-search-index-1")
90+
&& d.get_bool("queryable").unwrap_or(false)
8891
{
8992
index1 = Some(d);
90-
} else if d.get_str("name") == Ok("test-search-index-2")
91-
&& d.get_bool("queryable") == Ok(true)
93+
} else if d.get_str("name").is_ok_and(|n| n == "test-search-index-2")
94+
&& d.get_bool("queryable").unwrap_or(false)
9295
{
9396
index2 = Some(d);
9497
}
@@ -103,12 +106,12 @@ async fn search_index_create_multiple() {
103106
}
104107

105108
assert_eq!(
106-
index1.unwrap().get_document("latestDefinition"),
107-
Ok(&doc! { "mappings": { "dynamic": false } })
109+
index1.unwrap().get_document("latestDefinition").unwrap(),
110+
&doc! { "mappings": { "dynamic": false } }
108111
);
109112
assert_eq!(
110-
index2.unwrap().get_document("latestDefinition"),
111-
Ok(&doc! { "mappings": { "dynamic": false } })
113+
index2.unwrap().get_document("latestDefinition").unwrap(),
114+
&doc! { "mappings": { "dynamic": false } }
112115
);
113116
}
114117

@@ -138,7 +141,9 @@ async fn search_index_drop() {
138141
'outer: loop {
139142
let mut cursor = coll0.list_search_indexes().await.unwrap();
140143
while let Some(d) = cursor.try_next().await.unwrap() {
141-
if d.get_str("name") == Ok("test-search-index") && d.get_bool("queryable") == Ok(true) {
144+
if d.get_str("name").is_ok_and(|n| n == "test-search-index")
145+
&& d.get_bool("queryable").unwrap_or(false)
146+
{
142147
break 'outer;
143148
}
144149
}
@@ -188,7 +193,9 @@ async fn search_index_update() {
188193
'outer: loop {
189194
let mut cursor = coll0.list_search_indexes().await.unwrap();
190195
while let Some(d) = cursor.try_next().await.unwrap() {
191-
if d.get_str("name") == Ok("test-search-index") && d.get_bool("queryable") == Ok(true) {
196+
if d.get_str("name").is_ok_and(|n| n == "test-search-index")
197+
&& d.get_bool("queryable").unwrap_or(false)
198+
{
192199
break 'outer;
193200
}
194201
}
@@ -209,9 +216,9 @@ async fn search_index_update() {
209216
let found = 'find: loop {
210217
let mut cursor = coll0.list_search_indexes().await.unwrap();
211218
while let Some(d) = cursor.try_next().await.unwrap() {
212-
if d.get_str("name") == Ok("test-search-index")
213-
&& d.get_bool("queryable") == Ok(true)
214-
&& d.get_str("status") == Ok("READY")
219+
if d.get_str("name").is_ok_and(|n| n == "test-search-index")
220+
&& d.get_bool("queryable").unwrap_or(false)
221+
&& d.get_str("status").is_ok_and(|s| s == "READY")
215222
{
216223
break 'find d;
217224
}
@@ -223,8 +230,8 @@ async fn search_index_update() {
223230
};
224231

225232
assert_eq!(
226-
found.get_document("latestDefinition"),
227-
Ok(&doc! { "mappings": { "dynamic": true } })
233+
found.get_document("latestDefinition").unwrap(),
234+
&doc! { "mappings": { "dynamic": true } }
228235
);
229236
}
230237

@@ -245,7 +252,9 @@ async fn wait_for_index(coll: &Collection<Document>, name: &str) -> Document {
245252
while Instant::now() < deadline {
246253
let mut cursor = coll.list_search_indexes().name(name).await.unwrap();
247254
while let Some(def) = cursor.try_next().await.unwrap() {
248-
if def.get_str("name") == Ok(name) && def.get_bool("queryable") == Ok(true) {
255+
if def.get_str("name").is_ok_and(|n| n == name)
256+
&& def.get_bool("queryable").unwrap_or(false)
257+
{
249258
return def;
250259
}
251260
}
@@ -274,7 +283,7 @@ async fn search_index_create_with_type() {
274283
.unwrap();
275284
assert_eq!(name, "test-search-index-case7-implicit");
276285
let index1 = wait_for_index(&coll0, &name).await;
277-
assert_eq!(index1.get_str("type"), Ok("search"));
286+
assert_eq!(index1.get_str("type").unwrap(), "search");
278287

279288
let name = coll0
280289
.create_search_index(
@@ -288,7 +297,7 @@ async fn search_index_create_with_type() {
288297
.unwrap();
289298
assert_eq!(name, "test-search-index-case7-explicit");
290299
let index2 = wait_for_index(&coll0, &name).await;
291-
assert_eq!(index2.get_str("type"), Ok("search"));
300+
assert_eq!(index2.get_str("type").unwrap(), "search");
292301

293302
let name = coll0
294303
.create_search_index(
@@ -309,7 +318,7 @@ async fn search_index_create_with_type() {
309318
.unwrap();
310319
assert_eq!(name, "test-search-index-case7-vector");
311320
let index3 = wait_for_index(&coll0, &name).await;
312-
assert_eq!(index3.get_str("type"), Ok("vectorSearch"));
321+
assert_eq!(index3.get_str("type").unwrap(), "vectorSearch");
313322
}
314323

315324
// SearchIndex Case 8: Driver requires explicit type to create a vector search index

src/test/spec/retryable_writes.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,9 @@ async fn retry_write_retryable_write_error() {
246246
while let Some(msg) = event_rx.recv().await {
247247
if let CommandEvent::Succeeded(ev) = &*msg {
248248
if let Some(Bson::Document(wc_err)) = ev.reply.get("writeConcernError") {
249-
if ev.command_name == "insert" && wc_err.get_i32("code") == Ok(91) {
249+
if ev.command_name == "insert"
250+
&& wc_err.get_i32("code").is_ok_and(|c| c == 91)
251+
{
250252
// Spawn a new task so events continue to process
251253
let client = client.clone();
252254
let fp_tx = fp_tx.clone();

0 commit comments

Comments
 (0)