teo_runtime/value/
macros.rs

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