ipld_core/macros.rs
1//! `ipld!` macro.
2/// Construct an `Ipld` from a literal.
3///
4/// ```edition2018
5/// # extern crate alloc;
6/// # use ipld_core::ipld;
7/// #
8/// let value = ipld!({
9/// "code": 200,
10/// "success": true,
11/// "payload": {
12/// "features": [
13/// "serde",
14/// "json"
15/// ]
16/// }
17/// });
18/// ```
19///
20/// Variables or expressions can be interpolated into the JSON literal. Any type
21/// interpolated into an array element or object value must implement Serde's
22/// `Serialize` trait, while any type interpolated into a object key must
23/// implement `Into<String>`. If the `Serialize` implementation of the
24/// interpolated type decides to fail, or if the interpolated type contains a
25/// map with non-string keys, the `json!` macro will panic.
26///
27/// ```edition2018
28/// # extern crate alloc;
29/// # use ipld_core::ipld;
30/// #
31/// let code = 200;
32/// let features = vec!["serde", "json"];
33///
34/// let value = ipld!({
35/// "code": code,
36/// "success": code == 200,
37/// "payload": {
38/// features[0]: features[1]
39/// }
40/// });
41/// ```
42///
43/// Trailing commas are allowed inside both arrays and objects.
44///
45/// ```edition2018
46/// # extern crate alloc;
47/// # use ipld_core::ipld;
48/// #
49/// let value = ipld!([
50/// "notice",
51/// "the",
52/// "trailing",
53/// "comma -->",
54/// ]);
55/// ```
56#[macro_export(local_inner_macros)]
57macro_rules! ipld {
58 // Hide distracting implementation details from the generated rustdoc.
59 ($($ipld:tt)+) => {
60 ipld_internal!($($ipld)+)
61 };
62}
63
64#[macro_export(local_inner_macros)]
65#[doc(hidden)]
66macro_rules! ipld_internal {
67 //////////////////////////////////////////////////////////////////////////
68 // TT muncher for parsing the inside of an array [...]. Produces a vec![...]
69 // of the elements.
70 //
71 // Must be invoked as: ipld_internal!(@array [] $($tt)*)
72 //////////////////////////////////////////////////////////////////////////
73
74 // Done with trailing comma.
75 (@array [$($elems:expr,)*]) => {
76 ipld_internal_vec![$($elems,)*]
77 };
78
79 // Done without trailing comma.
80 (@array [$($elems:expr),*]) => {
81 ipld_internal_vec![$($elems),*]
82 };
83
84 // Next element is `null`.
85 (@array [$($elems:expr,)*] null $($rest:tt)*) => {
86 ipld_internal!(@array [$($elems,)* ipld_internal!(null)] $($rest)*)
87 };
88
89 // Next element is `true`.
90 (@array [$($elems:expr,)*] true $($rest:tt)*) => {
91 ipld_internal!(@array [$($elems,)* ipld_internal!(true)] $($rest)*)
92 };
93
94 // Next element is `false`.
95 (@array [$($elems:expr,)*] false $($rest:tt)*) => {
96 ipld_internal!(@array [$($elems,)* ipld_internal!(false)] $($rest)*)
97 };
98
99 // Next element is an array.
100 (@array [$($elems:expr,)*] [$($array:tt)*] $($rest:tt)*) => {
101 ipld_internal!(@array [$($elems,)* ipld_internal!([$($array)*])] $($rest)*)
102 };
103
104 // Next element is a map.
105 (@array [$($elems:expr,)*] {$($map:tt)*} $($rest:tt)*) => {
106 ipld_internal!(@array [$($elems,)* ipld_internal!({$($map)*})] $($rest)*)
107 };
108
109 // Next element is an expression followed by comma.
110 (@array [$($elems:expr,)*] $next:expr, $($rest:tt)*) => {
111 ipld_internal!(@array [$($elems,)* ipld_internal!($next),] $($rest)*)
112 };
113
114 // Last element is an expression with no trailing comma.
115 (@array [$($elems:expr,)*] $last:expr) => {
116 ipld_internal!(@array [$($elems,)* ipld_internal!($last)])
117 };
118
119 // Comma after the most recent element.
120 (@array [$($elems:expr),*] , $($rest:tt)*) => {
121 ipld_internal!(@array [$($elems,)*] $($rest)*)
122 };
123
124 // Unexpected token after most recent element.
125 (@array [$($elems:expr),*] $unexpected:tt $($rest:tt)*) => {
126 ipld_unexpected!($unexpected)
127 };
128
129 //////////////////////////////////////////////////////////////////////////
130 // TT muncher for parsing the inside of an object {...}. Each entry is
131 // inserted into the given map variable.
132 //
133 // Must be invoked as: json_internal!(@object $map () ($($tt)*) ($($tt)*))
134 //
135 // We require two copies of the input tokens so that we can match on one
136 // copy and trigger errors on the other copy.
137 //////////////////////////////////////////////////////////////////////////
138
139 // Done.
140 (@object $object:ident () () ()) => {};
141
142 // Insert the current entry followed by trailing comma.
143 (@object $object:ident [$($key:tt)+] ($value:expr) , $($rest:tt)*) => {
144 let _ = $object.insert(($($key)+).into(), $value);
145 ipld_internal!(@object $object () ($($rest)*) ($($rest)*));
146 };
147
148 // Current entry followed by unexpected token.
149 (@object $object:ident [$($key:tt)+] ($value:expr) $unexpected:tt $($rest:tt)*) => {
150 ipld_unexpected!($unexpected);
151 };
152
153 // Insert the last entry without trailing comma.
154 (@object $object:ident [$($key:tt)+] ($value:expr)) => {
155 let _ = $object.insert(($($key)+).into(), $value);
156 };
157
158 // Next value is `null`.
159 (@object $object:ident ($($key:tt)+) (: null $($rest:tt)*) $copy:tt) => {
160 ipld_internal!(@object $object [$($key)+] (ipld_internal!(null)) $($rest)*);
161 };
162
163 // Next value is `true`.
164 (@object $object:ident ($($key:tt)+) (: true $($rest:tt)*) $copy:tt) => {
165 ipld_internal!(@object $object [$($key)+] (ipld_internal!(true)) $($rest)*);
166 };
167
168 // Next value is `false`.
169 (@object $object:ident ($($key:tt)+) (: false $($rest:tt)*) $copy:tt) => {
170 ipld_internal!(@object $object [$($key)+] (ipld_internal!(false)) $($rest)*);
171 };
172
173 // Next value is an array.
174 (@object $object:ident ($($key:tt)+) (: [$($array:tt)*] $($rest:tt)*) $copy:tt) => {
175 ipld_internal!(@object $object [$($key)+] (ipld_internal!([$($array)*])) $($rest)*);
176 };
177
178 // Next value is a map.
179 (@object $object:ident ($($key:tt)+) (: {$($map:tt)*} $($rest:tt)*) $copy:tt) => {
180 ipld_internal!(@object $object [$($key)+] (ipld_internal!({$($map)*})) $($rest)*);
181 };
182
183 // Next value is an expression followed by comma.
184 (@object $object:ident ($($key:tt)+) (: $value:expr , $($rest:tt)*) $copy:tt) => {
185 ipld_internal!(@object $object [$($key)+] (ipld_internal!($value)) , $($rest)*);
186 };
187
188 // Last value is an expression with no trailing comma.
189 (@object $object:ident ($($key:tt)+) (: $value:expr) $copy:tt) => {
190 ipld_internal!(@object $object [$($key)+] (ipld_internal!($value)));
191 };
192
193 // Missing value for last entry. Trigger a reasonable error message.
194 (@object $object:ident ($($key:tt)+) (:) $copy:tt) => {
195 // "unexpected end of macro invocation"
196 ipld_internal!();
197 };
198
199 // Missing colon and value for last entry. Trigger a reasonable error
200 // message.
201 (@object $object:ident ($($key:tt)+) () $copy:tt) => {
202 // "unexpected end of macro invocation"
203 ipld_internal!();
204 };
205
206 // Misplaced colon. Trigger a reasonable error message.
207 (@object $object:ident () (: $($rest:tt)*) ($colon:tt $($copy:tt)*)) => {
208 // Takes no arguments so "no rules expected the token `:`".
209 ipld_unexpected!($colon);
210 };
211
212 // Found a comma inside a key. Trigger a reasonable error message.
213 (@object $object:ident ($($key:tt)*) (, $($rest:tt)*) ($comma:tt $($copy:tt)*)) => {
214 // Takes no arguments so "no rules expected the token `,`".
215 ipld_unexpected!($comma);
216 };
217
218 // Key is fully parenthesized. This avoids clippy double_parens false
219 // positives because the parenthesization may be necessary here.
220 (@object $object:ident () (($key:expr) : $($rest:tt)*) $copy:tt) => {
221 ipld_internal!(@object $object ($key) (: $($rest)*) (: $($rest)*));
222 };
223
224 // Munch a token into the current key.
225 (@object $object:ident ($($key:tt)*) ($tt:tt $($rest:tt)*) $copy:tt) => {
226 ipld_internal!(@object $object ($($key)* $tt) ($($rest)*) ($($rest)*));
227 };
228
229 //////////////////////////////////////////////////////////////////////////
230 // The main implementation.
231 //
232 // Must be invoked as: json_internal!($($json)+)
233 //////////////////////////////////////////////////////////////////////////
234
235 (null) => {
236 $crate::ipld::Ipld::Null
237 };
238
239 (true) => {
240 $crate::ipld::Ipld::Bool(true)
241 };
242
243 (false) => {
244 $crate::ipld::Ipld::Bool(false)
245 };
246
247 ([]) => {
248 $crate::ipld::Ipld::List(ipld_internal_vec![])
249 };
250
251 ([ $($tt:tt)+ ]) => {
252 $crate::ipld::Ipld::List(ipld_internal!(@array [] $($tt)+))
253 };
254
255 ({}) => {
256 $crate::ipld::Ipld::Map($crate::__private_do_not_use::BTreeMap::new())
257 };
258
259 ({ $($tt:tt)+ }) => {
260 $crate::ipld::Ipld::Map({
261 let mut object = $crate::__private_do_not_use::BTreeMap::new();
262 ipld_internal!(@object object () ($($tt)+) ($($tt)+));
263 object
264 })
265 };
266
267 // Any Serialize type: numbers, strings, struct literals, variables etc.
268 // Must be below every other rule.
269 ($other:expr) => {
270 {
271 $crate::ipld::Ipld::from($other)
272 }
273 };
274}
275
276// The json_internal macro above cannot invoke vec directly because it uses
277// local_inner_macros. A vec invocation there would resolve to $crate::vec.
278// Instead invoke vec here outside of local_inner_macros.
279#[macro_export]
280#[doc(hidden)]
281macro_rules! ipld_internal_vec {
282 ($($content:tt)*) => {
283 $crate::__private_do_not_use::vec![$($content)*]
284 };
285}
286
287#[macro_export]
288#[doc(hidden)]
289macro_rules! ipld_unexpected {
290 () => {};
291}