#[macro_export]
macro_rules! bson {
(@array [$($elems:expr,)*]) => {
vec![$($elems,)*]
};
(@array [$($elems:expr),*]) => {
vec![$($elems),*]
};
(@array [$($elems:expr,)*] null $($rest:tt)*) => {
$crate::bson!(@array [$($elems,)* $crate::bson!(null)] $($rest)*)
};
(@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
$crate::bson!(@array [$($elems,)* $crate::bson!([$($array)*])] $($rest)*)
};
(@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
$crate::bson!(@array [$($elems,)* $crate::bson!({$($map)*})] $($rest)*)
};
(@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
$crate::bson!(@array [$($elems,)* $crate::bson!($next),] $($rest)*)
};
(@array [$($elems:expr,)*] $last:expr) => {
$crate::bson!(@array [$($elems,)* $crate::bson!($last)])
};
(@array [$($elems:expr),*] , $($rest:tt)*) => {
$crate::bson!(@array [$($elems,)*] $($rest)*)
};
(@object $object:ident () () ()) => {};
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
$object.insert::<_, $crate::Bson>(($($key)+), $value);
$crate::bson!(@object $object () ($($rest)*) ($($rest)*));
};
(@object $object:ident [$($key:tt)+] ($value:expr)) => {
$object.insert::<_, $crate::Bson>(($($key)+), $value);
};
(@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!(null)) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!([$($array)*])) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!({$($map)*})) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!($value)) , $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
$crate::bson!(@object $object [$($key)+] ($crate::bson!($value)));
};
(@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
$crate::bson!();
};
(@object $object:ident ($($key:tt)+) () $copy:tt) => {
$crate::bson!();
};
(@object $object:ident () (: $($rest:tt)*) ($kv_separator:tt $($copy:tt)*)) => {
unimplemented!($kv_separator);
};
(@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
unimplemented!($comma);
};
(@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object ($key) (: $($rest)*) (: $($rest)*));
};
(@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
$crate::bson!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
};
(null) => {
$crate::Bson::Null
};
([]) => {
$crate::Bson::Array(vec![])
};
([ $($tt:tt)+ ]) => {
$crate::Bson::Array($crate::bson!(@array [] $($tt)+))
};
({}) => {
$crate::Bson::Document($crate::doc!{})
};
({$($tt:tt)+}) => {
$crate::Bson::Document($crate::doc!{$($tt)+})
};
($other:expr) => {
<_ as ::std::convert::Into<$crate::Bson>>::into($other)
};
}
#[macro_export]
macro_rules! doc {
() => {{ $crate::Document::new() }};
( $($tt:tt)+ ) => {{
let mut object = $crate::Document::new();
$crate::bson!(@object object () ($($tt)+) ($($tt)+));
object
}};
}
#[macro_export]
macro_rules! rawbson {
(@array [$($elems:expr,)*]) => {
$crate::RawArrayBuf::from_iter(vec![$($elems,)*])
};
(@array [$($elems:expr),*]) => {
$crate::RawArrayBuf::from_iter(vec![$($elems),*])
};
(@array [$($elems:expr,)*] null $($rest:tt)*) => {
$crate::rawbson!(@array [$($elems,)* $crate::rawbson!(null)] $($rest)*)
};
(@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
$crate::rawbson!(@array [$($elems,)* $crate::rawbson!([$($array)*])] $($rest)*)
};
(@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
$crate::rawbson!(@array [$($elems,)* $crate::rawbson!({$($map)*})] $($rest)*)
};
(@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
$crate::rawbson!(@array [$($elems,)* $crate::rawbson!($next),] $($rest)*)
};
(@array [$($elems:expr,)*] $last:expr) => {
$crate::rawbson!(@array [$($elems,)* $crate::rawbson!($last)])
};
(@array [$($elems:expr),*] , $($rest:tt)*) => {
$crate::rawbson!(@array [$($elems,)*] $($rest)*)
};
(@object $object:ident () () ()) => {};
(@object $object:ident [$key:literal] ($value:expr) , $($rest:tt)*) => {{
$object.append($crate::raw::cstr!($key), $value);
$crate::rawbson!(@object $object () ($($rest)*) ($($rest)*));
}};
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {{
$object.append($($key)+, $value);
$crate::rawbson!(@object $object () ($($rest)*) ($($rest)*));
}};
(@object $object:ident [$key:literal] ($value:expr)) => {
$object.append($crate::raw::cstr!($key), $value);
};
(@object $object:ident [$($key:tt)+] ($value:expr)) => {
$object.append($($key)+, $value);
};
(@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
$crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!(null)) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
$crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!([$($array)*])) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
$crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!({$($map)*})) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
$crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!($value)) , $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
$crate::rawbson!(@object $object [$($key)+] ($crate::rawbson!($value)));
};
(@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
$crate::rawbson!();
};
(@object $object:ident ($($key:tt)+) () $copy:tt) => {
$crate::rawbson!();
};
(@object $object:ident () (: $($rest:tt)*) ($kv_separator:tt $($copy:tt)*)) => {
unimplemented!($kv_separator);
};
(@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
unimplemented!($comma);
};
(@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
$crate::rawbson!(@object $object ($key) (: $($rest)*) (: $($rest)*));
};
(@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
$crate::rawbson!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
};
(null) => {
$crate::RawBson::Null
};
([]) => {
$crate::RawBson::Array($crate::RawArrayBuf::new())
};
([ $($tt:tt)+ ]) => {
$crate::RawBson::Array($crate::rawbson!(@array [] $($tt)+))
};
({}) => {
$crate::RawBson::Document($crate::rawdoc!{})
};
({$($tt:tt)+}) => {
$crate::RawBson::Document($crate::rawdoc!{$($tt)+})
};
($other:expr) => {
<_ as ::std::convert::Into<$crate::RawBson>>::into($other)
};
}
#[macro_export]
macro_rules! rawdoc {
() => {{ $crate::RawDocumentBuf::new() }};
( $($tt:tt)+ ) => {{
let mut object = $crate::RawDocumentBuf::new();
$crate::rawbson!(@object object () ($($tt)+) ($($tt)+));
object
}};
}
#[cfg(feature = "serde")]
macro_rules! serde_conv_doc {
($(#[$meta:meta])* $vis:vis $m:ident, $t:ty, $ser:expr, $de:expr) => {
#[allow(non_camel_case_types)]
$(#[$meta])*
$vis struct $m;
// Prevent clippy lints triggering because of the template here
#[allow(clippy::all)]
#[allow(missing_docs)]
const _:() = {
impl $m {
$vis fn serialize<S>(x: &$t, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let y = $ser(x).map_err(serde::ser::Error::custom)?;
Serialize::serialize(&y, serializer)
}
$vis fn deserialize<'de, D>(deserializer: D) -> Result<$t, D::Error>
where
D: Deserializer<'de>,
{
let y = Deserialize::deserialize(deserializer)?;
$de(y).map_err(serde::de::Error::custom)
}
}
#[cfg(feature = "serde_with-3")]
impl serde_with::SerializeAs<$t> for $m {
fn serialize_as<S>(x: &$t, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
Self::serialize(x, serializer)
}
}
#[cfg(feature = "serde_with-3")]
impl<'de> serde_with::DeserializeAs<'de, $t> for $m {
fn deserialize_as<D>(deserializer: D) -> Result<$t, D::Error>
where
D: Deserializer<'de>,
{
Self::deserialize(deserializer)
}
}
};
};
}
#[cfg(feature = "serde")]
pub(crate) use serde_conv_doc;