1#[macro_export(local_inner_macros)]
53macro_rules! platform_value {
54 ($($platform_value:tt)+) => {
56 platform_value_internal!($($platform_value)+)
57 };
58}
59
60#[macro_export(local_inner_macros)]
63#[doc(hidden)]
64macro_rules! platform_value_internal {
65 (@array [$($elems:expr,)*]) => {
74 platform_value_internal_vec![$($elems,)*]
75 };
76
77 (@array [$($elems:expr),*]) => {
79 platform_value_internal_vec![$($elems),*]
80 };
81
82 (@array [$($elems:expr,)*] null $($rest:tt)*) => {
84 platform_value_internal!(@array [$($elems,)* platform_value_internal!(null)] $($rest)*)
85 };
86
87 (@array [$($elems:expr,)*] true $($rest:tt)*) => {
89 platform_value_internal!(@array [$($elems,)* platform_value_internal!(true)] $($rest)*)
90 };
91
92 (@array [$($elems:expr,)*] false $($rest:tt)*) => {
94 platform_value_internal!(@array [$($elems,)* platform_value_internal!(false)] $($rest)*)
95 };
96
97 (@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
99 platform_value_internal!(@array [$($elems,)* platform_value_internal!([$($array)*])] $($rest)*)
100 };
101
102 (@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
104 platform_value_internal!(@array [$($elems,)* platform_value_internal!({$($map)*})] $($rest)*)
105 };
106
107 (@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
109 platform_value_internal!(@array [$($elems,)* platform_value_internal!($next),] $($rest)*)
110 };
111
112 (@array [$($elems:expr,)*] $last:expr) => {
114 platform_value_internal!(@array [$($elems,)* platform_value_internal!($last)])
115 };
116
117 (@array [$($elems:expr),*] , $($rest:tt)*) => {
119 platform_value_internal!(@array [$($elems,)*] $($rest)*)
120 };
121
122 (@array [$($elems:expr),*] $unexpected:tt $($rest:tt)*) => {
124 platform_value_unexpected!($unexpected)
125 };
126
127 (@object $object:ident () () ()) => {};
139
140 (@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
142 let _ = $object.push((($($key)+).into(), $value));
143 platform_value_internal!(@object $object () ($($rest)*) ($($rest)*));
144 };
145
146 (@object $object:ident [$($key:tt)+] ($value:expr) $unexpected:tt $($rest:tt)*) => {
148 platform_value_unexpected!($unexpected);
149 };
150
151 (@object $object:ident [$($key:tt)+] ($value:expr)) => {
153 let _ = $object.push((($($key)+).into(), $value));
154 };
155
156 (@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
158 platform_value_internal!(@object $object [$($key)+] (platform_value_internal!(null)) $($rest)*);
159 };
160
161 (@object $object:ident ($($key:tt)+) (: true $($rest:tt)*) $copy:tt) => {
163 platform_value_internal!(@object $object [$($key)+] (platform_value_internal!(true)) $($rest)*);
164 };
165
166 (@object $object:ident ($($key:tt)+) (: false $($rest:tt)*) $copy:tt) => {
168 platform_value_internal!(@object $object [$($key)+] (platform_value_internal!(false)) $($rest)*);
169 };
170
171 (@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
173 platform_value_internal!(@object $object [$($key)+] (platform_value_internal!([$($array)*])) $($rest)*);
174 };
175
176 (@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
178 platform_value_internal!(@object $object [$($key)+] (platform_value_internal!({$($map)*})) $($rest)*);
179 };
180
181 (@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
183 platform_value_internal!(@object $object [$($key)+] (platform_value_internal!($value)) , $($rest)*);
184 };
185
186 (@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
188 platform_value_internal!(@object $object [$($key)+] (platform_value_internal!($value)));
189 };
190
191 (@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
193 platform_value_internal!();
195 };
196
197 (@object $object:ident ($($key:tt)+) () $copy:tt) => {
200 platform_value_internal!();
202 };
203
204 (@object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
206 platform_value_unexpected!($colon);
208 };
209
210 (@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
212 platform_value_unexpected!($comma);
214 };
215
216 (@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
219 platform_value_internal!(@object $object ($key) (: $($rest)*) (: $($rest)*));
220 };
221
222 (@object $object:ident ($($key:tt)*) (: $($unexpected:tt)+) $copy:tt) => {
224 platform_value_expect_expr_comma!($($unexpected)+);
225 };
226
227 (@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
229 platform_value_internal!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
230 };
231
232 (null) => {
239 $crate::Value::Null
240 };
241
242 (true) => {
243 $crate::Value::Bool(true)
244 };
245
246 (false) => {
247 $crate::Value::Bool(false)
248 };
249
250 ([]) => {
251 $crate::Value::Array(platform_value_internal_vec![])
252 };
253
254 ([ $($tt:tt)+ ]) => {
255 $crate::Value::Array(platform_value_internal!(@array [] $($tt)+))
256 };
257
258 ({}) => {
259 $crate::Value::Map($crate::ValueMap::new())
260 };
261
262 ({ $($tt:tt)+ }) => {
263 $crate::Value::Map({
264 let mut object = $crate::ValueMap::new();
265 platform_value_internal!(@object object () ($($tt)+) ($($tt)+));
266 object
267 })
268 };
269
270 ($other:expr) => {
273 $crate::to_value(&$other).unwrap()
274 };
275}
276
277#[macro_export]
281#[doc(hidden)]
282macro_rules! platform_value_internal_vec {
283 ($($content:tt)*) => {
284 vec![$($content)*]
285 };
286}
287
288#[macro_export]
289#[doc(hidden)]
290macro_rules! platform_value_unexpected {
291 () => {};
292}
293
294#[macro_export]
295#[doc(hidden)]
296macro_rules! platform_value_expect_expr_comma {
297 ($e:expr , $($tt:tt)*) => {};
298}
299
300#[cfg(test)]
301mod test {
302 use crate::types::binary_data::BinaryData;
303 use crate::{to_value, Identifier, Value};
304
305 #[test]
306 fn test_null() {
307 let value = platform_value!(null);
308 assert_eq!(value, Value::Null)
309 }
310
311 #[test]
312 fn test_identity_is_kept() {
313 let id = Identifier::new([0; 32]);
314 let value = to_value(id).unwrap();
315 assert_eq!(value, Value::Identifier(id.to_buffer()));
316 let value = platform_value!(id);
317 assert_eq!(value, Value::Identifier(id.to_buffer()))
318 }
319
320 #[test]
321 fn test_binary_is_kept() {
322 let id = BinaryData::new([0; 44].to_vec());
323 let value = to_value(id.clone()).unwrap();
324 assert_eq!(value, Value::Bytes(id.to_vec()));
325 let value = platform_value!(id);
326 assert_eq!(value, Value::Bytes(id.to_vec()));
327 }
328}