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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
use std::borrow::Cow;
use std::collections::{ BTreeSet, BTreeMap };
use bson::Document;
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "cargo-clippy", allow(stutter))]
#[serde(untagged)]
pub enum UpdateSpec {
Modify(Box<Modification>),
Replace(Document),
}
impl From<Modification> for UpdateSpec {
fn from(modification: Modification) -> Self {
UpdateSpec::Modify(Box::new(modification))
}
}
impl From<Document> for UpdateSpec {
fn from(doc: Document) -> Self {
UpdateSpec::Replace(doc)
}
}
#[derive(Debug, Default, Clone, PartialEq, Serialize, Deserialize)]
pub struct Modification {
#[serde(rename = "$set", default, skip_serializing_if = "Document::is_empty")]
pub set: Document,
#[serde(rename = "$unset", default, with = "serde_unset")]
pub unset: BTreeSet<Cow<'static, str>>,
#[serde(rename = "$currentDate", default, skip_serializing_if = "BTreeMap::is_empty")]
pub set_current_date: BTreeMap<Cow<'static, str>, DateTimeType>,
#[serde(rename = "$rename", default, skip_serializing_if = "BTreeMap::is_empty")]
pub rename: BTreeMap<Cow<'static, str>, Cow<'static, str>>,
#[serde(rename = "$inc", default, skip_serializing_if = "Document::is_empty")]
pub inc: Document,
#[serde(rename = "$mul", default, skip_serializing_if = "Document::is_empty")]
pub mul: Document,
#[serde(rename = "$min", default, skip_serializing_if = "Document::is_empty")]
pub min: Document,
#[serde(rename = "$max", default, skip_serializing_if = "Document::is_empty")]
pub max: Document,
#[serde(rename = "$addToSet", default, skip_serializing_if = "Document::is_empty")]
pub add_to_set: Document,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum DateTimeType {
Timestamp,
Date,
}
mod serde_unset {
use std::borrow::Cow;
use std::collections::BTreeSet;
use bson::Document;
use serde::ser::{ Serializer, SerializeMap };
use serde::de::{ Deserialize, Deserializer };
pub fn serialize<S: Serializer>(paths: &BTreeSet<Cow<'static, str>>, serializer: S) -> Result<S::Ok, S::Error> {
let mut map = serializer.serialize_map(Some(paths.len()))?;
for path in paths {
map.serialize_entry(path, &())?;
}
map.end()
}
pub fn deserialize<'a, D: Deserializer<'a>>(deserializer: D) -> Result<BTreeSet<Cow<'static, str>>, D::Error> {
Document::deserialize(deserializer).map(
|doc| doc.into_iter().map(|(key, _)| key.into()).collect()
)
}
}