Skip to content

Commit 098256b

Browse files
author
Nathan Blinn
committed
RUST-859 Improve bson_util function consistency
1 parent dc66a1b commit 098256b

File tree

11 files changed

+49
-49
lines changed

11 files changed

+49
-49
lines changed

src/bson_util/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ pub(crate) fn update_document_check(update: &Document) -> Result<()> {
6868
}
6969
}
7070

71-
pub(crate) fn serialize_duration_as_int_millis<S: Serializer>(
71+
pub(crate) fn serialize_duration_option_as_int_millis<S: Serializer>(
7272
val: &Option<Duration>,
7373
serializer: S,
7474
) -> std::result::Result<S::Ok, S::Error> {
@@ -94,7 +94,7 @@ pub(crate) fn serialize_duration_option_as_int_secs<S: Serializer>(
9494
}
9595
}
9696

97-
pub(crate) fn deserialize_duration_from_u64_millis<'de, D>(
97+
pub(crate) fn deserialize_duration_option_from_u64_millis<'de, D>(
9898
deserializer: D,
9999
) -> std::result::Result<Option<Duration>, D::Error>
100100
where
@@ -104,7 +104,7 @@ where
104104
Ok(millis.map(Duration::from_millis))
105105
}
106106

107-
pub(crate) fn deserialize_duration_from_u64_seconds<'de, D>(
107+
pub(crate) fn deserialize_duration_option_from_u64_seconds<'de, D>(
108108
deserializer: D,
109109
) -> std::result::Result<Option<Duration>, D::Error>
110110
where
@@ -126,7 +126,7 @@ pub(crate) fn serialize_u32_option_as_i32<S: Serializer>(
126126
}
127127

128128
#[allow(clippy::trivially_copy_pass_by_ref)]
129-
pub(crate) fn serialize_batch_size<S: Serializer>(
129+
pub(crate) fn serialize_u32_option_as_batch_size<S: Serializer>(
130130
val: &Option<u32>,
131131
serializer: S,
132132
) -> std::result::Result<S::Ok, S::Error> {

src/client/options/mod.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use webpki_roots::TLS_SERVER_ROOTS;
3939

4040
use crate::{
4141
bson::{doc, Bson, Document},
42-
bson_util::{deserialize_duration_from_u64_millis, serialize_duration_as_int_millis},
42+
bson_util,
4343
client::auth::{AuthMechanism, Credential},
4444
concern::{Acknowledgment, ReadConcern, WriteConcern},
4545
error::{ErrorKind, Result},
@@ -575,21 +575,21 @@ impl Serialize for ClientOptions {
575575
appname: &'a Option<String>,
576576
compressors: &'a Option<Vec<String>>,
577577

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

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

584584
directconnection: &'a Option<bool>,
585585

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

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

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

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

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

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

620620
#[serde(flatten, serialize_with = "Tls::serialize_for_client_options")]
@@ -2344,8 +2344,8 @@ pub struct TransactionOptions {
23442344
/// The maximum amount of time to allow a single commitTransaction to run.
23452345
#[builder(default)]
23462346
#[serde(
2347-
serialize_with = "serialize_duration_as_int_millis",
2348-
deserialize_with = "deserialize_duration_from_u64_millis",
2347+
serialize_with = "bson_util::serialize_duration_option_as_int_millis",
2348+
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis",
23492349
rename(serialize = "maxTimeMS", deserialize = "maxCommitTimeMS"),
23502350
default
23512351
)]

src/cmap/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub(crate) struct ConnectionPoolOptions {
5454
/// The default is that connections will not be closed due to being idle.
5555
#[serde(rename = "maxIdleTimeMS")]
5656
#[serde(default)]
57-
#[serde(deserialize_with = "bson_util::deserialize_duration_from_u64_millis")]
57+
#[serde(deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis")]
5858
pub(crate) max_idle_time: Option<Duration>,
5959

6060
/// The maximum number of connections that the pool can have at a given time. This includes

src/cmap/test/file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ pub enum Operation {
6767
WaitForEvent {
6868
event: String,
6969
count: usize,
70-
#[serde(deserialize_with = "bson_util::deserialize_duration_from_u64_millis")]
70+
#[serde(deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis")]
7171
#[serde(default)]
7272
timeout: Option<Duration>,
7373
},

src/coll/options.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,7 @@ use typed_builder::TypedBuilder;
66

77
use crate::{
88
bson::{doc, Bson, Document},
9-
bson_util::{
10-
deserialize_duration_from_u64_millis,
11-
serialize_batch_size,
12-
serialize_duration_as_int_millis,
13-
serialize_u32_option_as_i32,
14-
serialize_u64_option_as_i64,
15-
},
9+
bson_util,
1610
concern::{ReadConcern, WriteConcern},
1711
options::Collation,
1812
selection_criteria::SelectionCriteria,
@@ -427,7 +421,10 @@ pub struct AggregateOptions {
427421
/// only the number of documents kept in memory at a given time (and by extension, the
428422
/// number of round trips needed to return the entire set of documents returned by the
429423
/// query).
430-
#[serde(serialize_with = "serialize_batch_size", rename(serialize = "cursor"))]
424+
#[serde(
425+
serialize_with = "bson_util::serialize_u32_option_as_batch_size",
426+
rename(serialize = "cursor")
427+
)]
431428
pub batch_size: Option<u32>,
432429

433430
/// Opt out of document-level validation.
@@ -452,7 +449,7 @@ pub struct AggregateOptions {
452449
/// This option will have no effect on non-tailable cursors that result from this operation.
453450
#[serde(
454451
skip_serializing,
455-
deserialize_with = "deserialize_duration_from_u64_millis",
452+
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis",
456453
default
457454
)]
458455
pub max_await_time: Option<Duration>,
@@ -462,9 +459,9 @@ pub struct AggregateOptions {
462459
/// This options maps to the `maxTimeMS` MongoDB query option, so the duration will be sent
463460
/// across the wire as an integer number of milliseconds.
464461
#[serde(
465-
serialize_with = "serialize_duration_as_int_millis",
462+
serialize_with = "bson_util::serialize_duration_option_as_int_millis",
466463
rename = "maxTimeMS",
467-
deserialize_with = "deserialize_duration_from_u64_millis",
464+
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis",
468465
default
469466
)]
470467
pub max_time: Option<Duration>,
@@ -518,7 +515,10 @@ pub struct CountOptions {
518515
///
519516
/// This options maps to the `maxTimeMS` MongoDB query option, so the duration will be sent
520517
/// across the wire as an integer number of milliseconds.
521-
#[serde(default, deserialize_with = "deserialize_duration_from_u64_millis")]
518+
#[serde(
519+
default,
520+
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis"
521+
)]
522522
pub max_time: Option<Duration>,
523523

524524
/// The number of documents to skip before counting.
@@ -558,9 +558,9 @@ pub struct EstimatedDocumentCountOptions {
558558
/// across the wire as an integer number of milliseconds.
559559
#[serde(
560560
default,
561-
serialize_with = "serialize_duration_as_int_millis",
561+
serialize_with = "bson_util::serialize_duration_option_as_int_millis",
562562
rename = "maxTimeMS",
563-
deserialize_with = "deserialize_duration_from_u64_millis"
563+
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis"
564564
)]
565565
pub max_time: Option<Duration>,
566566

@@ -588,9 +588,9 @@ pub struct DistinctOptions {
588588
/// across the wire as an integer number of milliseconds.
589589
#[serde(
590590
default,
591-
serialize_with = "serialize_duration_as_int_millis",
591+
serialize_with = "bson_util::serialize_duration_option_as_int_millis",
592592
rename = "maxTimeMS",
593-
deserialize_with = "deserialize_duration_from_u64_millis"
593+
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis"
594594
)]
595595
pub max_time: Option<Duration>,
596596

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

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

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

@@ -705,7 +705,7 @@ pub struct FindOptions {
705705
pub show_record_id: Option<bool>,
706706

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

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

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

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

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

830830
/// The order of the documents for the purposes of the operation.

src/concern/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,8 @@ pub struct WriteConcern {
187187
/// write would not have finished propagating if allowed more time to finish, and the
188188
/// server will not roll back the writes that occurred before the timeout was reached.
189189
#[serde(rename = "wtimeout")]
190-
#[serde(serialize_with = "bson_util::serialize_duration_as_int_millis")]
191-
#[serde(deserialize_with = "bson_util::deserialize_duration_from_u64_millis")]
190+
#[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")]
191+
#[serde(deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis")]
192192
#[serde(default)]
193193
pub w_timeout: Option<Duration>,
194194

@@ -302,7 +302,7 @@ impl WriteConcern {
302302
struct WriteConcernHelper<'a> {
303303
w: Option<&'a Acknowledgment>,
304304

305-
#[serde(serialize_with = "bson_util::serialize_duration_as_int_millis")]
305+
#[serde(serialize_with = "bson_util::serialize_duration_option_as_int_millis")]
306306
wtimeoutms: Option<Duration>,
307307

308308
journal: Option<bool>,

src/db/options.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ pub struct CreateCollectionOptions {
9999
/// information.
100100
#[serde(
101101
default,
102-
deserialize_with = "bson_util::deserialize_duration_from_u64_seconds",
102+
deserialize_with = "bson_util::deserialize_duration_option_from_u64_seconds",
103103
serialize_with = "bson_util::serialize_duration_option_as_int_secs"
104104
)]
105105
pub expire_after_seconds: Option<Duration>,
@@ -200,7 +200,7 @@ pub struct ListCollectionsOptions {
200200
/// number of round trips needed to return the entire set of documents returned by the
201201
/// query).
202202
#[serde(
203-
serialize_with = "bson_util::serialize_batch_size",
203+
serialize_with = "bson_util::serialize_u32_option_as_batch_size",
204204
rename(serialize = "cursor")
205205
)]
206206
pub batch_size: Option<u32>,

src/event/cmap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::time::Duration;
55

66
use serde::Deserialize;
77

8-
use crate::options::ServerAddress;
8+
use crate::{bson_util, options::ServerAddress};
99

1010
/// We implement `Deserialize` for all of the event types so that we can more easily parse the CMAP
1111
/// 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 {
4444
/// The default is that connections will not be closed due to being idle.
4545
#[serde(rename = "maxIdleTimeMS")]
4646
#[serde(default)]
47-
#[serde(deserialize_with = "crate::bson_util::deserialize_duration_from_u64_millis")]
47+
#[serde(deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis")]
4848
pub max_idle_time: Option<Duration>,
4949

5050
/// The maximum number of connections that the pool can have at a given time. This includes

src/operation/find_and_modify/options.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ pub(super) struct FindAndModifyOptions {
5757
pub(crate) array_filters: Option<Vec<Document>>,
5858

5959
#[serde(
60-
serialize_with = "bson_util::serialize_duration_as_int_millis",
60+
serialize_with = "bson_util::serialize_duration_option_as_int_millis",
6161
rename = "maxTimeMS"
6262
)]
6363
#[builder(default)]

src/selection_criteria.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use typed_builder::TypedBuilder;
66

77
use crate::{
88
bson::{doc, Bson, Document},
9-
bson_util::deserialize_duration_from_u64_seconds,
9+
bson_util,
1010
error::{Error, ErrorKind, Result},
1111
options::ServerAddress,
1212
sdam::public::ServerInfo,
@@ -188,7 +188,7 @@ pub struct ReadPreferenceOptions {
188188
#[serde(
189189
rename = "maxStalenessSeconds",
190190
default,
191-
deserialize_with = "deserialize_duration_from_u64_seconds"
191+
deserialize_with = "bson_util::deserialize_duration_option_from_u64_seconds"
192192
)]
193193
pub max_staleness: Option<Duration>,
194194

src/test/spec/command_monitoring/operation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ struct FindModifiers {
118118
hint: Option<Hint>,
119119
#[serde(
120120
rename = "$maxTimeMS",
121-
deserialize_with = "bson_util::deserialize_duration_from_u64_millis",
121+
deserialize_with = "bson_util::deserialize_duration_option_from_u64_millis",
122122
default
123123
)]
124124
max_time: Option<Duration>,

0 commit comments

Comments
 (0)