-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathcreate.rs
64 lines (53 loc) · 1.53 KB
/
create.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use bson::rawdoc;
use crate::{
cmap::{Command, RawCommandResponse, StreamDescription},
error::Result,
operation::{
append_options_to_raw_document,
remove_empty_write_concern,
OperationWithDefaults,
WriteConcernOnlyBody,
},
options::{CreateCollectionOptions, WriteConcern},
Namespace,
};
use super::ExecutionContext;
#[derive(Debug)]
pub(crate) struct Create {
ns: Namespace,
options: Option<CreateCollectionOptions>,
}
impl Create {
pub(crate) fn new(ns: Namespace, options: Option<CreateCollectionOptions>) -> Self {
Self { ns, options }
}
}
impl OperationWithDefaults for Create {
type O = ();
const NAME: &'static str = "create";
fn build(&mut self, _description: &StreamDescription) -> Result<Command> {
let mut body = rawdoc! {
Self::NAME: self.ns.coll.clone(),
};
remove_empty_write_concern!(self.options);
append_options_to_raw_document(&mut body, self.options.as_ref())?;
Ok(Command::new(
Self::NAME.to_string(),
self.ns.db.clone(),
body,
))
}
fn handle_response<'a>(
&'a self,
response: RawCommandResponse,
_context: ExecutionContext<'a>,
) -> Result<Self::O> {
let response: WriteConcernOnlyBody = response.body()?;
response.validate()
}
fn write_concern(&self) -> Option<&WriteConcern> {
self.options
.as_ref()
.and_then(|opts| opts.write_concern.as_ref())
}
}