moduforge_runtime/
macros.rs

1#[macro_export]
2macro_rules! node {
3    ($name:expr) => {
4        {
5            let mut node = $crate::node::Node::default();
6            node.set_name($name);
7            node
8        }
9    };
10    ($name:expr, $desc:expr) => {
11        {
12            let mut node = $crate::node::Node::default();
13            node.set_name($name).set_desc($desc);
14            node
15        }
16    };
17    ($name:expr, $desc:expr, $content:expr) => {
18        {
19            let mut node = $crate::node::Node::default();
20            node.set_name($name).set_desc($desc).set_content($content);
21            node
22        }
23    };
24    ($name:expr, $desc:expr, $content:expr, $($key:expr => $value:expr),*) => {
25        {
26            let mut node = $crate::node::Node::default();
27            node.set_name($name)
28                .set_desc($desc)
29                .set_content($content);
30            $(
31                node.set_attr($key, Some($value));
32            )*
33            node
34        }
35    };
36}
37
38#[macro_export]
39macro_rules! mark {
40    ($name:expr) => {
41        {
42            let mut mark = $crate::mark::Mark::default();
43            mark.set_name($name);
44            mark
45        }
46    };
47    ($name:expr, $desc:expr) => {
48        {
49            let mut mark = $crate::mark::Mark::default();
50            mark.set_name($name).set_desc($desc);
51            mark
52        }
53    };
54    ($name:expr, $desc:expr, $($key:expr => $value:expr),*) => {
55        {
56            let mut mark = $crate::mark::Mark::default();
57            mark.set_name($name)
58                .set_desc($desc);
59            $(
60                mark.set_attr($key, Some($value));
61            )*
62            mark
63        }
64    };
65}
66
67#[macro_export]
68macro_rules! impl_plugin {
69    ($name:ident, $append_fn:expr) => {
70        #[derive(Debug)]
71        pub struct $name {}
72
73        #[async_trait]
74        impl PluginTrait for $name
75        where
76            Self: Send + Sync,
77        {
78            async fn append_transaction(
79                &self,
80                trs: &[Transaction],
81                old_state: &State,
82                new_state: &State,
83            ) -> Option<Transaction> {
84                $append_fn(trs, old_state, new_state).await
85            }
86
87            async fn filter_transaction(
88                &self,
89                _tr: &Transaction,
90                _state: &State,
91            ) -> bool {
92                true
93            }
94
95            async fn before_apply_transaction(
96                &self,
97                _tr: &mut Transaction,
98                _state: &State,
99            ) -> Result<(), Box<dyn std::error::Error>> {
100                Ok(())
101            }
102
103            async fn after_apply_transaction(
104                &self,
105                _new_state: &State,
106                _tr: &mut Transaction,
107                _old_state: &State,
108            ) -> Result<(), Box<dyn std::error::Error>> {
109                Ok(())
110            }
111        }
112    };
113    ($name:ident, $append_fn:expr, $filter_fn:expr) => {
114        #[derive(Debug)]
115        pub struct $name {}
116
117        #[async_trait]
118        impl PluginTrait for $name
119        where
120            Self: Send + Sync,
121        {
122            async fn append_transaction(
123                &self,
124                tr: &Transaction,
125                old_state: &State,
126                new_state: &State,
127            ) -> Option<Transaction> {
128                $append_fn(tr, old_state, new_state).await
129            }
130
131            async fn filter_transaction(
132                &self,
133                tr: &Transaction,
134                state: &State,
135            ) -> bool {
136                $filter_fn(tr, state)
137            }
138
139            async fn before_apply_transaction(
140                &self,
141                _tr: &mut Transaction,
142                _state: &State,
143            ) -> Result<(), Box<dyn std::error::Error>> {
144                Ok(())
145            }
146
147            async fn after_apply_transaction(
148                &self,
149                _new_state: &State,
150                _tr: &mut Transaction,
151                _old_state: &State,
152            ) -> Result<(), Box<dyn std::error::Error>> {
153                Ok(())
154            }
155        }
156    };
157    ($name:ident, $append_fn:expr, $filter_fn:expr, $before_fn:expr) => {
158        #[derive(Debug)]
159        pub struct $name {}
160
161        #[async_trait]
162        impl PluginTrait for $name
163        where
164            Self: Send + Sync,
165        {
166            async fn append_transaction(
167                &self,
168                tr: &Transaction,
169                old_state: &State,
170                new_state: &State,
171            ) -> Option<Transaction> {
172                $append_fn(tr, old_state, new_state).await
173            }
174
175            async fn filter_transaction(
176                &self,
177                tr: &Transaction,
178                state: &State,
179            ) -> bool {
180                $filter_fn(tr, state).await
181            }
182
183            async fn before_apply_transaction(
184                &self,
185                tr: &mut Transaction,
186                state: &State,
187            ) -> Result<(), Box<dyn std::error::Error>> {
188                $before_fn(tr, state).await
189            }
190
191            async fn after_apply_transaction(
192                &self,
193                _new_state: &State,
194                _tr: &mut Transaction,
195                _old_state: &State,
196            ) -> Result<(), Box<dyn std::error::Error>> {
197                Ok(())
198            }
199        }
200    };
201    ($name:ident, $append_fn:expr, $filter_fn:expr, $before_fn:expr, $after_fn:expr) => {
202        #[derive(Debug)]
203        pub struct $name {}
204
205        #[async_trait]
206        impl PluginTrait for $name
207        where
208            Self: Send + Sync,
209        {
210            async fn append_transaction(
211                &self,
212                tr: &Transaction,
213                old_state: &State,
214                new_state: &State,
215            ) -> Option<Transaction> {
216                $append_fn(tr, old_state, new_state).await
217            }
218
219            async fn filter_transaction(
220                &self,
221                tr: &Transaction,
222                state: &State,
223            ) -> bool {
224                $filter_fn(tr, state).await
225            }
226
227            async fn before_apply_transaction(
228                &self,
229                tr: &mut Transaction,
230                state: &State,
231            ) -> Result<(), Box<dyn std::error::Error>> {
232                $before_fn(tr, state).await
233            }
234
235            async fn after_apply_transaction(
236                &self,
237                new_state: &State,
238                tr: &mut Transaction,
239                old_state: &State,
240            ) -> Result<(), Box<dyn std::error::Error>> {
241                $after_fn(new_state, tr, old_state).await
242            }
243        }
244    };
245}
246
247#[macro_export]
248macro_rules! impl_state_field {
249    ($name:ident, $init_fn:expr, $apply_fn:expr) => {
250        #[derive(Debug)]
251        pub struct $name;
252
253        #[async_trait]
254        impl StateField for $name
255        where
256            Self: Send + Sync,
257        {
258            async fn init(
259                &self,
260                config: &StateConfig,
261                instance: Option<&State>,
262            ) -> PluginState {
263                $init_fn(config, instance).await
264            }
265
266            async fn apply(
267                &self,
268                tr: &Transaction,
269                value: PluginState,
270                old_state: &State,
271                new_state: &State,
272            ) -> PluginState {
273                $apply_fn(tr, value, old_state, new_state).await
274            }
275        }
276    };
277}
278
279#[macro_export]
280macro_rules! impl_command {
281    ($name:ident, $execute_fn:expr) => {
282        #[derive(Debug)]
283        pub struct $name;
284
285        #[async_trait]
286        impl Command for $name {
287            async fn execute(
288                &self,
289                tr: &mut Transaction,
290            ) -> Result<(), TransformError> {
291                $execute_fn(tr).await
292            }
293
294            fn name(&self) -> String {
295                stringify!($name).to_string()
296            }
297        }
298    };
299    ($name:ident, $execute_fn:expr, $name_str:expr) => {
300        #[derive(Debug)]
301        pub struct $name;
302
303        #[async_trait]
304        impl Command for $name {
305            async fn execute(
306                &self,
307                tr: &mut Transaction,
308            ) -> Result<(), TransformError> {
309                $execute_fn(tr).await
310            }
311
312            fn name(&self) -> String {
313                $name_str.to_string()
314            }
315        }
316    };
317}