liquid_value/
macros.rs

1/// A liquid_value::Value literal.
2///
3/// # Example
4///
5/// ```rust
6/// # #[macro_use]
7/// # extern crate liquid_value;
8/// #
9/// # fn main() {
10/// liquid_value!(5)
11///     .as_scalar().unwrap()
12///     .to_integer().unwrap();
13/// liquid_value!("foo")
14///     .as_scalar().unwrap()
15///     .to_str();
16/// liquid_value!([1, 2, 3])
17///     .as_array().unwrap();
18/// liquid_value!({"foo": 5})
19///     .as_object().unwrap();
20/// # }
21/// ```
22#[macro_export(local_inner_macros)]
23macro_rules! liquid_value {
24    ($($value:tt)+) => {
25        value_internal!($($value)+)
26    };
27}
28
29#[macro_export(local_inner_macros)]
30#[doc(hidden)]
31macro_rules! value_internal {
32    // Done with trailing comma.
33    (@array [$($elems:expr,)*]) => {
34        value_internal_vec![$($elems,)*]
35    };
36
37    // Done without trailing comma.
38    (@array [$($elems:expr),*]) => {
39        value_internal_vec![$($elems),*]
40    };
41
42    // Next element is `nil`.
43    (@array [$($elems:expr,)*] nil $($rest:tt)*) => {
44        value_internal!(@array [$($elems,)* value_internal!(nil)] $($rest)*)
45    };
46
47    // Next element is `true`.
48    (@array [$($elems:expr,)*] true $($rest:tt)*) => {
49        value_internal!(@array [$($elems,)* value_internal!(true)] $($rest)*)
50    };
51
52    // Next element is `false`.
53    (@array [$($elems:expr,)*] false $($rest:tt)*) => {
54        value_internal!(@array [$($elems,)* value_internal!(false)] $($rest)*)
55    };
56
57    // Next element is an array.
58    (@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
59        value_internal!(@array [$($elems,)* value_internal!([$($array)*])] $($rest)*)
60    };
61
62    // Next element is a map.
63    (@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
64        value_internal!(@array [$($elems,)* value_internal!({$($map)*})] $($rest)*)
65    };
66
67    // Next element is an expression followed by comma.
68    (@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
69        value_internal!(@array [$($elems,)* value_internal!($next),] $($rest)*)
70    };
71
72    // Last element is an expression with no trailing comma.
73    (@array [$($elems:expr,)*] $last:expr) => {
74        value_internal!(@array [$($elems,)* value_internal!($last)])
75    };
76
77    // Comma after the most recent element.
78    (@array [$($elems:expr),*] , $($rest:tt)*) => {
79        value_internal!(@array [$($elems,)*] $($rest)*)
80    };
81
82    // Unexpected token after most recent element.
83    (@array [$($elems:expr),*] $unexpected:tt $($rest:tt)*) => {
84        value_unexpected!($unexpected)
85    };
86
87    //////////////////////////////////////////////////////////////////////////
88    // TT muncher for parsing the inside of an object {...}. Each entry is
89    // inserted into the given map variable.
90    //
91    // Must be invoked as: value_internal!(@object $map () ($($tt)*) ($($tt)*))
92    //
93    // We require two copies of the input tokens so that we can match on one
94    // copy and trigger errors on the other copy.
95    //////////////////////////////////////////////////////////////////////////
96
97    // Done.
98    (@object $object:ident () () ()) => {};
99
100    // Insert the current entry followed by trailing comma.
101    (@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
102        let _ = $object.insert(($($key)+).into(), $value);
103        value_internal!(@object $object () ($($rest)*) ($($rest)*));
104    };
105
106    // Current entry followed by unexpected token.
107    (@object $object:ident [$($key:tt)+] ($value:expr) $unexpected:tt $($rest:tt)*) => {
108        value_unexpected!($unexpected);
109    };
110
111    // Insert the last entry without trailing comma.
112    (@object $object:ident [$($key:tt)+] ($value:expr)) => {
113        let _ = $object.insert(($($key)+).into(), $value);
114    };
115
116    // Next value is `nil`.
117    (@object $object:ident ($($key:tt)+) (: nil $($rest:tt)*) $copy:tt) => {
118        value_internal!(@object $object [$($key)+] (value_internal!(nil)) $($rest)*);
119    };
120
121    // Next value is `true`.
122    (@object $object:ident ($($key:tt)+) (: true $($rest:tt)*) $copy:tt) => {
123        value_internal!(@object $object [$($key)+] (value_internal!(true)) $($rest)*);
124    };
125
126    // Next value is `false`.
127    (@object $object:ident ($($key:tt)+) (: false $($rest:tt)*) $copy:tt) => {
128        value_internal!(@object $object [$($key)+] (value_internal!(false)) $($rest)*);
129    };
130
131    // Next value is an array.
132    (@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
133        value_internal!(@object $object [$($key)+] (value_internal!([$($array)*])) $($rest)*);
134    };
135
136    // Next value is a map.
137    (@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
138        value_internal!(@object $object [$($key)+] (value_internal!({$($map)*})) $($rest)*);
139    };
140
141    // Next value is an expression followed by comma.
142    (@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
143        value_internal!(@object $object [$($key)+] (value_internal!($value)) , $($rest)*);
144    };
145
146    // Last value is an expression with no trailing comma.
147    (@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
148        value_internal!(@object $object [$($key)+] (value_internal!($value)));
149    };
150
151    // Missing value for last entry. Trigger a reasonable error message.
152    (@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
153        // "unexpected end of macro invocation"
154        value_internal!();
155    };
156
157    // Missing colon and value for last entry. Trigger a reasonable error
158    // message.
159    (@object $object:ident ($($key:tt)+) () $copy:tt) => {
160        // "unexpected end of macro invocation"
161        value_internal!();
162    };
163
164    // Misplaced colon. Trigger a reasonable error message.
165    (@object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
166        // Takes no arguments so "no rules expected the token `:`".
167        value_unexpected!($colon);
168    };
169
170    // Found a comma inside a key. Trigger a reasonable error message.
171    (@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
172        // Takes no arguments so "no rules expected the token `,`".
173        value_unexpected!($comma);
174    };
175
176    // Key is fully parenthesized. This avoids clippy double_parens false
177    // positives because the parenthesization may be necessary here.
178    (@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
179        value_internal!(@object $object ($key) (: $($rest)*) (: $($rest)*));
180    };
181
182    // Munch a token into the current key.
183    (@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
184        value_internal!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
185    };
186
187    //////////////////////////////////////////////////////////////////////////
188    // The main implementation.
189    //
190    // Must be invoked as: value_internal!($($value)+)
191    //////////////////////////////////////////////////////////////////////////
192
193    (nil) => {
194        $crate::Value::Nil
195    };
196
197    (true) => {
198        $crate::Value::scalar(true)
199    };
200
201    (false) => {
202        $crate::Value::scalar(false)
203    };
204
205    ([]) => {
206        $crate::Value::Array(value_internal_vec![])
207    };
208
209    ([ $($tt:tt)+ ]) => {
210        $crate::Value::Array(value_internal!(@array [] $($tt)+))
211    };
212
213    ({}) => {
214        $crate::Value::Object(Default::default())
215    };
216
217    ({ $($tt:tt)+ }) => {
218        $crate::Value::Object({
219            let mut object = $crate::Object::new();
220            value_internal!(@object object () ($($tt)+) ($($tt)+));
221            object
222        })
223    };
224
225    ($other:ident) => {
226        $other
227    };
228
229    // Any Serialize type: numbers, strings, struct literals, variables etc.
230    // Must be below every other rule.
231    ($other:expr) => {
232        $crate::to_value($other).unwrap()
233    };
234}
235
236#[macro_export]
237#[doc(hidden)]
238macro_rules! value_internal_vec {
239    ($($content:tt)*) => {
240        vec![$($content)*]
241    };
242}