Skip to content

Commit 4edd266

Browse files
authored
RUST-261 Mark enums and options structs as non-exhaustible (#178)
1 parent dfd951f commit 4edd266

File tree

18 files changed

+222
-202
lines changed

18 files changed

+222
-202
lines changed

src/client/options/mod.rs

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ impl fmt::Display for StreamAddress {
193193
/// Contains the options that can be used to create a new [`Client`](../struct.Client.html).
194194
#[derive(Clone, Derivative, TypedBuilder)]
195195
#[derivative(Debug, PartialEq)]
196+
#[non_exhaustive]
196197
pub struct ClientOptions {
197198
/// The initial list of seeds that the Client should connect to.
198199
///
@@ -416,6 +417,7 @@ impl From<TlsOptions> for Option<Tls> {
416417

417418
/// Specifies the TLS configuration that the [`Client`](../struct.Client.html) should use.
418419
#[derive(Clone, Debug, Default, PartialEq, TypedBuilder)]
420+
#[non_exhaustive]
419421
pub struct TlsOptions {
420422
/// Whether or not the [`Client`](../struct.Client.html) should return an error if the server
421423
/// presents an invalid certificate. This setting should _not_ be set to `true` in
@@ -513,6 +515,7 @@ impl TlsOptions {
513515
/// Extra information to append to the driver version in the metadata of the handshake with the
514516
/// server. This should be used by libraries wrapping the driver, e.g. ODMs.
515517
#[derive(Clone, Debug, TypedBuilder, PartialEq)]
518+
#[non_exhaustive]
516519
pub struct DriverInfo {
517520
/// The name of the library wrapping the driver.
518521
pub name: String,
@@ -1238,20 +1241,16 @@ impl ClientOptionsParser {
12381241
self.read_preference = Some(match &value.to_lowercase()[..] {
12391242
"primary" => ReadPreference::Primary,
12401243
"secondary" => ReadPreference::Secondary {
1241-
tag_sets: None,
1242-
max_staleness: None,
1244+
options: Default::default(),
12431245
},
12441246
"primarypreferred" => ReadPreference::PrimaryPreferred {
1245-
tag_sets: None,
1246-
max_staleness: None,
1247+
options: Default::default(),
12471248
},
12481249
"secondarypreferred" => ReadPreference::SecondaryPreferred {
1249-
tag_sets: None,
1250-
max_staleness: None,
1250+
options: Default::default(),
12511251
},
12521252
"nearest" => ReadPreference::Nearest {
1253-
tag_sets: None,
1254-
max_staleness: None,
1253+
options: Default::default(),
12551254
},
12561255
other => {
12571256
return Err(ErrorKind::ArgumentError {
@@ -1484,7 +1483,7 @@ mod tests {
14841483
use super::{ClientOptions, StreamAddress};
14851484
use crate::{
14861485
concern::{Acknowledgment, ReadConcernLevel, WriteConcern},
1487-
selection_criteria::ReadPreference,
1486+
selection_criteria::{ReadPreference, ReadPreferenceOptions},
14881487
};
14891488

14901489
macro_rules! tag_set {
@@ -1807,17 +1806,18 @@ mod tests {
18071806
],
18081807
selection_criteria: Some(
18091808
ReadPreference::SecondaryPreferred {
1810-
tag_sets: Some(vec![
1811-
tag_set! {
1812-
"dc" => "ny",
1813-
"rack" => "1"
1814-
},
1815-
tag_set! {
1816-
"dc" => "ny"
1817-
},
1818-
tag_set! {},
1819-
]),
1820-
max_staleness: None,
1809+
options: ReadPreferenceOptions::builder()
1810+
.tag_sets(vec![
1811+
tag_set! {
1812+
"dc" => "ny",
1813+
"rack" => "1"
1814+
},
1815+
tag_set! {
1816+
"dc" => "ny"
1817+
},
1818+
tag_set! {},
1819+
])
1820+
.build()
18211821
}
18221822
.into()
18231823
),

src/client/options/test.rs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,20 @@ fn document_from_client_options(mut options: ClientOptions) -> Document {
8686
if let Some(SelectionCriteria::ReadPreference(read_pref)) = options.selection_criteria.take() {
8787
let (level, tag_sets, max_staleness) = match read_pref {
8888
ReadPreference::Primary => ("primary", None, None),
89-
ReadPreference::PrimaryPreferred {
90-
tag_sets,
91-
max_staleness,
92-
} => ("primaryPreferred", tag_sets, max_staleness),
93-
ReadPreference::Secondary {
94-
tag_sets,
95-
max_staleness,
96-
} => ("secondary", tag_sets, max_staleness),
97-
ReadPreference::SecondaryPreferred {
98-
tag_sets,
99-
max_staleness,
100-
} => ("secondaryPreferred", tag_sets, max_staleness),
101-
ReadPreference::Nearest {
102-
tag_sets,
103-
max_staleness,
104-
} => ("nearest", tag_sets, max_staleness),
89+
ReadPreference::PrimaryPreferred { options } => {
90+
("primaryPreferred", options.tag_sets, options.max_staleness)
91+
}
92+
ReadPreference::Secondary { options } => {
93+
("secondary", options.tag_sets, options.max_staleness)
94+
}
95+
ReadPreference::SecondaryPreferred { options } => (
96+
"secondaryPreferred",
97+
options.tag_sets,
98+
options.max_staleness,
99+
),
100+
ReadPreference::Nearest { options } => {
101+
("nearest", options.tag_sets, options.max_staleness)
102+
}
105103
};
106104

107105
doc.insert("readpreference", level);

src/client/session/test.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -465,20 +465,16 @@ async fn find_and_getmore_share_session() {
465465
let read_preferences: Vec<ReadPreference> = vec![
466466
ReadPreference::Primary,
467467
ReadPreference::PrimaryPreferred {
468-
tag_sets: None,
469-
max_staleness: None,
468+
options: Default::default(),
470469
},
471470
ReadPreference::Secondary {
472-
tag_sets: None,
473-
max_staleness: None,
471+
options: Default::default(),
474472
},
475473
ReadPreference::SecondaryPreferred {
476-
tag_sets: None,
477-
max_staleness: None,
474+
options: Default::default(),
478475
},
479476
ReadPreference::Nearest {
480-
tag_sets: None,
481-
max_staleness: None,
477+
options: Default::default(),
482478
},
483479
];
484480

src/cmap/test/integration.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ async fn acquire_connection_and_send_command() {
3030

3131
let body = doc! { "listDatabases": 1 };
3232
let read_pref = ReadPreference::PrimaryPreferred {
33-
tag_sets: None,
34-
max_staleness: None,
33+
options: Default::default(),
3534
};
3635
let cmd = Command::new_read(
3736
"listDatabases".to_string(),

src/coll/options.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ use crate::{
1515
/// These are the valid options for creating a [`Collection`](../struct.Collection.html) with
1616
/// [`Database::collection_with_options`](../struct.Database.html#method.collection_with_options).
1717
#[derive(Debug, Default, TypedBuilder)]
18+
#[non_exhaustive]
1819
pub struct CollectionOptions {
1920
/// The default read preference for operations.
2021
#[builder(default)]
@@ -34,6 +35,7 @@ pub struct CollectionOptions {
3435
/// [`Collection::find_one_and_update`](../struct.Collection.html#method.find_one_and_update)
3536
/// operation should return the document before or after modification.
3637
#[derive(Debug)]
38+
#[non_exhaustive]
3739
pub enum ReturnDocument {
3840
/// Return the document after modification.
3941
After,
@@ -44,6 +46,7 @@ pub enum ReturnDocument {
4446
/// Specifies the index to use for an operation.
4547
#[derive(Clone, Debug, Deserialize, Serialize)]
4648
#[serde(untagged)]
49+
#[non_exhaustive]
4750
pub enum Hint {
4851
/// Specifies the keys of the index to use.
4952
Keys(Document),
@@ -62,6 +65,7 @@ impl Hint {
6265

6366
/// Specifies the type of cursor to return from a find operation.
6467
#[derive(Debug, Clone, Copy)]
68+
#[non_exhaustive]
6569
pub enum CursorType {
6670
/// Default; close the cursor after the last document is received from the server.
6771
NonTailable,
@@ -78,6 +82,7 @@ pub enum CursorType {
7882
/// Specifies the options to a
7983
/// [`Collection::insert_one`](../struct.Collection.html#method.insert_one) operation.
8084
#[derive(Clone, Debug, Default, TypedBuilder)]
85+
#[non_exhaustive]
8186
pub struct InsertOneOptions {
8287
/// Opt out of document-level validation.
8388
#[builder(default)]
@@ -93,6 +98,7 @@ pub struct InsertOneOptions {
9398
#[skip_serializing_none]
9499
#[derive(Clone, Debug, Default, TypedBuilder, Serialize, Deserialize)]
95100
#[serde(rename_all = "camelCase")]
101+
#[non_exhaustive]
96102
pub struct InsertManyOptions {
97103
/// Opt out of document-level validation.
98104
#[builder(default)]
@@ -126,6 +132,7 @@ impl InsertManyOptions {
126132
/// [documentation](https://docs.mongodb.com/manual/reference/command/update/#update-command-behaviors)
127133
#[derive(Clone, Debug, Serialize)]
128134
#[serde(untagged)]
135+
#[non_exhaustive]
129136
pub enum UpdateModifications {
130137
/// A document that contains only update operator expressions.
131138
Document(Document),
@@ -162,6 +169,7 @@ impl From<Vec<Document>> for UpdateModifications {
162169
/// [`Collection::update_one`](../struct.Collection.html#method.update_one) or
163170
/// [`Collection::update_many`](../struct.Collection.html#method.update_many) operation.
164171
#[derive(Debug, Default, TypedBuilder)]
172+
#[non_exhaustive]
165173
pub struct UpdateOptions {
166174
/// A set of filters specifying to which array elements an update should apply.
167175
///
@@ -213,6 +221,7 @@ impl UpdateOptions {
213221
/// Specifies the options to a
214222
/// [`Collection::replace_one`](../struct.Collection.html#method.replace_one) operation.
215223
#[derive(Debug, Default, TypedBuilder)]
224+
#[non_exhaustive]
216225
pub struct ReplaceOptions {
217226
/// Opt out of document-level validation.
218227
#[builder(default)]
@@ -247,6 +256,7 @@ pub struct ReplaceOptions {
247256
#[serde_with::skip_serializing_none]
248257
#[derive(Debug, Default, TypedBuilder, Serialize)]
249258
#[serde(rename_all = "camelCase")]
259+
#[non_exhaustive]
250260
pub struct DeleteOptions {
251261
/// The collation to use for the operation.
252262
///
@@ -264,6 +274,7 @@ pub struct DeleteOptions {
264274
/// [`Collection::find_one_and_delete`](../struct.Collection.html#method.find_one_and_delete)
265275
/// operation.
266276
#[derive(Debug, Default, TypedBuilder)]
277+
#[non_exhaustive]
267278
pub struct FindOneAndDeleteOptions {
268279
/// The maximum amount of time to allow the query to run.
269280
///
@@ -296,6 +307,7 @@ pub struct FindOneAndDeleteOptions {
296307
/// [`Collection::find_one_and_replace`](../struct.Collection.html#method.find_one_and_replace)
297308
/// operation.
298309
#[derive(Debug, Default, TypedBuilder)]
310+
#[non_exhaustive]
299311
pub struct FindOneAndReplaceOptions {
300312
/// Opt out of document-level validation.
301313
#[builder(default)]
@@ -340,6 +352,7 @@ pub struct FindOneAndReplaceOptions {
340352
/// [`Collection::find_one_and_update`](../struct.Collection.html#method.find_one_and_update)
341353
/// operation.
342354
#[derive(Debug, Default, TypedBuilder)]
355+
#[non_exhaustive]
343356
pub struct FindOneAndUpdateOptions {
344357
/// A set of filters specifying to which array elements an update should apply.
345358
///
@@ -392,6 +405,7 @@ pub struct FindOneAndUpdateOptions {
392405
#[skip_serializing_none]
393406
#[serde(rename_all = "camelCase")]
394407
#[derive(Clone, Debug, Default, TypedBuilder, Serialize)]
408+
#[non_exhaustive]
395409
pub struct AggregateOptions {
396410
/// Enables writing to temporary files. When set to true, aggregation stages can write data to
397411
/// the _tmp subdirectory in the dbPath directory.
@@ -473,6 +487,7 @@ pub struct AggregateOptions {
473487
/// Specifies the options to a
474488
/// [`Collection::count_documents`](../struct.Collection.html#method.count_documents) operation.
475489
#[derive(Debug, Default, TypedBuilder)]
490+
#[non_exhaustive]
476491
pub struct CountOptions {
477492
/// The index to use for the operation.
478493
#[builder(default)]
@@ -511,6 +526,7 @@ pub struct CountOptions {
511526
#[serde_with::skip_serializing_none]
512527
#[derive(Debug, Default, TypedBuilder, Serialize, Clone)]
513528
#[serde(rename_all = "camelCase")]
529+
#[non_exhaustive]
514530
pub struct EstimatedDocumentCountOptions {
515531
/// The maximum amount of time to allow the query to run.
516532
///
@@ -540,6 +556,7 @@ pub struct EstimatedDocumentCountOptions {
540556
#[serde_with::skip_serializing_none]
541557
#[derive(Debug, Default, TypedBuilder, Serialize, Clone)]
542558
#[serde(rename_all = "camelCase")]
559+
#[non_exhaustive]
543560
pub struct DistinctOptions {
544561
/// The maximum amount of time to allow the query to run.
545562
///
@@ -576,6 +593,7 @@ pub struct DistinctOptions {
576593
#[skip_serializing_none]
577594
#[derive(Debug, Default, TypedBuilder, Serialize)]
578595
#[serde(rename_all = "camelCase")]
596+
#[non_exhaustive]
579597
pub struct FindOptions {
580598
/// Enables writing to temporary files by the server. When set to true, the find operation can
581599
/// write data to the _tmp subdirectory in the dbPath directory. Only supported in server
@@ -741,6 +759,7 @@ where
741759
/// Specifies the options to a [`Collection::find_one`](../struct.Collection.html#method.find_one)
742760
/// operation.
743761
#[derive(Debug, Default, TypedBuilder)]
762+
#[non_exhaustive]
744763
pub struct FindOneOptions {
745764
/// If true, partial results will be returned from a mongos rather than an error being
746765
/// returned if one or more shards is down.
@@ -820,6 +839,7 @@ pub struct FindOneOptions {
820839

821840
/// Specifies an index to create.
822841
#[derive(Debug, TypedBuilder)]
842+
#[non_exhaustive]
823843
pub struct IndexModel {
824844
/// The fields to index, along with their sort order.
825845
pub keys: Document,
@@ -833,6 +853,7 @@ pub struct IndexModel {
833853
/// operation.
834854
#[derive(Debug, Default, TypedBuilder, Serialize)]
835855
#[serde(rename_all = "camelCase")]
856+
#[non_exhaustive]
836857
pub struct DropCollectionOptions {
837858
/// The write concern for the operation.
838859
#[builder(default)]

src/concern/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ impl Serialize for ReadConcernLevel {
128128
/// information about write concerns.
129129
#[skip_serializing_none]
130130
#[derive(Clone, Debug, Default, PartialEq, TypedBuilder, Serialize, Deserialize)]
131+
#[non_exhaustive]
131132
pub struct WriteConcern {
132133
/// Requests acknowledgement that the operation has propagated to a specific number or variety
133134
/// of servers.
@@ -155,6 +156,7 @@ pub struct WriteConcern {
155156

156157
/// The type of the `w` field in a [`WriteConcern`](struct.WriteConcern.html).
157158
#[derive(Clone, Debug, PartialEq)]
159+
#[non_exhaustive]
158160
pub enum Acknowledgment {
159161
/// Requires acknowledgement that the write has reached the specified number of nodes.
160162
///

src/db/options.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use crate::{
1313
/// These are the valid options for creating a [`Database`](../struct.Database.html) with
1414
/// [`Client::database_with_options`](../struct.Client.html#method.database_with_options).
1515
#[derive(Clone, Debug, Default, TypedBuilder)]
16+
#[non_exhaustive]
1617
pub struct DatabaseOptions {
1718
/// The default read preference for operations.
1819
#[builder(default)]
@@ -32,6 +33,7 @@ pub struct DatabaseOptions {
3233
#[skip_serializing_none]
3334
#[derive(Debug, Default, TypedBuilder, Serialize)]
3435
#[serde(rename_all = "camelCase")]
36+
#[non_exhaustive]
3537
pub struct CreateCollectionOptions {
3638
/// Whether the collection should be capped. If true, `size` must also be set.
3739
#[builder(default)]
@@ -94,6 +96,7 @@ pub struct CreateCollectionOptions {
9496
/// an update.
9597
#[derive(Debug, Serialize)]
9698
#[serde(rename_all = "camelCase")]
99+
#[non_exhaustive]
97100
pub enum ValidationLevel {
98101
/// Perform no validation for inserts and updates.
99102
Off,
@@ -108,6 +111,7 @@ pub enum ValidationLevel {
108111
/// documents do not pass the validation.
109112
#[derive(Debug, Serialize)]
110113
#[serde(rename_all = "camelCase")]
114+
#[non_exhaustive]
111115
pub enum ValidationAction {
112116
Error,
113117
Warn,
@@ -116,6 +120,7 @@ pub enum ValidationAction {
116120
/// Specifies the options to a [`Database::drop`](../struct.Database.html#method.drop) operation.
117121
#[derive(Debug, Default, TypedBuilder, Serialize)]
118122
#[serde(rename_all = "camelCase")]
123+
#[non_exhaustive]
119124
pub struct DropDatabaseOptions {
120125
/// The write concern for the operation.
121126
#[builder(default)]
@@ -125,6 +130,7 @@ pub struct DropDatabaseOptions {
125130
/// Specifies the options to a
126131
/// [`Database::list_collections`](../struct.Database.html#method.list_collections) operation.
127132
#[derive(Debug, Default, TypedBuilder, Serialize)]
133+
#[non_exhaustive]
128134
pub struct ListCollectionsOptions {
129135
/// The number of documents the server should return per cursor batch.
130136
///
@@ -141,6 +147,7 @@ pub struct ListCollectionsOptions {
141147
/// [`Client::list_databases`](../struct.Client.html#method.list_databases) operation.
142148
#[derive(Debug, Default, TypedBuilder, Serialize)]
143149
#[serde(rename_all = "camelCase")]
150+
#[non_exhaustive]
144151
pub struct ListDatabasesOptions {
145152
/// Determines which databases to return based on the user's access privileges. This option is
146153
/// only supported on server versions 4.0.5+.

0 commit comments

Comments
 (0)