Skip to content

RUST-858 Use into field setter for typed-builder #368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/client/auth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ impl FromStr for AuthMechanism {
/// Some fields (mechanism and source) may be omitted and will either be negotiated or assigned a
/// default value, depending on the values of other fields in the credential.
#[derive(Clone, Default, Deserialize, TypedBuilder, PartialEq)]
#[builder(field_defaults(default, setter(strip_option)))]
#[builder(field_defaults(default, setter(into)))]
#[non_exhaustive]
pub struct Credential {
/// The username to authenticate with. This applies to all mechanisms but may be omitted when
Expand Down
65 changes: 35 additions & 30 deletions src/client/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ impl<'de> Deserialize<'de> for ServerApiVersion {

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

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

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

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

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

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

/// The connect timeout passed to each underlying TcpStream when attemtping to connect to the
/// server.
///
/// The default value is 10 seconds.
#[builder(default, setter(strip_option))]
#[builder(default)]
pub connect_timeout: Option<Duration>,

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

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

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

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

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

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

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

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

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

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

/// Whether or not the client should retry a read operation if the operation fails.
///
/// The default value is true.
#[builder(default, setter(strip_option))]
#[builder(default)]
pub retry_reads: Option<bool>,

/// Whether or not the client should retry a write operation if the operation fails.
///
/// The default value is true.
#[builder(default, setter(strip_option))]
#[builder(default)]
pub retry_writes: Option<bool>,

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

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

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

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

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

/// Used by tests to override MIN_HEARTBEAT_FREQUENCY.
#[builder(default, setter(strip_option))]
#[builder(default)]
#[cfg(test)]
pub(crate) heartbeat_freq_test: Option<Duration>,
}
Expand Down Expand Up @@ -723,7 +725,7 @@ impl Tls {

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

/// The version of the library wrapping the driver.
#[builder(default, setter(strip_option))]
#[builder(default)]
pub version: Option<String>,

/// Optional platform information for the wrapping driver.
#[builder(default, setter(strip_option))]
#[builder(default)]
pub platform: Option<String>,
}

Expand Down Expand Up @@ -1836,7 +1839,9 @@ impl ClientOptionsParser {
}
None => {
self.tls = Some(Tls::Enabled(
TlsOptions::builder().ca_file_path(value.into()).build(),
TlsOptions::builder()
.ca_file_path(PathBuf::from(value))
.build(),
))
}
},
Expand All @@ -1853,7 +1858,7 @@ impl ClientOptionsParser {
None => {
self.tls = Some(Tls::Enabled(
TlsOptions::builder()
.cert_key_file_path(value.into())
.cert_key_file_path(PathBuf::from(value))
.build(),
))
}
Expand Down Expand Up @@ -2290,7 +2295,7 @@ mod tests {
/// Contains the options that can be used to create a new
/// [`ClientSession`](../struct.ClientSession.html).
#[derive(Clone, Debug, Deserialize, TypedBuilder)]
#[builder(field_defaults(default, setter(strip_option)))]
#[builder(field_defaults(default, setter(into)))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct SessionOptions {
Expand All @@ -2307,7 +2312,7 @@ pub struct SessionOptions {
/// Contains the options that can be used for a transaction.
#[skip_serializing_none]
#[derive(Debug, Default, Serialize, Deserialize, TypedBuilder, Clone)]
#[builder(field_defaults(default, setter(strip_option)))]
#[builder(field_defaults(default, setter(into)))]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct TransactionOptions {
Expand Down
3 changes: 2 additions & 1 deletion src/client/session/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use crate::{
bson::{doc, Bson},
error::Result,
options::{Acknowledgment, FindOptions, InsertOneOptions, ReadPreference, WriteConcern},
selection_criteria::SelectionCriteria,
test::{EventClient, TestClient, CLIENT_OPTIONS, LOCK},
Collection,
RUNTIME,
Expand Down Expand Up @@ -482,7 +483,7 @@ async fn find_and_getmore_share_session() {
async fn run_test(client: &EventClient, coll: &Collection, read_preference: ReadPreference) {
let options = FindOptions::builder()
.batch_size(2)
.selection_criteria(read_preference.into())
.selection_criteria(SelectionCriteria::ReadPreference(read_preference))
.build();

let mut cursor = coll
Expand Down
7 changes: 4 additions & 3 deletions src/cmap/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::{
/// Contains the options for creating a connection pool.
#[derive(Clone, Default, Deserialize, TypedBuilder, Derivative)]
#[derivative(Debug, PartialEq)]
#[builder(field_defaults(default, setter(strip_option)))]
#[builder(field_defaults(default, setter(into)))]
#[serde(rename_all = "camelCase")]
pub(crate) struct ConnectionPoolOptions {
/// The application name specified by the user. This is sent to the server as part of the
Expand Down Expand Up @@ -140,12 +140,13 @@ impl From<ConnectionPoolOptions> for ConnectionOptions {
}

#[derive(Clone, Debug, TypedBuilder)]
#[builder(field_defaults(setter(into)))]
pub(crate) struct StreamOptions {
pub(crate) address: ServerAddress,

#[builder(default, setter(strip_option))]
#[builder(default)]
pub(crate) connect_timeout: Option<Duration>,

#[builder(default, setter(strip_option))]
#[builder(default)]
pub(crate) tls_options: Option<TlsOptions>,
}
9 changes: 5 additions & 4 deletions src/coll/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ where
pub fn clone_with_type<U: Serialize + DeserializeOwned + Unpin + Debug>(
&self,
) -> Collection<U> {
let mut options = CollectionOptions::builder().build();
options.selection_criteria = self.inner.selection_criteria.clone();
options.read_concern = self.inner.read_concern.clone();
options.write_concern = self.inner.write_concern.clone();
let options = CollectionOptions::builder()
.selection_criteria(self.inner.selection_criteria.clone())
.read_concern(self.inner.read_concern.clone())
.write_concern(self.inner.write_concern.clone())
.build();

Collection::new(self.inner.db.clone(), &self.inner.name, Some(options))
}
Expand Down
Loading