From 098256bb96abcdf32b0fdedf4477961fa2f12fff Mon Sep 17 00:00:00 2001 From: Nathan Blinn Date: Fri, 30 Jul 2021 13:39:49 -0400 Subject: [PATCH] RUST-859 Improve bson_util function consistency --- src/bson_util/mod.rs | 8 ++-- src/client/options/mod.rs | 18 ++++---- src/cmap/options.rs | 2 +- src/cmap/test/file.rs | 2 +- src/coll/options.rs | 46 +++++++++---------- src/concern/mod.rs | 6 +-- src/db/options.rs | 4 +- src/event/cmap.rs | 4 +- src/operation/find_and_modify/options.rs | 2 +- src/selection_criteria.rs | 4 +- src/test/spec/command_monitoring/operation.rs | 2 +- 11 files changed, 49 insertions(+), 49 deletions(-) diff --git a/src/bson_util/mod.rs b/src/bson_util/mod.rs index 705db73b0..40d78b20e 100644 --- a/src/bson_util/mod.rs +++ b/src/bson_util/mod.rs @@ -68,7 +68,7 @@ pub(crate) fn update_document_check(update: &Document) -> Result<()> { } } -pub(crate) fn serialize_duration_as_int_millis( +pub(crate) fn serialize_duration_option_as_int_millis( val: &Option, serializer: S, ) -> std::result::Result { @@ -94,7 +94,7 @@ pub(crate) fn serialize_duration_option_as_int_secs( } } -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, D::Error> where @@ -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, D::Error> where @@ -126,7 +126,7 @@ pub(crate) fn serialize_u32_option_as_i32( } #[allow(clippy::trivially_copy_pass_by_ref)] -pub(crate) fn serialize_batch_size( +pub(crate) fn serialize_u32_option_as_batch_size( val: &Option, serializer: S, ) -> std::result::Result { diff --git a/src/client/options/mod.rs b/src/client/options/mod.rs index cb34aee25..9db39c822 100644 --- a/src/client/options/mod.rs +++ b/src/client/options/mod.rs @@ -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}, @@ -575,7 +575,7 @@ impl Serialize for ClientOptions { appname: &'a Option, compressors: &'a Option>, - #[serde(serialize_with = "serialize_duration_as_int_millis")] + #[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")] connecttimeoutms: &'a Option, #[serde(flatten, serialize_with = "Credential::serialize_for_client_options")] @@ -583,13 +583,13 @@ impl Serialize for ClientOptions { directconnection: &'a Option, - #[serde(serialize_with = "serialize_duration_as_int_millis")] + #[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")] heartbeatfrequencyms: &'a Option, - #[serde(serialize_with = "serialize_duration_as_int_millis")] + #[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")] localthresholdms: &'a Option, - #[serde(serialize_with = "serialize_duration_as_int_millis")] + #[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")] maxidletimems: &'a Option, maxpoolsize: &'a Option, @@ -611,10 +611,10 @@ impl Serialize for ClientOptions { )] selectioncriteria: &'a Option, - #[serde(serialize_with = "serialize_duration_as_int_millis")] + #[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")] serverselectiontimeoutms: &'a Option, - #[serde(serialize_with = "serialize_duration_as_int_millis")] + #[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")] sockettimeoutms: &'a Option, #[serde(flatten, serialize_with = "Tls::serialize_for_client_options")] @@ -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 )] diff --git a/src/cmap/options.rs b/src/cmap/options.rs index 3f795dcb2..a5c51d0a5 100644 --- a/src/cmap/options.rs +++ b/src/cmap/options.rs @@ -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, /// The maximum number of connections that the pool can have at a given time. This includes diff --git a/src/cmap/test/file.rs b/src/cmap/test/file.rs index 6ade2e4c8..96d92792a 100644 --- a/src/cmap/test/file.rs +++ b/src/cmap/test/file.rs @@ -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, }, diff --git a/src/coll/options.rs b/src/coll/options.rs index 983d1ca4e..2bed5e8c1 100644 --- a/src/coll/options.rs +++ b/src/coll/options.rs @@ -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, @@ -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, /// Opt out of document-level validation. @@ -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, @@ -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, @@ -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, /// The number of documents to skip before counting. @@ -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, @@ -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, @@ -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, /// Tags the query with an arbitrary string to help trace the operation through the database @@ -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, /// The maximum amount of time to allow the query to run. @@ -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, @@ -705,7 +705,7 @@ pub struct FindOptions { pub show_record_id: Option, /// 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, /// The order of the documents for the purposes of the operation. @@ -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, /// 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, /// The inclusive lower bound for a specific index. @@ -824,7 +824,7 @@ pub struct FindOneOptions { pub show_record_id: Option, /// 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, /// The order of the documents for the purposes of the operation. diff --git a/src/concern/mod.rs b/src/concern/mod.rs index 9e386f081..aa2bdb114 100644 --- a/src/concern/mod.rs +++ b/src/concern/mod.rs @@ -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, @@ -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, journal: Option, diff --git a/src/db/options.rs b/src/db/options.rs index faf3d2f07..db9903387 100644 --- a/src/db/options.rs +++ b/src/db/options.rs @@ -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, @@ -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, diff --git a/src/event/cmap.rs b/src/event/cmap.rs index 3995163b9..c3a491333 100644 --- a/src/event/cmap.rs +++ b/src/event/cmap.rs @@ -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 @@ -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, /// The maximum number of connections that the pool can have at a given time. This includes diff --git a/src/operation/find_and_modify/options.rs b/src/operation/find_and_modify/options.rs index b1fd72cd7..06c86d02a 100644 --- a/src/operation/find_and_modify/options.rs +++ b/src/operation/find_and_modify/options.rs @@ -57,7 +57,7 @@ pub(super) struct FindAndModifyOptions { pub(crate) array_filters: Option>, #[serde( - serialize_with = "bson_util::serialize_duration_as_int_millis", + serialize_with = "bson_util::serialize_duration_option_as_int_millis", rename = "maxTimeMS" )] #[builder(default)] diff --git a/src/selection_criteria.rs b/src/selection_criteria.rs index 0ac1dfc49..bcf35cf8c 100644 --- a/src/selection_criteria.rs +++ b/src/selection_criteria.rs @@ -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, @@ -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, diff --git a/src/test/spec/command_monitoring/operation.rs b/src/test/spec/command_monitoring/operation.rs index a6d58c30d..c2e8453c4 100644 --- a/src/test/spec/command_monitoring/operation.rs +++ b/src/test/spec/command_monitoring/operation.rs @@ -118,7 +118,7 @@ struct FindModifiers { hint: Option, #[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,