Debian Codegraph @

src/extjson/models.rs

raw
1 //! A module defining serde models for the extended JSON representations of the various BSON types.
2
3 use serde::{
4 de::{Error, Unexpected},
5 Deserialize,
6 Serialize,
7 };
8 use std::borrow::Cow;
9
10 use crate::{extjson, oid, raw::serde::CowStr, spec::BinarySubtype, Bson};
11
12 #[derive(Deserialize)]
13 #[serde(deny_unknown_fields)]
14 pub(crate) struct Int32 {
15 #[serde(rename = "$numberInt")]
16 value: String,
17 }
18
19 impl Int32 {
20 pub(crate) fn parse(self) -> extjson::de::Result<i32> {
21 let i: i32 = self.value.parse().map_err(|_| {
22 extjson::de::Error::invalid_value(
23 Unexpected::Str(self.value.as_str()),
24 &"i32 as a string",
25 )
26 })?;
27 Ok(i)
28 }
29 }
30
31 #[derive(Deserialize, Serialize)]
32 #[serde(deny_unknown_fields)]
33 pub(crate) struct Int64 {
34 #[serde(rename = "$numberLong")]
35 value: String,
36 }
37
38 impl Int64 {
39 pub(crate) fn parse(self) -> extjson::de::Result<i64> {
40 let i: i64 = self.value.parse().map_err(|_| {
41 extjson::de::Error::invalid_value(
42 Unexpected::Str(self.value.as_str()),
43 &"i64 as a string",
44 )
45 })?;
46 Ok(i)
47 }
48 }
49
50 #[derive(Deserialize)]
51 #[serde(deny_unknown_fields)]
52 pub(crate) struct Double {
53 #[serde(rename = "$numberDouble")]
54 value: String,
55 }
56
57 impl Double {
58 pub(crate) fn parse(self) -> extjson::de::Result<f64> {
59 match self.value.as_str() {
60 "Infinity" => Ok(std::f64::INFINITY),
61 "-Infinity" => Ok(std::f64::NEG_INFINITY),
62 "NaN" => Ok(std::f64::NAN),
63 other => {
64 let d: f64 = other.parse().map_err(|_| {
65 extjson::de::Error::invalid_value(
66 Unexpected::Str(other),
67 &"bson double as string",
68 )
69 })?;
70 Ok(d)
71 }
72 }
73 }
74 }
75
76 #[derive(Deserialize)]
77 #[serde(deny_unknown_fields)]
78 pub(crate) struct Decimal128 {
79 #[serde(rename = "$numberDecimal")]
80 value: String,
81 }
82
83 impl Decimal128 {
84 pub(crate) fn parse(self) -> extjson::de::Result<crate::Decimal128> {
85 self.value.parse().map_err(|_| {
86 extjson::de::Error::invalid_value(
87 Unexpected::Str(&self.value),
88 &"bson decimal128 as string",
89 )
90 })
91 }
92 }
93
94 #[derive(Serialize, Deserialize)]
95 #[serde(deny_unknown_fields)]
96 pub(crate) struct ObjectId {
97 #[serde(rename = "$oid")]
98 oid: String,
99 }
100
101 impl ObjectId {
102 pub(crate) fn parse(self) -> extjson::de::Result<oid::ObjectId> {
103 let oid = oid::ObjectId::parse_str(self.oid.as_str())?;
104 Ok(oid)
105 }
106 }
107
108 impl From<crate::oid::ObjectId> for ObjectId {
109 fn from(id: crate::oid::ObjectId) -> Self {
110 Self { oid: id.to_hex() }
111 }
112 }
113
114 #[derive(Deserialize)]
115 #[serde(deny_unknown_fields)]
116 pub(crate) struct Symbol {
117 #[serde(rename = "$symbol")]
118 pub(crate) value: String,
119 }
120
121 #[derive(Deserialize)]
122 #[serde(deny_unknown_fields)]
123 pub(crate) struct Regex {
124 #[serde(rename = "$regularExpression")]
125 body: RegexBody,
126 }
127
128 #[derive(Serialize, Deserialize)]
129 #[serde(deny_unknown_fields)]
130 pub(crate) struct RegexBody {
131 pub(crate) pattern: String,
132 pub(crate) options: String,
133 }
134
135 impl Regex {
136 pub(crate) fn parse(self) -> crate::Regex {
137 crate::Regex::new(self.body.pattern, self.body.options)
138 }
139 }
140
141 #[derive(Deserialize)]
142 #[serde(deny_unknown_fields)]
143 pub(crate) struct Binary {
144 #[serde(rename = "$binary")]
145 pub(crate) body: BinaryBody,
146 }
147
148 #[derive(Deserialize, Serialize)]
149 #[serde(deny_unknown_fields)]
150 pub(crate) struct BinaryBody {
151 pub(crate) base64: String,
152
153 #[serde(rename = "subType")]
154 pub(crate) subtype: String,
155 }
156
157 impl Binary {
158 pub(crate) fn parse(self) -> extjson::de::Result<crate::Binary> {
159 let bytes = base64::decode(self.body.base64.as_str()).map_err(|_| {
160 extjson::de::Error::invalid_value(
161 Unexpected::Str(self.body.base64.as_str()),
162 &"base64 encoded bytes",
163 )
164 })?;
165
166 let subtype = hex::decode(self.body.subtype.as_str()).map_err(|_| {
167 extjson::de::Error::invalid_value(
168 Unexpected::Str(self.body.subtype.as_str()),
169 &"hexadecimal number as a string",
170 )
171 })?;
172
173 if subtype.len() == 1 {
174 Ok(crate::Binary {
175 bytes,
176 subtype: subtype[0].into(),
177 })
178 } else {
179 Err(extjson::de::Error::invalid_value(
180 Unexpected::Bytes(subtype.as_slice()),
181 &"one byte subtype",
182 ))
183 }
184 }
185 }
186
187 #[derive(Deserialize)]
188 #[serde(deny_unknown_fields)]
189 pub(crate) struct Uuid {
190 #[serde(rename = "$uuid")]
191 value: String,
192 }
193
194 impl Uuid {
195 pub(crate) fn parse(self) -> extjson::de::Result<crate::Binary> {
196 let uuid = uuid::Uuid::parse_str(&self.value).map_err(|_| {
197 extjson::de::Error::invalid_value(
198 Unexpected::Str(&self.value),
199 &"$uuid value does not follow RFC 4122 format regarding length and hyphens",
200 )
201 })?;
202
203 Ok(crate::Binary {
204 subtype: BinarySubtype::Uuid,
205 bytes: uuid.as_bytes().to_vec(),
206 })
207 }
208 }
209
210 #[derive(Deserialize)]
211 #[serde(deny_unknown_fields)]
212 pub(crate) struct JavaScriptCodeWithScope {
213 #[serde(rename = "$code")]
214 pub(crate) code: String,
215
216 #[serde(rename = "$scope")]
217 #[serde(default)]
218 pub(crate) scope: Option<serde_json::Map<String, serde_json::Value>>,
219 }
220
221 #[derive(Deserialize)]
222 #[serde(deny_unknown_fields)]
223 pub(crate) struct Timestamp {
224 #[serde(rename = "$timestamp")]
225 body: TimestampBody,
226 }
227
228 #[derive(Serialize, Deserialize, Debug)]
229 #[serde(deny_unknown_fields)]
230 pub(crate) struct TimestampBody {
231 #[serde(serialize_with = "crate::serde_helpers::serialize_u32_as_i64")]
232 pub(crate) t: u32,
233
234 #[serde(serialize_with = "crate::serde_helpers::serialize_u32_as_i64")]
235 pub(crate) i: u32,
236 }
237
238 impl Timestamp {
239 pub(crate) fn parse(self) -> crate::Timestamp {
240 crate::Timestamp {
241 time: self.body.t,
242 increment: self.body.i,
243 }
244 }
245 }
246
247 #[derive(Serialize, Deserialize)]
248 #[serde(deny_unknown_fields)]
249 pub(crate) struct DateTime {
250 #[serde(rename = "$date")]
251 pub(crate) body: DateTimeBody,
252 }
253
254 #[derive(Deserialize, Serialize)]
255 #[serde(untagged)]
256 pub(crate) enum DateTimeBody {
257 Canonical(Int64),
258 Relaxed(String),
259 }
260
261 impl DateTimeBody {
262 pub(crate) fn from_millis(m: i64) -> Self {
263 DateTimeBody::Canonical(Int64 {
264 value: m.to_string(),
265 })
266 }
267 }
268
269 impl DateTime {
270 pub(crate) fn parse(self) -> extjson::de::Result<crate::DateTime> {
271 match self.body {
272 DateTimeBody::Canonical(date) => {
273 let date = date.parse()?;
274 Ok(crate::DateTime::from_millis(date))
275 }
276 DateTimeBody::Relaxed(date) => {
277 let datetime = crate::DateTime::parse_rfc3339_str(date.as_str()).map_err(|_| {
278 extjson::de::Error::invalid_value(
279 Unexpected::Str(date.as_str()),
280 &"rfc3339 formatted utc datetime",
281 )
282 })?;
283 Ok(datetime)
284 }
285 }
286 }
287 }
288
289 #[derive(Deserialize)]
290 #[serde(deny_unknown_fields)]
291 pub(crate) struct MinKey {
292 #[serde(rename = "$minKey")]
293 pub(crate) value: u8,
294 }
295
296 impl MinKey {
297 pub(crate) fn parse(self) -> extjson::de::Result<Bson> {
298 if self.value == 1 {
299 Ok(Bson::MinKey)
300 } else {
301 Err(extjson::de::Error::invalid_value(
302 Unexpected::Unsigned(self.value as u64),
303 &"value of $minKey should always be 1",
304 ))
305 }
306 }
307 }
308
309 #[derive(Deserialize)]
310 #[serde(deny_unknown_fields)]
311 pub(crate) struct MaxKey {
312 #[serde(rename = "$maxKey")]
313 pub(crate) value: u8,
314 }
315
316 impl MaxKey {
317 pub(crate) fn parse(self) -> extjson::de::Result<Bson> {
318 if self.value == 1 {
319 Ok(Bson::MaxKey)
320 } else {
321 Err(extjson::de::Error::invalid_value(
322 Unexpected::Unsigned(self.value as u64),
323 &"value of $maxKey should always be 1",
324 ))
325 }
326 }
327 }
328
329 #[derive(Deserialize)]
330 #[serde(deny_unknown_fields)]
331 pub(crate) struct DbPointer {
332 #[serde(rename = "$dbPointer")]
333 body: DbPointerBody,
334 }
335
336 #[derive(Serialize, Deserialize)]
337 #[serde(deny_unknown_fields)]
338 pub(crate) struct DbPointerBody {
339 #[serde(rename = "$ref")]
340 pub(crate) ref_ns: String,
341
342 #[serde(rename = "$id")]
343 pub(crate) id: ObjectId,
344 }
345
346 impl DbPointer {
347 pub(crate) fn parse(self) -> extjson::de::Result<crate::DbPointer> {
348 Ok(crate::DbPointer {
349 namespace: self.body.ref_ns,
350 id: self.body.id.parse()?,
351 })
352 }
353 }
354
355 #[derive(Deserialize)]
356 #[serde(deny_unknown_fields)]
357 pub(crate) struct Undefined {
358 #[serde(rename = "$undefined")]
359 pub(crate) value: bool,
360 }
361
362 impl Undefined {
363 pub(crate) fn parse(self) -> extjson::de::Result<Bson> {
364 if self.value {
365 Ok(Bson::Undefined)
366 } else {
367 Err(extjson::de::Error::invalid_value(
368 Unexpected::Bool(false),
369 &"$undefined should always be true",
370 ))
371 }
372 }
373 }
374
375 #[derive(Debug, Deserialize)]
376 pub(crate) struct BorrowedRegexBody<'a> {
377 #[serde(borrow)]
378 pub(crate) pattern: Cow<'a, str>,
379
380 #[serde(borrow)]
381 pub(crate) options: Cow<'a, str>,
382 }
383
384 #[derive(Debug, Deserialize)]
385 pub(crate) struct BorrowedBinaryBody<'a> {
386 #[serde(borrow)]
387 pub(crate) bytes: Cow<'a, [u8]>,
388
389 #[serde(rename = "subType")]
390 pub(crate) subtype: u8,
391 }
392
393 #[derive(Deserialize)]
394 pub(crate) struct BorrowedDbPointerBody<'a> {
395 #[serde(rename = "$ref")]
396 #[serde(borrow)]
397 pub(crate) ns: CowStr<'a>,
398
399 #[serde(rename = "$id")]
400 pub(crate) id: oid::ObjectId,
401 }