Debian Codegraph @

src/macros.rs

raw
1 // BSON macro based on the serde_json json! implementation.
2
3 /// Construct a bson::BSON value from a literal.
4 ///
5 /// ```rust
6 /// # use bson::bson;
7 /// #
8 /// # fn main() {
9 /// let value = bson!({
10 /// "code": 200,
11 /// "success": true,
12 /// "payload": {
13 /// "some": [
14 /// "pay",
15 /// "loads",
16 /// ]
17 /// }
18 /// });
19 /// # }
20 /// ```
21 #[macro_export]
22 macro_rules! bson {
23 //////////////////////////////////////////////////////////////////////////
24 // TT muncher for parsing the inside of an array [...]. Produces a vec![...]
25 // of the elements.
26 //
27 // Must be invoked as: bson!(@array [] $($tt)*)
28 //////////////////////////////////////////////////////////////////////////
29
30 // Finished with trailing comma.
31 (@array [$($elems:expr,)*]) => {
32 vec![$($elems,)*]
33 };
34
35 // Finished without trailing comma.
36 (@array [$($elems:expr),*]) => {
37 vec![$($elems),*]
38 };
39
40 // Next element is `null`.
41 (@array [$($elems:expr,)*] null $($rest:tt)*) => {
42 $crate::bson!(@array [$($elems,)* $crate::bson!(null)] $($rest)*)
43 };
44
45 // Next element is an array.
46 (@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
47 $crate::bson!(@array [$($elems,)* $crate::bson!([$($array)*])] $($rest)*)
48 };
49
50 // Next element is a map.
51 (@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
52 $crate::bson!(@array [$($elems,)* $crate::bson!({$($map)*})] $($rest)*)
53 };
54
55 // Next element is an expression followed by comma.
56 (@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
57 $crate::bson!(@array [$($elems,)* $crate::bson!($next),] $($rest)*)
58 };
59
60 // Last element is an expression with no trailing comma.
61 (@array [$($elems:expr,)*] $last:expr) => {
62 $crate::bson!(@array [$($elems,)* $crate::bson!($last)])
63 };
64
65 // Comma after the most recent element.
66 (@array [$($elems:expr),*] , $($rest:tt)*) => {
67 $crate::bson!(@array [$($elems,)*] $($rest)*)
68 };
69
70 //////////////////////////////////////////////////////////////////////////
71 // TT muncher for parsing the inside of an object {...}. Each entry is
72 // inserted into the given map variable.
73 //
74 // Must be invoked as: bson!(@object $map () ($($tt)*) ($($tt)*))
75 //
76 // We require two copies of the input tokens so that we can match on one
77 // copy and trigger errors on the other copy.
78 //////////////////////////////////////////////////////////////////////////
79
80 // Finished.
81 (@object $object:ident () () ()) => {};
82
83 // Insert the current entry followed by trailing comma.
84 (@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
85 $object.insert::<_, $crate::Bson>(($($key)+), $value);
86 $crate::bson!(@object $object () ($($rest)*) ($($rest)*));
87 };
88
89 // Insert the last entry without trailing comma.
90 (@object $object:ident [$($key:tt)+] ($value:expr)) => {
91 $object.insert::<_, $crate::Bson>(($($key)+), $value);
92 };
93
94 // Next value is `null`.
95 (@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
96 $crate::bson!(@object $object [$($key)+] ($crate::bson!(null)) $($rest)*);
97 };
98
99 // Next value is an array.
100 (@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
101 $crate::bson!(@object $object [$($key)+] ($crate::bson!([$($array)*])) $($rest)*);
102 };
103
104 // Next value is a map.
105 (@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
106 $crate::bson!(@object $object [$($key)+] ($crate::bson!({$($map)*})) $($rest)*);
107 };
108
109 // Next value is an expression followed by comma.
110 (@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
111 $crate::bson!(@object $object [$($key)+] ($crate::bson!($value)) , $($rest)*);
112 };
113
114 // Last value is an expression with no trailing comma.
115 (@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
116 $crate::bson!(@object $object [$($key)+] ($crate::bson!($value)));
117 };
118
119 // Missing value for last entry. Trigger a reasonable error message.
120 (@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
121 // "unexpected end of macro invocation"
122 $crate::bson!();
123 };
124
125 // Missing key-value separator and value for last entry.
126 // Trigger a reasonable error message.
127 (@object $object:ident ($($key:tt)+) () $copy:tt) => {
128 // "unexpected end of macro invocation"
129 $crate::bson!();
130 };
131
132 // Misplaced key-value separator. Trigger a reasonable error message.
133 (@object $object:ident () (: $($rest:tt)*) ($kv_separator:tt $($copy:tt)*)) => {
134 // Takes no arguments so "no rules expected the token `:`".
135 unimplemented!($kv_separator);
136 };
137
138 // Found a comma inside a key. Trigger a reasonable error message.
139 (@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
140 // Takes no arguments so "no rules expected the token `,`".
141 unimplemented!($comma);
142 };
143
144 // Key is fully parenthesized. This avoids clippy double_parens false
145 // positives because the parenthesization may be necessary here.
146 (@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
147 $crate::bson!(@object $object ($key) (: $($rest)*) (: $($rest)*));
148 };
149
150 // Munch a token into the current key.
151 (@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
152 $crate::bson!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
153 };
154
155 //////////////////////////////////////////////////////////////////////////
156 // The main implementation.
157 //
158 // Must be invoked as: bson!($($bson)+)
159 //////////////////////////////////////////////////////////////////////////
160
161 (null) => {
162 $crate::Bson::Null
163 };
164
165 ([]) => {
166 $crate::Bson::Array(vec![])
167 };
168
169 ([ $($tt:tt)+ ]) => {
170 $crate::Bson::Array($crate::bson!(@array [] $($tt)+))
171 };
172
173 ({}) => {
174 $crate::Bson::Document($crate::doc!{})
175 };
176
177 ({$($tt:tt)+}) => {
178 $crate::Bson::Document($crate::doc!{$($tt)+})
179 };
180
181 // Any Into<Bson> type.
182 // Must be below every other rule.
183 ($other:expr) => {
184 <_ as ::std::convert::Into<$crate::Bson>>::into($other)
185 };
186 }
187
188 /// Construct a bson::Document value.
189 ///
190 /// ```rust
191 /// # use bson::doc;
192 /// #
193 /// # fn main() {
194 /// let value = doc! {
195 /// "code": 200,
196 /// "success": true,
197 /// "payload": {
198 /// "some": [
199 /// "pay",
200 /// "loads",
201 /// ]
202 /// }
203 /// };
204 /// # }
205 /// ```
206 #[macro_export]
207 macro_rules! doc {
208 () => {{ $crate::Document::new() }};
209 ( $($tt:tt)+ ) => {{
210 let mut object = $crate::Document::new();
211 $crate::bson!(@object object () ($($tt)+) ($($tt)+));
212 object
213 }};
214 }
215
216 /// Construct a [`crate::RawBson`] value from a literal.
217 ///
218 /// ```rust
219 /// use bson::rawbson;
220 ///
221 /// let value = rawbson!({
222 /// "code": 200,
223 /// "success": true,
224 /// "payload": {
225 /// "some": [
226 /// "pay",
227 /// "loads",
228 /// ]
229 /// }
230 /// });
231 /// ```
232 #[macro_export]
233 macro_rules! rawbson {
234 //////////////////////////////////////////////////////////////////////////
235 // TT muncher for parsing the inside of an array [...]. Produces a
236 // RawArrayBuf containing the elements.
237 //
238 // Must be invoked as: bson!(@array [] $($tt)*)
239 //////////////////////////////////////////////////////////////////////////
240
241 // Finished with trailing comma.
242 (@array [$($elems:expr,)*]) => {
243 <$crate::RawArrayBuf as std::iter::FromIterator::<$crate::RawBson>>::from_iter(vec![$($elems,)*])
244 };
245
246 // Finished without trailing comma.
247 (@array [$($elems:expr),*]) => {
248 <$crate::RawArrayBuf as std::iter::FromIterator::<$crate::RawBson>>::from_iter(vec![$($elems),*])
249 };
250
251 // Next element is `null`.
252 (@array [$($elems:expr,)*] null $($rest:tt)*) => {
253 $crate::rawbson!(@array [$($elems,)* $crate::rawbson!(null)] $($rest)*)
254 };
255
256 // Next element is an array.
257 (@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
258 $crate::rawbson!(@array [$($elems,)* $crate::rawbson!([$($array)*])] $($rest)*)
259 };
260
261 // Next element is a map.
262 (@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
263 $crate::rawbson!(@array [$($elems,)* $crate::rawbson!({$($map)*})] $($rest)*)
264 };
265
266 // Next element is an expression followed by comma.
267 (@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
268 $crate::rawbson!(@array [$($elems,)* $crate::rawbson!($next),] $($rest)*)
269 };
270
271 // Last element is an expression with no trailing comma.
272 (@array [$($elems:expr,)*] $last:expr) => {
273 $crate::rawbson!(@array [$($elems,)* $crate::rawbson!($last)])
274 };
275
276 // Comma after the most recent element.
277 (@array [$($elems:expr),*] , $($rest:tt)*) => {
278 $crate::rawbson!(@array [$($elems,)*] $($rest)*)
279 };
280
281 //////////////////////////////////////////////////////////////////////////
282 // TT muncher for parsing the inside of an object {...}. Each entry is
283 // inserted into the given map variable.
284 //
285 // Must be invoked as: rawbson!(@object $map () ($($tt)*) ($($tt)*))
286 //
287 // We require two copies of the input tokens so that we can match on one
288 // copy and trigger errors on the other copy.
289 //////////////////////////////////////////////////////////////////////////
290
291 // Finished.
292 (@object $object:ident () () ()) => {};
293
294 // Insert the current entry followed by trailing comma.
295 (@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
296 $object.append(($($key)+), $value);
297 $crate::rawbson!(@object $object () ($($rest)*) ($($rest)*));
298 };
299
300 // Insert the last entry without trailing comma.
301 (@object $object:ident [$($key:tt)+] ($value:expr)) => {
302 $object.append(($($key)+), $value);
303 };
304
305 // Next value is `null`.
306 (@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
307 $crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!(null)) $($rest)*);
308 };
309
310 // Next value is an array.
311 (@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
312 $crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!([$($array)*])) $($rest)*);
313 };
314
315 // Next value is a map.
316 (@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
317 $crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!({$($map)*})) $($rest)*);
318 };
319
320 // Next value is an expression followed by comma.
321 (@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
322 $crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!($value)) , $($rest)*);
323 };
324
325 // Last value is an expression with no trailing comma.
326 (@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
327 $crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!($value)));
328 };
329
330 // Missing value for last entry. Trigger a reasonable error message.
331 (@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
332 // "unexpected end of macro invocation"
333 $crate::rawbson!();
334 };
335
336 // Missing key-value separator and value for last entry.
337 // Trigger a reasonable error message.
338 (@object $object:ident ($($key:tt)+) () $copy:tt) => {
339 // "unexpected end of macro invocation"
340 $crate::rawbson!();
341 };
342
343 // Misplaced key-value separator. Trigger a reasonable error message.
344 (@object $object:ident () (: $($rest:tt)*) ($kv_separator:tt $($copy:tt)*)) => {
345 // Takes no arguments so "no rules expected the token `:`".
346 unimplemented!($kv_separator);
347 };
348
349 // Found a comma inside a key. Trigger a reasonable error message.
350 (@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
351 // Takes no arguments so "no rules expected the token `,`".
352 unimplemented!($comma);
353 };
354
355 // Key is fully parenthesized. This avoids clippy double_parens false
356 // positives because the parenthesization may be necessary here.
357 (@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
358 $crate::rawbson!(@object $object ($key) (: $($rest)*) (: $($rest)*));
359 };
360
361 // Munch a token into the current key.
362 (@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
363 $crate::rawbson!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
364 };
365
366 //////////////////////////////////////////////////////////////////////////
367 // The main implementation.
368 //
369 // Must be invoked as: rawbson!($($bson)+)
370 //////////////////////////////////////////////////////////////////////////
371
372 (null) => {
373 $crate::RawBson::Null
374 };
375
376 ([]) => {
377 $crate::RawBson::Array($crate::RawArrayBuf::new())
378 };
379
380 ([ $($tt:tt)+ ]) => {
381 $crate::RawBson::Array($crate::rawbson!(@array [] $($tt)+))
382 };
383
384 ({}) => {
385 $crate::RawBson::Document($crate::rawdoc!{})
386 };
387
388 ({$($tt:tt)+}) => {
389 $crate::RawBson::Document($crate::rawdoc!{$($tt)+})
390 };
391
392 // Any Into<RawBson> type.
393 // Must be below every other rule.
394 ($other:expr) => {
395 <_ as ::std::convert::Into<$crate::RawBson>>::into($other)
396 };
397 }
398
399 /// Construct a [`crate::RawDocumentBuf`] value.
400 ///
401 /// ```rust
402 /// use bson::rawdoc;
403 ///
404 /// let value = rawdoc! {
405 /// "code": 200,
406 /// "success": true,
407 /// "payload": {
408 /// "some": [
409 /// "pay",
410 /// "loads",
411 /// ]
412 /// }
413 /// };
414 /// ```
415 #[macro_export]
416 macro_rules! rawdoc {
417 () => {{ $crate::RawDocumentBuf::new() }};
418 ( $($tt:tt)+ ) => {{
419 let mut object = $crate::RawDocumentBuf::new();
420 $crate::rawbson!(@object object () ($($tt)+) ($($tt)+));
421 object
422 }};
423 }