async_graphql_value/
macros.rs#[macro_export]
macro_rules! value {
($($json:tt)+) => {
$crate::value_internal!($($json)+)
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! value_internal {
(@array [$($elems:expr,)*]) => {
$crate::value_internal_vec![$($elems,)*]
};
(@array [$($elems:expr),*]) => {
$crate::value_internal_vec![$($elems),*]
};
(@array [$($elems:expr,)*] null $($rest:tt)*) => {
$crate::value_internal!(@array [$($elems,)* $crate::value_internal!(null)] $($rest)*)
};
(@array [$($elems:expr,)*] true $($rest:tt)*) => {
$crate::value_internal!(@array [$($elems,)* $crate::value_internal!(true)] $($rest)*)
};
(@array [$($elems:expr,)*] false $($rest:tt)*) => {
$crate::value_internal!(@array [$($elems,)* $crate::value_internal!(false)] $($rest)*)
};
(@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
$crate::value_internal!(@array [$($elems,)* $crate::value_internal!([$($array)*])] $($rest)*)
};
(@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
$crate::value_internal!(@array [$($elems,)* $crate::value_internal!({$($map)*})] $($rest)*)
};
(@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
$crate::value_internal!(@array [$($elems,)* $crate::value_internal!($next),] $($rest)*)
};
(@array [$($elems:expr,)*] $last:expr) => {
$crate::value_internal!(@array [$($elems,)* $crate::value_internal!($last)])
};
(@array [$($elems:expr),*] , $($rest:tt)*) => {
$crate::value_internal!(@array [$($elems,)*] $($rest)*)
};
(@array [$($elems:expr),*] $unexpected:tt $($rest:tt)*) => {
$crate::value_unexpected!($unexpected)
};
(@object $object:ident () () ()) => {};
(@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
let _ = $object.insert($crate::Name::new($($key)+), $value);
$crate::value_internal!(@object $object () ($($rest)*) ($($rest)*));
};
(@object $object:ident [$($key:tt)+] ($value:expr) $unexpected:tt $($rest:tt)*) => {
$crate::value_unexpected!($unexpected);
};
(@object $object:ident [$($key:tt)+] ($value:expr)) => {
let _ = $object.insert($crate::Name::new($($key)+), $value);
};
(@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
$crate::value_internal!(@object $object [$($key)+] ($crate::value_internal!(null)) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: true $($rest:tt)*) $copy:tt) => {
$crate::value_internal!(@object $object [$($key)+] ($crate::value_internal!(true)) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: false $($rest:tt)*) $copy:tt) => {
$crate::value_internal!(@object $object [$($key)+] ($crate::value_internal!(false)) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
$crate::value_internal!(@object $object [$($key)+] ($crate::value_internal!([$($array)*])) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
$crate::value_internal!(@object $object [$($key)+] ($crate::value_internal!({$($map)*})) $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
$crate::value_internal!(@object $object [$($key)+] ($crate::value_internal!($value)) , $($rest)*);
};
(@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
$crate::value_internal!(@object $object [$($key)+] ($crate::value_internal!($value)));
};
(@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
$crate::value_internal!();
};
(@object $object:ident ($($key:tt)+) () $copy:tt) => {
$crate::value_internal!();
};
(@object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
$crate::value_unexpected!($colon);
};
(@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
$crate::value_unexpected!($comma);
};
(@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
$crate::value_internal!(@object $object ($key) (: $($rest)*) (: $($rest)*));
};
(@object $object:ident ($($key:tt)*) (: $($unexpected:tt)+) $copy:tt) => {
$crate::value_expect_expr_comma!($($unexpected)+);
};
(@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
$crate::value_internal!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
};
(null) => {
$crate::ConstValue::Null
};
(true) => {
$crate::ConstValue::Boolean(true)
};
(false) => {
$crate::ConstValue::Boolean(false)
};
([]) => {
$crate::ConstValue::List($crate::value_internal_vec![])
};
([ $($tt:tt)+ ]) => {
$crate::ConstValue::List($crate::value_internal!(@array [] $($tt)+))
};
({}) => {
$crate::ConstValue::Object(Default::default())
};
({ $($tt:tt)+ }) => {
$crate::ConstValue::Object({
let mut object = $crate::indexmap::IndexMap::new();
$crate::value_internal!(@object object () ($($tt)+) ($($tt)+));
object
})
};
($other:expr) => {
$crate::to_value(&$other).unwrap()
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! value_internal_vec {
($($content:tt)*) => {
vec![$($content)*]
};
}
#[macro_export]
#[doc(hidden)]
macro_rules! value_unexpected {
() => {};
}
#[macro_export]
#[doc(hidden)]
macro_rules! value_expect_expr_comma {
($e:expr , $($tt:tt)*) => {};
}
#[cfg(test)]
mod tests {
use indexmap::IndexMap;
use crate::{ConstValue, Name};
#[test]
fn test_macro() {
assert_eq!(value!(1), ConstValue::Number(1.into()));
assert_eq!(value!(1 + 2), ConstValue::Number(3.into()));
assert_eq!(value!("abc"), ConstValue::String("abc".into()));
assert_eq!(value!(true), ConstValue::Boolean(true));
assert_eq!(
value!([1, 2, 3]),
ConstValue::List((1..=3).map(|n| ConstValue::Number(n.into())).collect())
);
assert_eq!(
value!([1, 2, 3,]),
ConstValue::List((1..=3).map(|n| ConstValue::Number(n.into())).collect())
);
assert_eq!(value!({"a": 10, "b": true}), {
let mut map = IndexMap::new();
map.insert(Name::new("a"), ConstValue::Number(10.into()));
map.insert(Name::new("b"), ConstValue::Boolean(true));
ConstValue::Object(map)
});
}
}