Skip to content

Commit 008e368

Browse files
RUST-858 Use into field setter for typed-builder (#368)
1 parent 3f1165c commit 008e368

File tree

16 files changed

+110
-97
lines changed

16 files changed

+110
-97
lines changed

src/client/auth/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ impl FromStr for AuthMechanism {
325325
/// Some fields (mechanism and source) may be omitted and will either be negotiated or assigned a
326326
/// default value, depending on the values of other fields in the credential.
327327
#[derive(Clone, Default, Deserialize, TypedBuilder, PartialEq)]
328-
#[builder(field_defaults(default, setter(strip_option)))]
328+
#[builder(field_defaults(default, setter(into)))]
329329
#[non_exhaustive]
330330
pub struct Credential {
331331
/// The username to authenticate with. This applies to all mechanisms but may be omitted when

src/client/options/mod.rs

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ impl<'de> Deserialize<'de> for ServerApiVersion {
334334

335335
/// Options used to declare a versioned server API.
336336
#[derive(Clone, Debug, Deserialize, PartialEq, TypedBuilder)]
337+
#[builder(field_defaults(setter(into)))]
337338
#[serde(rename_all = "camelCase")]
338339
#[non_exhaustive]
339340
pub(crate) struct ServerApi {
@@ -342,18 +343,19 @@ pub(crate) struct ServerApi {
342343

343344
/// Whether the MongoDB server should reject all commands that are not part of the
344345
/// declared API version. This includes command options and aggregation pipeline stages.
345-
#[builder(default, setter(strip_option))]
346+
#[builder(default)]
346347
pub strict: Option<bool>,
347348

348349
/// Whether the MongoDB server should return command failures when functionality that is
349350
/// deprecated from the declared API version is used.
350351
/// Note that at the time of this writing, no deprecations in version 1 exist.
351-
#[builder(default, setter(strip_option))]
352+
#[builder(default)]
352353
pub deprecation_errors: Option<bool>,
353354
}
354355

355356
/// Contains the options that can be used to create a new [`Client`](../struct.Client.html).
356357
#[derive(Clone, Derivative, Deserialize, TypedBuilder)]
358+
#[builder(field_defaults(setter(into)))]
357359
#[derivative(Debug, PartialEq)]
358360
#[serde(rename_all = "camelCase")]
359361
#[non_exhaustive]
@@ -373,7 +375,7 @@ pub struct ClientOptions {
373375
/// The application name that the Client will send to the server as part of the handshake. This
374376
/// can be used in combination with the server logs to determine which Client is connected to a
375377
/// server.
376-
#[builder(default, setter(strip_option))]
378+
#[builder(default)]
377379
pub app_name: Option<String>,
378380

379381
#[builder(default, setter(skip))]
@@ -382,45 +384,45 @@ pub struct ClientOptions {
382384
/// The handler that should process all Connection Monitoring and Pooling events. See the
383385
/// CmapEventHandler type documentation for more details.
384386
#[derivative(Debug = "ignore", PartialEq = "ignore")]
385-
#[builder(default, setter(strip_option))]
387+
#[builder(default)]
386388
#[serde(skip)]
387389
pub cmap_event_handler: Option<Arc<dyn CmapEventHandler>>,
388390

389391
/// The handler that should process all command-related events. See the CommandEventHandler
390392
/// type documentation for more details.
391393
#[derivative(Debug = "ignore", PartialEq = "ignore")]
392-
#[builder(default, setter(strip_option))]
394+
#[builder(default)]
393395
#[serde(skip)]
394396
pub command_event_handler: Option<Arc<dyn CommandEventHandler>>,
395397

396398
/// The connect timeout passed to each underlying TcpStream when attemtping to connect to the
397399
/// server.
398400
///
399401
/// The default value is 10 seconds.
400-
#[builder(default, setter(strip_option))]
402+
#[builder(default)]
401403
pub connect_timeout: Option<Duration>,
402404

403405
/// The credential to use for authenticating connections made by this client.
404-
#[builder(default, setter(strip_option))]
406+
#[builder(default)]
405407
pub credential: Option<Credential>,
406408

407409
/// Specifies whether the Client should directly connect to a single host rather than
408410
/// autodiscover all servers in the cluster.
409411
///
410412
/// The default value is false.
411-
#[builder(default, setter(strip_option))]
413+
#[builder(default)]
412414
pub direct_connection: Option<bool>,
413415

414416
/// Extra information to append to the driver version in the metadata of the handshake with the
415417
/// server. This should be used by libraries wrapping the driver, e.g. ODMs.
416-
#[builder(default, setter(strip_option))]
418+
#[builder(default)]
417419
pub driver_info: Option<DriverInfo>,
418420

419421
/// The amount of time each monitoring thread should wait between sending an isMaster command
420422
/// to its respective server.
421423
///
422424
/// The default value is 10 seconds.
423-
#[builder(default, setter(strip_option))]
425+
#[builder(default)]
424426
pub heartbeat_freq: Option<Duration>,
425427

426428
/// When running a read operation with a ReadPreference that allows selecting secondaries,
@@ -434,14 +436,14 @@ pub struct ClientOptions {
434436
/// lowest average round trip time is eligible.
435437
///
436438
/// The default value is 15 ms.
437-
#[builder(default, setter(strip_option))]
439+
#[builder(default)]
438440
pub local_threshold: Option<Duration>,
439441

440442
/// The amount of time that a connection can remain idle in a connection pool before being
441443
/// closed. A value of zero indicates that connections should not be closed due to being idle.
442444
///
443445
/// By default, connections will not be closed due to being idle.
444-
#[builder(default, setter(strip_option))]
446+
#[builder(default)]
445447
pub max_idle_time: Option<Duration>,
446448

447449
/// The maximum amount of connections that the Client should allow to be created in a
@@ -450,41 +452,41 @@ pub struct ClientOptions {
450452
/// operation finishes and its connection is checked back into the pool.
451453
///
452454
/// The default value is 100.
453-
#[builder(default, setter(strip_option))]
455+
#[builder(default)]
454456
pub max_pool_size: Option<u32>,
455457

456458
/// The minimum number of connections that should be available in a server's connection pool at
457459
/// a given time. If fewer than `min_pool_size` connections are in the pool, connections will
458460
/// be added to the pool in the background until `min_pool_size` is reached.
459461
///
460462
/// The default value is 0.
461-
#[builder(default, setter(strip_option))]
463+
#[builder(default)]
462464
pub min_pool_size: Option<u32>,
463465

464466
/// Specifies the default read concern for operations performed on the Client. See the
465467
/// ReadConcern type documentation for more details.
466-
#[builder(default, setter(strip_option))]
468+
#[builder(default)]
467469
pub read_concern: Option<ReadConcern>,
468470

469471
/// The name of the replica set that the Client should connect to.
470-
#[builder(default, setter(strip_option))]
472+
#[builder(default)]
471473
pub repl_set_name: Option<String>,
472474

473475
/// Whether or not the client should retry a read operation if the operation fails.
474476
///
475477
/// The default value is true.
476-
#[builder(default, setter(strip_option))]
478+
#[builder(default)]
477479
pub retry_reads: Option<bool>,
478480

479481
/// Whether or not the client should retry a write operation if the operation fails.
480482
///
481483
/// The default value is true.
482-
#[builder(default, setter(strip_option))]
484+
#[builder(default)]
483485
pub retry_writes: Option<bool>,
484486

485487
/// The default selection criteria for operations performed on the Client. See the
486488
/// SelectionCriteria type documentation for more details.
487-
#[builder(default, setter(strip_option))]
489+
#[builder(default)]
488490
pub selection_criteria: Option<SelectionCriteria>,
489491

490492
/// The declared API version for this client.
@@ -503,7 +505,7 @@ pub struct ClientOptions {
503505
/// timing outs
504506
///
505507
/// The default value is 30 seconds.
506-
#[builder(default, setter(strip_option))]
508+
#[builder(default)]
507509
pub server_selection_timeout: Option<Duration>,
508510

509511
#[builder(default, setter(skip))]
@@ -512,12 +514,12 @@ pub struct ClientOptions {
512514
/// The TLS configuration for the Client to use in its connections with the server.
513515
///
514516
/// By default, TLS is disabled.
515-
#[builder(default, setter(strip_option))]
517+
#[builder(default)]
516518
pub tls: Option<Tls>,
517519

518520
/// Specifies the default write concern for operations performed on the Client. See the
519521
/// WriteConcern type documentation for more details.
520-
#[builder(default, setter(strip_option))]
522+
#[builder(default)]
521523
pub write_concern: Option<WriteConcern>,
522524

523525
#[builder(default, setter(skip))]
@@ -541,7 +543,7 @@ pub struct ClientOptions {
541543
pub(crate) resolver_config: Option<ResolverConfig>,
542544

543545
/// Used by tests to override MIN_HEARTBEAT_FREQUENCY.
544-
#[builder(default, setter(strip_option))]
546+
#[builder(default)]
545547
#[cfg(test)]
546548
pub(crate) heartbeat_freq_test: Option<Duration>,
547549
}
@@ -723,7 +725,7 @@ impl Tls {
723725

724726
/// Specifies the TLS configuration that the [`Client`](../struct.Client.html) should use.
725727
#[derive(Clone, Debug, Default, Deserialize, PartialEq, TypedBuilder)]
726-
#[builder(field_defaults(default, setter(strip_option)))]
728+
#[builder(field_defaults(default, setter(into)))]
727729
#[non_exhaustive]
728730
pub struct TlsOptions {
729731
/// Whether or not the [`Client`](../struct.Client.html) should return an error if the server
@@ -861,17 +863,18 @@ impl TlsOptions {
861863
/// Extra information to append to the driver version in the metadata of the handshake with the
862864
/// server. This should be used by libraries wrapping the driver, e.g. ODMs.
863865
#[derive(Clone, Debug, Deserialize, TypedBuilder, PartialEq)]
866+
#[builder(field_defaults(setter(into)))]
864867
#[non_exhaustive]
865868
pub struct DriverInfo {
866869
/// The name of the library wrapping the driver.
867870
pub name: String,
868871

869872
/// The version of the library wrapping the driver.
870-
#[builder(default, setter(strip_option))]
873+
#[builder(default)]
871874
pub version: Option<String>,
872875

873876
/// Optional platform information for the wrapping driver.
874-
#[builder(default, setter(strip_option))]
877+
#[builder(default)]
875878
pub platform: Option<String>,
876879
}
877880

@@ -1836,7 +1839,9 @@ impl ClientOptionsParser {
18361839
}
18371840
None => {
18381841
self.tls = Some(Tls::Enabled(
1839-
TlsOptions::builder().ca_file_path(value.into()).build(),
1842+
TlsOptions::builder()
1843+
.ca_file_path(PathBuf::from(value))
1844+
.build(),
18401845
))
18411846
}
18421847
},
@@ -1853,7 +1858,7 @@ impl ClientOptionsParser {
18531858
None => {
18541859
self.tls = Some(Tls::Enabled(
18551860
TlsOptions::builder()
1856-
.cert_key_file_path(value.into())
1861+
.cert_key_file_path(PathBuf::from(value))
18571862
.build(),
18581863
))
18591864
}
@@ -2290,7 +2295,7 @@ mod tests {
22902295
/// Contains the options that can be used to create a new
22912296
/// [`ClientSession`](../struct.ClientSession.html).
22922297
#[derive(Clone, Debug, Deserialize, TypedBuilder)]
2293-
#[builder(field_defaults(default, setter(strip_option)))]
2298+
#[builder(field_defaults(default, setter(into)))]
22942299
#[serde(rename_all = "camelCase")]
22952300
#[non_exhaustive]
22962301
pub struct SessionOptions {
@@ -2307,7 +2312,7 @@ pub struct SessionOptions {
23072312
/// Contains the options that can be used for a transaction.
23082313
#[skip_serializing_none]
23092314
#[derive(Debug, Default, Serialize, Deserialize, TypedBuilder, Clone)]
2310-
#[builder(field_defaults(default, setter(strip_option)))]
2315+
#[builder(field_defaults(default, setter(into)))]
23112316
#[serde(rename_all = "camelCase")]
23122317
#[non_exhaustive]
23132318
pub struct TransactionOptions {

src/client/session/test.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use crate::{
88
bson::{doc, Bson},
99
error::Result,
1010
options::{Acknowledgment, FindOptions, InsertOneOptions, ReadPreference, WriteConcern},
11+
selection_criteria::SelectionCriteria,
1112
test::{EventClient, TestClient, CLIENT_OPTIONS, LOCK},
1213
Collection,
1314
RUNTIME,
@@ -486,7 +487,7 @@ async fn find_and_getmore_share_session() {
486487
) {
487488
let options = FindOptions::builder()
488489
.batch_size(2)
489-
.selection_criteria(read_preference.into())
490+
.selection_criteria(SelectionCriteria::ReadPreference(read_preference))
490491
.build();
491492

492493
let mut cursor = coll

src/cmap/options.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
/// Contains the options for creating a connection pool.
1515
#[derive(Clone, Default, Deserialize, TypedBuilder, Derivative)]
1616
#[derivative(Debug, PartialEq)]
17-
#[builder(field_defaults(default, setter(strip_option)))]
17+
#[builder(field_defaults(default, setter(into)))]
1818
#[serde(rename_all = "camelCase")]
1919
pub(crate) struct ConnectionPoolOptions {
2020
/// The application name specified by the user. This is sent to the server as part of the
@@ -140,12 +140,13 @@ impl From<ConnectionPoolOptions> for ConnectionOptions {
140140
}
141141

142142
#[derive(Clone, Debug, TypedBuilder)]
143+
#[builder(field_defaults(setter(into)))]
143144
pub(crate) struct StreamOptions {
144145
pub(crate) address: ServerAddress,
145146

146-
#[builder(default, setter(strip_option))]
147+
#[builder(default)]
147148
pub(crate) connect_timeout: Option<Duration>,
148149

149-
#[builder(default, setter(strip_option))]
150+
#[builder(default)]
150151
pub(crate) tls_options: Option<TlsOptions>,
151152
}

src/coll/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,11 @@ where
135135
pub fn clone_with_type<U: Serialize + DeserializeOwned + Unpin + Debug>(
136136
&self,
137137
) -> Collection<U> {
138-
let mut options = CollectionOptions::builder().build();
139-
options.selection_criteria = self.inner.selection_criteria.clone();
140-
options.read_concern = self.inner.read_concern.clone();
141-
options.write_concern = self.inner.write_concern.clone();
138+
let options = CollectionOptions::builder()
139+
.selection_criteria(self.inner.selection_criteria.clone())
140+
.read_concern(self.inner.read_concern.clone())
141+
.write_concern(self.inner.write_concern.clone())
142+
.build();
142143

143144
Collection::new(self.inner.db.clone(), &self.inner.name, Some(options))
144145
}

0 commit comments

Comments
 (0)