Skip to content

RUST-859 Improve bson_util function consistency #411

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 1 commit into from
Aug 2, 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
8 changes: 4 additions & 4 deletions src/bson_util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ pub(crate) fn update_document_check(update: &Document) -> Result<()> {
}
}

pub(crate) fn serialize_duration_as_int_millis<S: Serializer>(
pub(crate) fn serialize_duration_option_as_int_millis<S: Serializer>(
val: &Option<Duration>,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
Expand All @@ -94,7 +94,7 @@ pub(crate) fn serialize_duration_option_as_int_secs<S: Serializer>(
}
}

pub(crate) fn deserialize_duration_from_u64_millis<'de, D>(
pub(crate) fn deserialize_duration_option_from_u64_millis<'de, D>(
deserializer: D,
) -> std::result::Result<Option<Duration>, D::Error>
where
Expand All @@ -104,7 +104,7 @@ where
Ok(millis.map(Duration::from_millis))
}

pub(crate) fn deserialize_duration_from_u64_seconds<'de, D>(
pub(crate) fn deserialize_duration_option_from_u64_seconds<'de, D>(
deserializer: D,
) -> std::result::Result<Option<Duration>, D::Error>
where
Expand All @@ -126,7 +126,7 @@ pub(crate) fn serialize_u32_option_as_i32<S: Serializer>(
}

#[allow(clippy::trivially_copy_pass_by_ref)]
pub(crate) fn serialize_batch_size<S: Serializer>(
pub(crate) fn serialize_u32_option_as_batch_size<S: Serializer>(
val: &Option<u32>,
serializer: S,
) -> std::result::Result<S::Ok, S::Error> {
Expand Down
18 changes: 9 additions & 9 deletions src/client/options/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use webpki_roots::TLS_SERVER_ROOTS;

use crate::{
bson::{doc, Bson, Document},
bson_util::{deserialize_duration_from_u64_millis, serialize_duration_as_int_millis},
bson_util,
client::auth::{AuthMechanism, Credential},
concern::{Acknowledgment, ReadConcern, WriteConcern},
error::{ErrorKind, Result},
Expand Down Expand Up @@ -575,21 +575,21 @@ impl Serialize for ClientOptions {
appname: &'a Option<String>,
compressors: &'a Option<Vec<String>>,

#[serde(serialize_with = "serialize_duration_as_int_millis")]
#[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")]
connecttimeoutms: &'a Option<Duration>,

#[serde(flatten, serialize_with = "Credential::serialize_for_client_options")]
credential: &'a Option<Credential>,

directconnection: &'a Option<bool>,

#[serde(serialize_with = "serialize_duration_as_int_millis")]
#[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")]
heartbeatfrequencyms: &'a Option<Duration>,

#[serde(serialize_with = "serialize_duration_as_int_millis")]
#[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")]
localthresholdms: &'a Option<Duration>,

#[serde(serialize_with = "serialize_duration_as_int_millis")]
#[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")]
maxidletimems: &'a Option<Duration>,

maxpoolsize: &'a Option<u32>,
Expand All @@ -611,10 +611,10 @@ impl Serialize for ClientOptions {
)]
selectioncriteria: &'a Option<SelectionCriteria>,

#[serde(serialize_with = "serialize_duration_as_int_millis")]
#[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")]
serverselectiontimeoutms: &'a Option<Duration>,

#[serde(serialize_with = "serialize_duration_as_int_millis")]
#[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")]
sockettimeoutms: &'a Option<Duration>,

#[serde(flatten, serialize_with = "Tls::serialize_for_client_options")]
Expand Down Expand Up @@ -2344,8 +2344,8 @@ pub struct TransactionOptions {
/// The maximum amount of time to allow a single commitTransaction to run.
#[builder(default)]
#[serde(
serialize_with = "serialize_duration_as_int_millis",
deserialize_with = "deserialize_duration_from_u64_millis",
serialize_with = "bson_util::serialize_duration_option_as_int_millis",
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis",
rename(serialize = "maxTimeMS", deserialize = "maxCommitTimeMS"),
default
)]
Expand Down
2 changes: 1 addition & 1 deletion src/cmap/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub(crate) struct ConnectionPoolOptions {
/// The default is that connections will not be closed due to being idle.
#[serde(rename = "maxIdleTimeMS")]
#[serde(default)]
#[serde(deserialize_with = "bson_util::deserialize_duration_from_u64_millis")]
#[serde(deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis")]
pub(crate) max_idle_time: Option<Duration>,

/// The maximum number of connections that the pool can have at a given time. This includes
Expand Down
2 changes: 1 addition & 1 deletion src/cmap/test/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub enum Operation {
WaitForEvent {
event: String,
count: usize,
#[serde(deserialize_with = "bson_util::deserialize_duration_from_u64_millis")]
#[serde(deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis")]
#[serde(default)]
timeout: Option<Duration>,
},
Expand Down
46 changes: 23 additions & 23 deletions src/coll/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@ use typed_builder::TypedBuilder;

use crate::{
bson::{doc, Bson, Document},
bson_util::{
deserialize_duration_from_u64_millis,
serialize_batch_size,
serialize_duration_as_int_millis,
serialize_u32_option_as_i32,
serialize_u64_option_as_i64,
},
bson_util,
concern::{ReadConcern, WriteConcern},
options::Collation,
selection_criteria::SelectionCriteria,
Expand Down Expand Up @@ -427,7 +421,10 @@ pub struct AggregateOptions {
/// only the number of documents kept in memory at a given time (and by extension, the
/// number of round trips needed to return the entire set of documents returned by the
/// query).
#[serde(serialize_with = "serialize_batch_size", rename(serialize = "cursor"))]
#[serde(
serialize_with = "bson_util::serialize_u32_option_as_batch_size",
rename(serialize = "cursor")
)]
pub batch_size: Option<u32>,

/// Opt out of document-level validation.
Expand All @@ -452,7 +449,7 @@ pub struct AggregateOptions {
/// This option will have no effect on non-tailable cursors that result from this operation.
#[serde(
skip_serializing,
deserialize_with = "deserialize_duration_from_u64_millis",
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis",
default
)]
pub max_await_time: Option<Duration>,
Expand All @@ -462,9 +459,9 @@ pub struct AggregateOptions {
/// This options maps to the `maxTimeMS` MongoDB query option, so the duration will be sent
/// across the wire as an integer number of milliseconds.
#[serde(
serialize_with = "serialize_duration_as_int_millis",
serialize_with = "bson_util::serialize_duration_option_as_int_millis",
rename = "maxTimeMS",
deserialize_with = "deserialize_duration_from_u64_millis",
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis",
default
)]
pub max_time: Option<Duration>,
Expand Down Expand Up @@ -518,7 +515,10 @@ pub struct CountOptions {
///
/// This options maps to the `maxTimeMS` MongoDB query option, so the duration will be sent
/// across the wire as an integer number of milliseconds.
#[serde(default, deserialize_with = "deserialize_duration_from_u64_millis")]
#[serde(
default,
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis"
)]
pub max_time: Option<Duration>,

/// The number of documents to skip before counting.
Expand Down Expand Up @@ -558,9 +558,9 @@ pub struct EstimatedDocumentCountOptions {
/// across the wire as an integer number of milliseconds.
#[serde(
default,
serialize_with = "serialize_duration_as_int_millis",
serialize_with = "bson_util::serialize_duration_option_as_int_millis",
rename = "maxTimeMS",
deserialize_with = "deserialize_duration_from_u64_millis"
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis"
)]
pub max_time: Option<Duration>,

Expand Down Expand Up @@ -588,9 +588,9 @@ pub struct DistinctOptions {
/// across the wire as an integer number of milliseconds.
#[serde(
default,
serialize_with = "serialize_duration_as_int_millis",
serialize_with = "bson_util::serialize_duration_option_as_int_millis",
rename = "maxTimeMS",
deserialize_with = "deserialize_duration_from_u64_millis"
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis"
)]
pub max_time: Option<Duration>,

Expand Down Expand Up @@ -633,7 +633,7 @@ pub struct FindOptions {
/// only the number of documents kept in memory at a given time (and by extension, the
/// number of round trips needed to return the entire set of documents returned by the
/// query.
#[serde(serialize_with = "serialize_u32_option_as_i32")]
#[serde(serialize_with = "bson_util::serialize_u32_option_as_i32")]
pub batch_size: Option<u32>,

/// Tags the query with an arbitrary string to help trace the operation through the database
Expand Down Expand Up @@ -665,7 +665,7 @@ pub struct FindOptions {
///
/// Note: this option is deprecated starting in MongoDB version 4.0 and removed in MongoDB 4.2.
/// Use the maxTimeMS option instead.
#[serde(serialize_with = "serialize_u64_option_as_i64")]
#[serde(serialize_with = "bson_util::serialize_u64_option_as_i64")]
pub max_scan: Option<u64>,

/// The maximum amount of time to allow the query to run.
Expand All @@ -674,7 +674,7 @@ pub struct FindOptions {
/// across the wire as an integer number of milliseconds.
#[serde(
rename = "maxTimeMS",
serialize_with = "serialize_duration_as_int_millis"
serialize_with = "bson_util::serialize_duration_option_as_int_millis"
)]
pub max_time: Option<Duration>,

Expand Down Expand Up @@ -705,7 +705,7 @@ pub struct FindOptions {
pub show_record_id: Option<bool>,

/// The number of documents to skip before counting.
#[serde(serialize_with = "serialize_u64_option_as_i64")]
#[serde(serialize_with = "bson_util::serialize_u64_option_as_i64")]
pub skip: Option<u64>,

/// The order of the documents for the purposes of the operation.
Expand Down Expand Up @@ -791,14 +791,14 @@ pub struct FindOneOptions {
///
/// Note: this option is deprecated starting in MongoDB version 4.0 and removed in MongoDB 4.2.
/// Use the maxTimeMS option instead.
#[serde(serialize_with = "serialize_u64_option_as_i64")]
#[serde(serialize_with = "bson_util::serialize_u64_option_as_i64")]
pub max_scan: Option<u64>,

/// The maximum amount of time to allow the query to run.
///
/// This options maps to the `maxTimeMS` MongoDB query option, so the duration will be sent
/// across the wire as an integer number of milliseconds.
#[serde(deserialize_with = "deserialize_duration_from_u64_millis")]
#[serde(deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis")]
pub max_time: Option<Duration>,

/// The inclusive lower bound for a specific index.
Expand All @@ -824,7 +824,7 @@ pub struct FindOneOptions {
pub show_record_id: Option<bool>,

/// The number of documents to skip before counting.
#[serde(serialize_with = "serialize_u64_option_as_i64")]
#[serde(serialize_with = "bson_util::serialize_u64_option_as_i64")]
pub skip: Option<u64>,

/// The order of the documents for the purposes of the operation.
Expand Down
6 changes: 3 additions & 3 deletions src/concern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ pub struct WriteConcern {
/// write would not have finished propagating if allowed more time to finish, and the
/// server will not roll back the writes that occurred before the timeout was reached.
#[serde(rename = "wtimeout")]
#[serde(serialize_with = "bson_util::serialize_duration_as_int_millis")]
#[serde(deserialize_with = "bson_util::deserialize_duration_from_u64_millis")]
#[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")]
#[serde(deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis")]
#[serde(default)]
pub w_timeout: Option<Duration>,

Expand Down Expand Up @@ -302,7 +302,7 @@ impl WriteConcern {
struct WriteConcernHelper<'a> {
w: Option<&'a Acknowledgment>,

#[serde(serialize_with = "bson_util::serialize_duration_as_int_millis")]
#[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")]
wtimeoutms: Option<Duration>,

journal: Option<bool>,
Expand Down
4 changes: 2 additions & 2 deletions src/db/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub struct CreateCollectionOptions {
/// information.
#[serde(
default,
deserialize_with = "bson_util::deserialize_duration_from_u64_seconds",
deserialize_with = "bson_util::deserialize_duration_option_from_u64_seconds",
serialize_with = "bson_util::serialize_duration_option_as_int_secs"
)]
pub expire_after_seconds: Option<Duration>,
Expand Down Expand Up @@ -200,7 +200,7 @@ pub struct ListCollectionsOptions {
/// number of round trips needed to return the entire set of documents returned by the
/// query).
#[serde(
serialize_with = "bson_util::serialize_batch_size",
serialize_with = "bson_util::serialize_u32_option_as_batch_size",
rename(serialize = "cursor")
)]
pub batch_size: Option<u32>,
Expand Down
4 changes: 2 additions & 2 deletions src/event/cmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::time::Duration;

use serde::Deserialize;

use crate::options::ServerAddress;
use crate::{bson_util, options::ServerAddress};

/// We implement `Deserialize` for all of the event types so that we can more easily parse the CMAP
/// spec tests. However, we have no need to parse the address field from the JSON files (if it's
Expand Down Expand Up @@ -44,7 +44,7 @@ pub struct ConnectionPoolOptions {
/// The default is that connections will not be closed due to being idle.
#[serde(rename = "maxIdleTimeMS")]
#[serde(default)]
#[serde(deserialize_with = "crate::bson_util::deserialize_duration_from_u64_millis")]
#[serde(deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis")]
pub max_idle_time: Option<Duration>,

/// The maximum number of connections that the pool can have at a given time. This includes
Expand Down
2 changes: 1 addition & 1 deletion src/operation/find_and_modify/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub(super) struct FindAndModifyOptions {
pub(crate) array_filters: Option<Vec<Document>>,

#[serde(
serialize_with = "bson_util::serialize_duration_as_int_millis",
serialize_with = "bson_util::serialize_duration_option_as_int_millis",
rename = "maxTimeMS"
)]
#[builder(default)]
Expand Down
4 changes: 2 additions & 2 deletions src/selection_criteria.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use typed_builder::TypedBuilder;

use crate::{
bson::{doc, Bson, Document},
bson_util::deserialize_duration_from_u64_seconds,
bson_util,
error::{Error, ErrorKind, Result},
options::ServerAddress,
sdam::public::ServerInfo,
Expand Down Expand Up @@ -188,7 +188,7 @@ pub struct ReadPreferenceOptions {
#[serde(
rename = "maxStalenessSeconds",
default,
deserialize_with = "deserialize_duration_from_u64_seconds"
deserialize_with = "bson_util::deserialize_duration_option_from_u64_seconds"
)]
pub max_staleness: Option<Duration>,

Expand Down
2 changes: 1 addition & 1 deletion src/test/spec/command_monitoring/operation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ struct FindModifiers {
hint: Option<Hint>,
#[serde(
rename = "$maxTimeMS",
deserialize_with = "bson_util::deserialize_duration_from_u64_millis",
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis",
default
)]
max_time: Option<Duration>,
Expand Down