Skip to content

Commit 6f48d65

Browse files
authored
RUST-1810 Clean up concern helpers (#1011)
1 parent 5000d32 commit 6f48d65

File tree

9 files changed

+53
-55
lines changed

9 files changed

+53
-55
lines changed

manual/src/encryption.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ async fn main() -> Result<()> {
294294
db.create_collection(
295295
&encrypted_namespace.coll,
296296
CreateCollectionOptions::builder()
297-
.write_concern(WriteConcern::MAJORITY)
297+
.write_concern(WriteConcern::majority())
298298
.validator(doc! { "$jsonSchema": schema })
299299
.build(),
300300
).await?;

src/client/csfle/client_encryption.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ impl ClientEncryption {
8585
.collection_with_options(
8686
&key_vault_namespace.coll,
8787
CollectionOptions::builder()
88-
.write_concern(WriteConcern::MAJORITY)
89-
.read_concern(ReadConcern::MAJORITY)
88+
.write_concern(WriteConcern::majority())
89+
.read_concern(ReadConcern::majority())
9090
.build(),
9191
);
9292
Ok(ClientEncryption {

src/client/csfle/state_machine.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl CryptExecutor {
167167
.find(
168168
filter,
169169
FindOptions::builder()
170-
.read_concern(ReadConcern::MAJORITY)
170+
.read_concern(ReadConcern::majority())
171171
.build(),
172172
)
173173
.await?;

src/concern.rs

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -29,29 +29,6 @@ pub struct ReadConcern {
2929
pub level: ReadConcernLevel,
3030
}
3131

32-
impl ReadConcern {
33-
/// A `ReadConcern` with level `ReadConcernLevel::Local`.
34-
pub const LOCAL: ReadConcern = ReadConcern {
35-
level: ReadConcernLevel::Local,
36-
};
37-
/// A `ReadConcern` with level `ReadConcernLevel::Majority`.
38-
pub const MAJORITY: ReadConcern = ReadConcern {
39-
level: ReadConcernLevel::Majority,
40-
};
41-
/// A `ReadConcern` with level `ReadConcernLevel::Linearizable`.
42-
pub const LINEARIZABLE: ReadConcern = ReadConcern {
43-
level: ReadConcernLevel::Linearizable,
44-
};
45-
/// A `ReadConcern` with level `ReadConcernLevel::Available`.
46-
pub const AVAILABLE: ReadConcern = ReadConcern {
47-
level: ReadConcernLevel::Available,
48-
};
49-
/// A `ReadConcern` with level `ReadConcernLevel::Snapshot`.
50-
pub const SNAPSHOT: ReadConcern = ReadConcern {
51-
level: ReadConcernLevel::Snapshot,
52-
};
53-
}
54-
5532
/// An internal-only read concern type that allows the omission of a "level" as well as
5633
/// specification of "atClusterTime" and "afterClusterTime".
5734
#[skip_serializing_none]
@@ -104,8 +81,8 @@ impl ReadConcern {
10481
/// Creates a read concern with a custom read concern level. This is present to provide forwards
10582
/// compatibility with any future read concerns which may be added to new versions of
10683
/// MongoDB.
107-
pub fn custom(level: String) -> Self {
108-
ReadConcernLevel::from_str(level.as_str()).into()
84+
pub fn custom(level: impl AsRef<str>) -> Self {
85+
ReadConcernLevel::from_str(level.as_ref()).into()
10986
}
11087

11188
#[cfg(test)]
@@ -242,20 +219,6 @@ pub struct WriteConcern {
242219
pub journal: Option<bool>,
243220
}
244221

245-
impl WriteConcern {
246-
// Can't use `Default::default()` in const contexts yet :(
247-
const DEFAULT: WriteConcern = WriteConcern {
248-
w: None,
249-
w_timeout: None,
250-
journal: None,
251-
};
252-
/// A `WriteConcern` requesting `Acknowledgment::Majority`.
253-
pub const MAJORITY: WriteConcern = WriteConcern {
254-
w: Some(Acknowledgment::Majority),
255-
..WriteConcern::DEFAULT
256-
};
257-
}
258-
259222
/// The type of the `w` field in a [`WriteConcern`](struct.WriteConcern.html).
260223
#[derive(Clone, Debug, PartialEq)]
261224
#[non_exhaustive]
@@ -311,6 +274,16 @@ impl From<u32> for Acknowledgment {
311274
}
312275
}
313276

277+
impl From<&str> for Acknowledgment {
278+
fn from(s: &str) -> Self {
279+
if s == "majority" {
280+
Acknowledgment::Majority
281+
} else {
282+
Acknowledgment::Custom(s.to_string())
283+
}
284+
}
285+
}
286+
314287
impl From<String> for Acknowledgment {
315288
fn from(s: String) -> Self {
316289
if s == "majority" {
@@ -322,6 +295,21 @@ impl From<String> for Acknowledgment {
322295
}
323296

324297
impl WriteConcern {
298+
/// A 'WriteConcern' requesting [`Acknowledgment::Nodes`].
299+
pub fn nodes(v: u32) -> Self {
300+
Acknowledgment::Nodes(v).into()
301+
}
302+
303+
/// A `WriteConcern` requesting [`Acknowledgment::Majority`].
304+
pub fn majority() -> Self {
305+
Acknowledgment::Majority.into()
306+
}
307+
308+
/// A `WriteConcern` with a custom acknowledgment.
309+
pub fn custom(s: impl AsRef<str>) -> Self {
310+
Acknowledgment::from(s.as_ref()).into()
311+
}
312+
325313
pub(crate) fn is_acknowledged(&self) -> bool {
326314
self.w != Some(Acknowledgment::Nodes(0)) || self.journal == Some(true)
327315
}
@@ -381,3 +369,13 @@ impl WriteConcern {
381369
state.serialize(serializer)
382370
}
383371
}
372+
373+
impl From<Acknowledgment> for WriteConcern {
374+
fn from(w: Acknowledgment) -> Self {
375+
WriteConcern {
376+
w: Some(w),
377+
w_timeout: None,
378+
journal: None,
379+
}
380+
}
381+
}

src/test/csfle.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ async fn init_client() -> Result<(EventClient, Collection<Document>)> {
7373
.collection_with_options::<Document>(
7474
"datakeys",
7575
CollectionOptions::builder()
76-
.read_concern(ReadConcern::MAJORITY)
77-
.write_concern(WriteConcern::MAJORITY)
76+
.read_concern(ReadConcern::majority())
77+
.write_concern(WriteConcern::majority())
7878
.build(),
7979
);
8080
datakeys.drop(None).await?;
@@ -1588,7 +1588,7 @@ impl DeadlockTestCase {
15881588
.insert_one(
15891589
load_testdata("external/external-key.json")?,
15901590
InsertOneOptions::builder()
1591-
.write_concern(WriteConcern::MAJORITY)
1591+
.write_concern(WriteConcern::majority())
15921592
.build(),
15931593
)
15941594
.await?;
@@ -2357,7 +2357,7 @@ async fn explicit_encryption_setup() -> Result<Option<ExplicitEncryptionTestData
23572357
.insert_one(
23582358
key1_document,
23592359
InsertOneOptions::builder()
2360-
.write_concern(WriteConcern::MAJORITY)
2360+
.write_concern(WriteConcern::majority())
23612361
.build(),
23622362
)
23632363
.await?;
@@ -2504,7 +2504,7 @@ async fn unique_index_keyaltnames_setup() -> Result<(ClientEncryption, Binary)>
25042504
),
25052505
},
25062506
CreateIndexOptions::builder()
2507-
.write_concern(WriteConcern::MAJORITY)
2507+
.write_concern(WriteConcern::majority())
25082508
.build(),
25092509
)
25102510
.await?;
@@ -3260,7 +3260,7 @@ async fn range_explicit_encryption_test(
32603260
.insert_one(
32613261
key1_document,
32623262
InsertOneOptions::builder()
3263-
.write_concern(WriteConcern::MAJORITY)
3263+
.write_concern(WriteConcern::majority())
32643264
.build(),
32653265
)
32663266
.await?;

src/test/spec/v2_runner.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl TestContext {
142142
// Reset the test collection as needed
143143
#[allow(unused_mut)]
144144
let mut options = DropCollectionOptions::builder()
145-
.write_concern(WriteConcern::MAJORITY)
145+
.write_concern(WriteConcern::majority())
146146
.build();
147147
#[cfg(feature = "in-use-encryption-unstable")]
148148
if let Some(enc_fields) = &test_file.encrypted_fields {
@@ -158,7 +158,7 @@ impl TestContext {
158158

159159
#[allow(unused_mut)]
160160
let mut options = CreateCollectionOptions::builder()
161-
.write_concern(WriteConcern::MAJORITY)
161+
.write_concern(WriteConcern::majority())
162162
.build();
163163
#[cfg(feature = "in-use-encryption-unstable")]
164164
{
@@ -181,7 +181,7 @@ impl TestContext {
181181
TestData::Single(data) => {
182182
if !data.is_empty() {
183183
let options = InsertManyOptions::builder()
184-
.write_concern(WriteConcern::MAJORITY)
184+
.write_concern(WriteConcern::majority())
185185
.build();
186186
coll.insert_many(data.clone(), options).await.unwrap();
187187
}

src/test/spec/v2_runner/csfle.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub(crate) async fn populate_key_vault(client: &Client, kv_data: Option<&Vec<Doc
1717
.collection_with_options::<Document>(
1818
"datakeys",
1919
CollectionOptions::builder()
20-
.write_concern(WriteConcern::MAJORITY)
20+
.write_concern(WriteConcern::majority())
2121
.build(),
2222
);
2323
datakeys.drop(None).await.unwrap();

src/test/spec/v2_runner/operation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ impl TestOperation for AssertCollectionExists {
13071307
.database_with_options(
13081308
&self.database,
13091309
DatabaseOptions::builder()
1310-
.read_concern(ReadConcern::MAJORITY)
1310+
.read_concern(ReadConcern::majority())
13111311
.build(),
13121312
)
13131313
.list_collection_names(None)

src/test/spec/v2_runner/test_file.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ impl Outcome {
171171
let coll_opts = CollectionOptions::default();
172172
#[cfg(feature = "in-use-encryption-unstable")]
173173
let coll_opts = CollectionOptions::builder()
174-
.read_concern(crate::options::ReadConcern::LOCAL)
174+
.read_concern(crate::options::ReadConcern::local())
175175
.build();
176176
let coll = client
177177
.database(db_name)

0 commit comments

Comments
 (0)