1#[macro_export]
22macro_rules! bson {
23 (@array [$($elems:expr,)*]) => {
32 vec![$($elems,)*]
33 };
34
35 (@array [$($elems:expr),*]) => {
37 vec![$($elems),*]
38 };
39
40 (@array [$($elems:expr,)*] null $($rest:tt)*) => {
42 $crate::bson!(@array [$($elems,)* $crate::bson!(null)] $($rest)*)
43 };
44
45 (@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
47 $crate::bson!(@array [$($elems,)* $crate::bson!([$($array)*])] $($rest)*)
48 };
49
50 (@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
52 $crate::bson!(@array [$($elems,)* $crate::bson!({$($map)*})] $($rest)*)
53 };
54
55 (@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
57 $crate::bson!(@array [$($elems,)* $crate::bson!($next),] $($rest)*)
58 };
59
60 (@array [$($elems:expr,)*] $last:expr) => {
62 $crate::bson!(@array [$($elems,)* $crate::bson!($last)])
63 };
64
65 (@array [$($elems:expr),*] , $($rest:tt)*) => {
67 $crate::bson!(@array [$($elems,)*] $($rest)*)
68 };
69
70 (@object $object:ident () () ()) => {};
82
83 (@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 (@object $object:ident [$($key:tt)+] ($value:expr)) => {
91 $object.insert::<_, $crate::Bson>(($($key)+), $value);
92 };
93
94 (@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
96 $crate::bson!(@object $object [$($key)+] ($crate::bson!(null)) $($rest)*);
97 };
98
99 (@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
101 $crate::bson!(@object $object [$($key)+] ($crate::bson!([$($array)*])) $($rest)*);
102 };
103
104 (@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
106 $crate::bson!(@object $object [$($key)+] ($crate::bson!({$($map)*})) $($rest)*);
107 };
108
109 (@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
111 $crate::bson!(@object $object [$($key)+] ($crate::bson!($value)) , $($rest)*);
112 };
113
114 (@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
116 $crate::bson!(@object $object [$($key)+] ($crate::bson!($value)));
117 };
118
119 (@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
121 $crate::bson!();
123 };
124
125 (@object $object:ident ($($key:tt)+) () $copy:tt) => {
128 $crate::bson!();
130 };
131
132 (@object $object:ident () (: $($rest:tt)*) ($kv_separator:tt $($copy:tt)*)) => {
134 unimplemented!($kv_separator);
136 };
137
138 (@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
140 unimplemented!($comma);
142 };
143
144 (@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
147 $crate::bson!(@object $object ($key) (: $($rest)*) (: $($rest)*));
148 };
149
150 (@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
152 $crate::bson!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
153 };
154
155 (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 ($other:expr) => {
184 <_ as ::std::convert::Into<$crate::Bson>>::into($other)
185 };
186}
187
188#[macro_export]
207macro_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#[macro_export]
233macro_rules! rawbson {
234 (@array [$($elems:expr,)*]) => {
243 $crate::RawArrayBuf::from_iter(vec![$($elems,)*])
244 };
245
246 (@array [$($elems:expr),*]) => {
248 $crate::RawArrayBuf::from_iter(vec![$($elems),*])
249 };
250
251 (@array [$($elems:expr,)*] null $($rest:tt)*) => {
253 $crate::rawbson!(@array [$($elems,)* $crate::rawbson!(null)] $($rest)*)
254 };
255
256 (@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
258 $crate::rawbson!(@array [$($elems,)* $crate::rawbson!([$($array)*])] $($rest)*)
259 };
260
261 (@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
263 $crate::rawbson!(@array [$($elems,)* $crate::rawbson!({$($map)*})] $($rest)*)
264 };
265
266 (@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
268 $crate::rawbson!(@array [$($elems,)* $crate::rawbson!($next),] $($rest)*)
269 };
270
271 (@array [$($elems:expr,)*] $last:expr) => {
273 $crate::rawbson!(@array [$($elems,)* $crate::rawbson!($last)])
274 };
275
276 (@array [$($elems:expr),*] , $($rest:tt)*) => {
278 $crate::rawbson!(@array [$($elems,)*] $($rest)*)
279 };
280
281 (@object $object:ident () () ()) => {};
293
294 (@object $object:ident [$key:literal] ($value:expr) , $($rest:tt)*) => {{
296 $object.append($crate::raw::cstr!($key), $value);
297 $crate::rawbson!(@object $object () ($($rest)*) ($($rest)*));
298 }};
299
300 (@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {{
302 $object.append($($key)+, $value);
303 $crate::rawbson!(@object $object () ($($rest)*) ($($rest)*));
304 }};
305
306 (@object $object:ident [$key:literal] ($value:expr)) => {
308 $object.append($crate::raw::cstr!($key), $value);
309 };
310
311 (@object $object:ident [$($key:tt)+] ($value:expr)) => {
313 $object.append($($key)+, $value);
314 };
315
316 (@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
318 $crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!(null)) $($rest)*);
319 };
320
321 (@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
323 $crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!([$($array)*])) $($rest)*);
324 };
325
326 (@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
328 $crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!({$($map)*})) $($rest)*);
329 };
330
331 (@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
333 $crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!($value)) , $($rest)*);
334 };
335
336 (@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
338 $crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!($value)));
339 };
340
341 (@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
343 $crate::rawbson!();
345 };
346
347 (@object $object:ident ($($key:tt)+) () $copy:tt) => {
350 $crate::rawbson!();
352 };
353
354 (@object $object:ident () (: $($rest:tt)*) ($kv_separator:tt $($copy:tt)*)) => {
356 unimplemented!($kv_separator);
358 };
359
360 (@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
362 unimplemented!($comma);
364 };
365
366 (@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
369 $crate::rawbson!(@object $object ($key) (: $($rest)*) (: $($rest)*));
370 };
371
372 (@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
374 $crate::rawbson!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
375 };
376
377 (null) => {
384 $crate::RawBson::Null
385 };
386
387 ([]) => {
388 $crate::RawBson::Array($crate::RawArrayBuf::new())
389 };
390
391 ([ $($tt:tt)+ ]) => {
392 $crate::RawBson::Array($crate::rawbson!(@array [] $($tt)+))
393 };
394
395 ({}) => {
396 $crate::RawBson::Document($crate::rawdoc!{})
397 };
398
399 ({$($tt:tt)+}) => {
400 $crate::RawBson::Document($crate::rawdoc!{$($tt)+})
401 };
402
403 ($other:expr) => {
406 <_ as ::std::convert::Into<$crate::RawBson>>::into($other)
407 };
408}
409
410#[macro_export]
427macro_rules! rawdoc {
428 () => {{ $crate::RawDocumentBuf::new() }};
429 ( $($tt:tt)+ ) => {{
430 let mut object = $crate::RawDocumentBuf::new();
431 $crate::rawbson!(@object object () ($($tt)+) ($($tt)+));
432 object
433 }};
434}
435
436#[cfg(feature = "serde")]
443macro_rules! serde_conv_doc {
444 ($(#[$meta:meta])* $vis:vis $m:ident, $t:ty, $ser:expr, $de:expr) => {
445 #[allow(non_camel_case_types)]
446 $(#[$meta])*
447 $vis struct $m;
448
449 #[allow(clippy::all)]
453 #[allow(missing_docs)]
454 const _:() = {
455 impl $m {
456 $vis fn serialize<S>(x: &$t, serializer: S) -> Result<S::Ok, S::Error>
457 where
458 S: Serializer,
459 {
460 let y = $ser(x).map_err(serde::ser::Error::custom)?;
461 Serialize::serialize(&y, serializer)
462 }
463
464 $vis fn deserialize<'de, D>(deserializer: D) -> Result<$t, D::Error>
465 where
466 D: Deserializer<'de>,
467 {
468 let y = Deserialize::deserialize(deserializer)?;
469 $de(y).map_err(serde::de::Error::custom)
470 }
471 }
472
473 #[cfg(feature = "serde_with-3")]
474 impl serde_with::SerializeAs<$t> for $m {
475 fn serialize_as<S>(x: &$t, serializer: S) -> Result<S::Ok, S::Error>
476 where
477 S: Serializer,
478 {
479 Self::serialize(x, serializer)
480 }
481 }
482
483 #[cfg(feature = "serde_with-3")]
484 impl<'de> serde_with::DeserializeAs<'de, $t> for $m {
485 fn deserialize_as<D>(deserializer: D) -> Result<$t, D::Error>
486 where
487 D: Deserializer<'de>,
488 {
489 Self::deserialize(deserializer)
490 }
491 }
492 };
493 };
494}
495
496#[cfg(feature = "serde")]
497pub(crate) use serde_conv_doc;