1#[macro_export(local_inner_macros)]
22macro_rules! value {
23 ($($value:tt)+) => {
24 value_internal!($($value)+)
25 };
26}
27
28#[macro_export(local_inner_macros)]
29#[doc(hidden)]
30macro_rules! value_internal {
31 (nil) => {
38 $crate::model::Value::Nil
39 };
40
41 (true) => {
42 $crate::model::Value::scalar(true)
43 };
44
45 (false) => {
46 $crate::model::Value::scalar(false)
47 };
48
49 ([]) => {
50 $crate::model::Value::Array(::std::default::Default::default())
51 };
52
53 ([ $($tt:tt)+ ]) => {
54 $crate::model::Value::Array(array_internal!(@array [] $($tt)+))
55 };
56
57 ({}) => {
58 $crate::model::Value::Object(::std::default::Default::default())
59 };
60
61 ({ $($tt:tt)+ }) => {
62 $crate::model::Value::Object({
63 let mut object = $crate::model::Object::new();
64 object_internal!(@object object () ($($tt)+) ($($tt)+));
65 object
66 })
67 };
68
69 ($other:ident) => {
70 $other
71 };
72
73 ($other:expr) => {
76 $crate::model::to_value(&$other).unwrap()
77 };
78}
79
80#[macro_export]
81#[doc(hidden)]
82macro_rules! value_unexpected {
83 () => {};
84}
85
86#[macro_export(local_inner_macros)]
96macro_rules! object {
97 ($($value:tt)+) => {
98 object_internal!($($value)+)
99 };
100}
101
102#[macro_export(local_inner_macros)]
103#[doc(hidden)]
104macro_rules! object_internal {
105 (@object $object:ident () () ()) => {};
117
118 (@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
120 let _ = $object.insert(($($key)+).into(), $value);
121 object_internal!(@object $object () ($($rest)*) ($($rest)*));
122 };
123
124 (@object $object:ident [$($key:tt)+] ($value:expr) $unexpected:tt $($rest:tt)*) => {
126 object_unexpected!($unexpected);
127 };
128
129 (@object $object:ident [$($key:tt)+] ($value:expr)) => {
131 let _ = $object.insert(($($key)+).into(), $value);
132 };
133
134 (@object $object:ident ($($key:tt)+) (: nil $($rest:tt)*) $copy:tt) => {
136 object_internal!(@object $object [$($key)+] (value_internal!(nil)) $($rest)*);
137 };
138
139 (@object $object:ident ($($key:tt)+) (: true $($rest:tt)*) $copy:tt) => {
141 object_internal!(@object $object [$($key)+] (value_internal!(true)) $($rest)*);
142 };
143
144 (@object $object:ident ($($key:tt)+) (: false $($rest:tt)*) $copy:tt) => {
146 object_internal!(@object $object [$($key)+] (value_internal!(false)) $($rest)*);
147 };
148
149 (@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
151 object_internal!(@object $object [$($key)+] (value_internal!([$($array)*])) $($rest)*);
152 };
153
154 (@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
156 object_internal!(@object $object [$($key)+] (value_internal!({$($map)*})) $($rest)*);
157 };
158
159 (@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
161 object_internal!(@object $object [$($key)+] (value_internal!($value)) , $($rest)*);
162 };
163
164 (@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
166 object_internal!(@object $object [$($key)+] (value_internal!($value)));
167 };
168
169 (@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
171 object_internal!();
173 };
174
175 (@object $object:ident ($($key:tt)+) () $copy:tt) => {
178 object_internal!();
180 };
181
182 (@object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
184 object_unexpected!($colon);
186 };
187
188 (@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
190 object_unexpected!($comma);
192 };
193
194 (@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
197 object_internal!(@object $object ($key) (: $($rest)*) (: $($rest)*));
198 };
199
200 (@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
202 object_internal!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
203 };
204
205 ({}) => {
212 $crate::model::Object::new()
213 };
214
215 ({ $($tt:tt)+ }) => {
216 {
217 let mut object = $crate::model::Object::new();
218 object_internal!(@object object () ($($tt)+) ($($tt)+));
219 object
220 }
221 };
222
223 ($other:ident) => {
224 $other
225 };
226
227 ($other:expr) => {
230 $crate::model::to_object(&$other).unwrap()
231 };
232}
233
234#[macro_export]
235#[doc(hidden)]
236macro_rules! object_unexpected {
237 () => {};
238}
239
240#[macro_export(local_inner_macros)]
252macro_rules! array {
253 ($($value:tt)+) => {
254 array_internal!($($value)+)
255 };
256}
257
258#[macro_export(local_inner_macros)]
259#[doc(hidden)]
260macro_rules! array_internal {
261 (@array [$($elems:expr,)*]) => {
263 array_internal_vec![$($elems,)*]
264 };
265
266 (@array [$($elems:expr),*]) => {
268 array_internal_vec![$($elems),*]
269 };
270
271 (@array [$($elems:expr,)*] nil $($rest:tt)*) => {
273 array_internal!(@array [$($elems,)* value_internal!(nil)] $($rest)*)
274 };
275
276 (@array [$($elems:expr,)*] true $($rest:tt)*) => {
278 array_internal!(@array [$($elems,)* value_internal!(true)] $($rest)*)
279 };
280
281 (@array [$($elems:expr,)*] false $($rest:tt)*) => {
283 array_internal!(@array [$($elems,)* value_internal!(false)] $($rest)*)
284 };
285
286 (@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
288 array_internal!(@array [$($elems,)* value_internal!([$($array)*])] $($rest)*)
289 };
290
291 (@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
293 array_internal!(@array [$($elems,)* value_internal!({$($map)*})] $($rest)*)
294 };
295
296 (@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
298 array_internal!(@array [$($elems,)* value_internal!($next),] $($rest)*)
299 };
300
301 (@array [$($elems:expr,)*] $last:expr) => {
303 array_internal!(@array [$($elems,)* value_internal!($last)])
304 };
305
306 (@array [$($elems:expr),*] , $($rest:tt)*) => {
308 array_internal!(@array [$($elems,)*] $($rest)*)
309 };
310
311 (@array [$($elems:expr),*] $unexpected:tt $($rest:tt)*) => {
313 array_unexpected!($unexpected)
314 };
315
316 ([]) => {
323 $crate::model::Array::default()
324 };
325
326 ([ $($tt:tt)+ ]) => {
327 array_internal!(@array [] $($tt)+)
328 };
329
330 ($other:ident) => {
331 $other
332 };
333}
334
335#[macro_export]
336#[doc(hidden)]
337macro_rules! array_internal_vec {
338 ($($content:tt)*) => {
339 vec![$($content)*]
340 };
341}
342
343#[macro_export]
344#[doc(hidden)]
345macro_rules! array_unexpected {
346 () => {};
347}
348
349#[macro_export]
364macro_rules! scalar {
365 ($value:literal) => {
366 $crate::model::Scalar::new($value)
367 };
368
369 ($other:ident) => {
370 $other
371 };
372
373 ($other:expr) => {
376 $crate::model::to_scalar(&$other).unwrap()
377 };
378}
379
380#[allow(unused_macros)]
381#[macro_export]
382macro_rules! call_filter {
383 ($filter:expr, $input:expr) => {{
384 $crate::call_filter!($filter, $input, )
385 }};
386 ($filter:expr, $input:expr, $($args:expr),*) => {{
387 let positional = Box::new(vec![$($crate::Expression::Literal($crate::value!($args))),*].into_iter());
388 let keyword = Box::new(Vec::new().into_iter());
389 let args = $crate::parser::FilterArguments { positional, keyword };
390
391 let runtime = $crate::runtime::RuntimeBuilder::new().build();
392
393 let input = $crate::value!($input);
394
395 $crate::ParseFilter::parse(&$filter, args)
396 .and_then(|filter| $crate::Filter::evaluate(&*filter, &input, &runtime))
397 }};
398}