1#[macro_export]
3macro_rules! impl_extension {
4 () => {
5 {
6 mf_core::extension::Extension::new()
7 }
8 };
9 ($(attr:$attr:expr),*) => {
10 {
11 let mut ext = mf_core::extension::Extension::new();
12 $(
13 ext.add_global_attribute($attr);
14 )*
15 ext
16 }
17 };
18 ($(plugin:$plugin:expr),*) => {
19 {
20 let mut ext = mf_core::extension::Extension::new();
21 $(
22 ext.add_plugin(std::sync::Arc::new($plugin));
23 )*
24 ext
25 }
26 };
27 ($(op:$op:expr),*) => {
28 {
29 let mut ext = mf_core::extension::Extension::new();
30 $(
31 ext.add_op_fn(std::sync::Arc::new($op));
32 )*
33 ext
34 }
35 };
36 ($(attr:$attr:expr),* ; $(plugin:$plugin:expr),*) => {
37 {
38 let mut ext = mf_core::extension::Extension::new();
39 $(
40 ext.add_global_attribute($attr);
41 )*
42 $(
43 ext.add_plugin(std::sync::Arc::new($plugin));
44 )*
45 ext
46 }
47 };
48 ($(attr:$attr:expr),* ; $(plugin:$plugin:expr),* ; $(op:$op:expr),*) => {
49 {
50 let mut ext = mf_core::extension::Extension::new();
51 $(
52 ext.add_global_attribute($attr);
53 )*
54 $(
55 ext.add_plugin(std::sync::Arc::new($plugin));
56 )*
57 $(
58 ext.add_op_fn(std::sync::Arc::new($op));
59 )*
60 ext
61 }
62 };
63}
64
65#[macro_export]
92macro_rules! mf_ops {
93 ($name:ident, [ $( $op:ident ),+ $(,)? ]) => {
94 pub fn $name() -> mf_core::extension::OpFn {
95 vec![
96 $(
97 std::sync::Arc::new($op),
98 )+
99 ]
100 }
101 };
102}
103
104#[macro_export]
158macro_rules! mf_extension {
159 (
160 $name:ident
161 $(, ops = [ $( $op:ident ),+ $(,)? ] )?
162 $(, plugins = [ $( $plugin:expr ),+ $(,)? ] )?
163 $(, global_attributes = [ $( $attr:expr ),+ $(,)? ] )?
164 $(, node_transform = $node_transform_fn:expr )?
165 $(, docs = $docs:expr )?
166 $(,)?
167 ) => {
168 $( #[doc = $docs] )?
169 #[doc = concat!("let extension = ", stringify!($name), "::init();")]
177 #[allow(non_camel_case_types)]
179 pub struct $name;
180
181 impl $name {
182 pub fn init() -> mf_core::extension::Extension {
187 let mut ext = mf_core::extension::Extension::new();
188
189 $(
191 let ops: mf_core::extension::OpFn = vec![
192 $(
193 std::sync::Arc::new($op),
194 )+
195 ];
196 for op in ops {
197 ext.add_op_fn(op);
198 }
199 )?
200
201 $(
203 $(
204 ext.add_plugin(std::sync::Arc::new($plugin));
205 )+
206 )?
207
208 $(
210 $(
211 ext.add_global_attribute($attr);
212 )+
213 )?
214
215 $(
217 ext.add_node_transform(std::sync::Arc::new($node_transform_fn));
218 )?
219
220 ext
221 }
222 }
223 };
224}
225
226#[macro_export]
228macro_rules! mf_extension_with_config {
229 (
230 $name:ident,
231 config = { $( $config_field:ident : $config_type:ty ),+ $(,)? },
232 init_fn = $init_fn:expr
233 $(, docs = $docs:expr )?
234 $(,)?
235 ) => {
236 $( #[doc = $docs] )?
237 #[allow(non_camel_case_types)]
240 pub struct $name;
241
242 impl $name {
243 pub fn init( $( $config_field: $config_type ),+ ) -> mf_core::extension::Extension {
245 let mut ext = mf_core::extension::Extension::new();
246 ($init_fn)(&mut ext, $( $config_field ),+ );
247 ext
248 }
249 }
250 };
251}
252
253#[macro_export]
255macro_rules! mf_global_attr {
256 ($types:expr, $attributes:expr) => {{
257 use std::collections::HashMap;
258 use mf_model::schema::AttributeSpec;
259
260 let mut attr_map = HashMap::new();
261 let attributes: Vec<(&str, AttributeSpec)> = $attributes;
262 for (key, spec) in attributes {
263 attr_map.insert(key.to_string(), spec);
264 }
265
266 mf_core::types::GlobalAttributeItem {
267 types: $types.iter().map(|s| s.to_string()).collect(),
268 attributes: attr_map,
269 }
270 }};
271
272 ($type_name:expr, $key:expr, $value:expr) => {{
274 use std::collections::HashMap;
275 use mf_model::schema::AttributeSpec;
276 use serde_json::Value;
277
278 let mut attr_map = HashMap::new();
279 attr_map.insert(
280 $key.to_string(),
281 AttributeSpec { default: Some(Value::String($value.to_string())) },
282 );
283
284 mf_core::types::GlobalAttributeItem {
285 types: vec![$type_name.to_string()],
286 attributes: attr_map,
287 }
288 }};
289}
290
291#[macro_export]
293macro_rules! mf_op {
294 ($name:ident, $body:block) => {
295 fn $name(
296 _manager: &mf_state::ops::GlobalResourceManager
297 ) -> mf_core::ForgeResult<()> {
298 $body
299 }
300 };
301 ($name:ident, |$manager:ident| $body:block) => {
302 fn $name(
303 $manager: &mf_state::ops::GlobalResourceManager
304 ) -> mf_core::ForgeResult<()> {
305 $body
306 }
307 };
308}
309
310#[macro_export]
312macro_rules! mf_node_transform {
313 ($name:ident, $body:block) => {
314 fn $name(_node: &mf_core::node::Node) -> Option<mf_core::node::Node> {
315 $body
316 }
317 };
318 ($name:ident, |$node:ident| $body:block) => {
319 fn $name($node: &mf_core::node::Node) -> Option<mf_core::node::Node> {
320 $body
321 }
322 };
323}