1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
//! # Macros
//!
//! This module provides convenience macros for defining extension points,
//! creating plugins, and working with hooks.
//!
//! ## Overview
//!
//! - [`extension_point!`](crate::extension_point): Defines a new extension point and its associated trait
//! - [`simple_plugin!`](crate::simple_plugin): Creates a simple plugin implementation with minimal boilerplate
//! - [`register_hook!`](crate::register_hook): Registers a hook with a hook registry
//!
//! These macros reduce the amount of boilerplate code needed to work with the
//! steckrs plugin system, making it easier to define and use plugins.
/// Defines a new [`ExtensionPoint`](crate::hook::ExtensionPoint) and its associated trait.
///
/// This macro generates:
/// - An extension point struct
/// - A trait for the extension point
/// - An implementation of the [`ExtensionPoint`](crate::hook::ExtensionPoint) trait for the struct
///
/// # Parameters
///
/// - `$name_meta`: Attributes for the extension point, like documentation and derives
/// - `$name`: The name of the extension point struct
/// - `$trait_meta`: Attributes for the trait, like documentation and derives
/// - `$trait_name`: The name of the trait that hooks will implement
/// - `$($fn_sig:tt)*`: The function signatures for the trait
///
/// # Examples
///
/// ```
/// use steckrs::{extension_point,Plugin};
/// use steckrs::hook::{Hook, ExtensionPoint};
///
/// // Define an extension point with a single method
/// extension_point!(
/// Logger: LoggerTrait; // Name of EP, Name of it's trait
/// fn log(&self, message: &str); // the methods the trait of the EP implements
/// );
///
/// // Define an extension point with multiple methods
/// extension_point!(
/// /// struct documentation for Formatter
/// Formatter:
/// /// trait documentation for FormatterTrait
/// FormatterTrait;
/// /// Function documentation for format
/// fn format(&self, text: &str) -> String;
/// fn supports_format(&self, format_name: &str) -> bool;
/// );
///
/// // Implement the trait for a concrete type
/// struct ConsoleLogger;
/// impl LoggerTrait for ConsoleLogger {
/// fn log(&self, message: &str) {
/// println!("Log: {}", message);
/// }
/// }
///
///
/// let hook = Hook::<Logger>::new(Box::new(ConsoleLogger), "myhook");
/// hook.inner().log("Hello from hook!");
/// ```
/// Creates a simple [Plugin](crate::Plugin) with a specified set of hooks.
///
/// This macro generates a plugin struct with the following features:
/// - Implements the `Plugin` trait
/// - Has a static `ID` constant
/// - Provides a `new()` method
/// - Registers the specified hooks
///
/// # Parameters
///
/// - `$plugin_meta`: Attributes for the plugin, like documentation and derives
/// - `$name`: The name of the plugin struct
/// - `$id`: The unique ID of the plugin (as a string literal)
/// - `$description`: A description of the plugin (as a string literal)
/// - `hooks: [($ext_point:ty, $hook_impl:ty)]`: A list of extension point and hook implementation pairs
///
/// # Examples
///
/// ```
/// use steckrs::{extension_point, simple_plugin, Plugin};
///
/// // Define an extension point
/// extension_point!(
/// Greeter: GreeterTrait;
/// fn greet(&self, name: &str) -> String;
/// );
///
/// // Implement the extension point
/// struct FormalGreeter;
/// impl GreeterTrait for FormalGreeter {
/// fn greet(&self, name: &str) -> String {
/// format!("Good day, {}!", name)
/// }
/// }
///
/// // Create a plugin with the implementation
/// simple_plugin!(
/// FormalGreetingPlugin,
/// "formal_greeting_plugin",
/// "A plugin that provides formal greetings",
/// hooks: [(Greeter, FormalGreeter)]
/// );
///
/// // Create an instance of the plugin
/// let plugin = FormalGreetingPlugin::new();
/// assert_eq!(plugin.id(), "formal_greeting_plugin");
/// assert_eq!(plugin.description(), "A plugin that provides formal greetings");
///
/// // Create a plugin with multiple hooks
/// struct CasualGreeter;
/// impl GreeterTrait for CasualGreeter {
/// fn greet(&self, name: &str) -> String {
/// format!("Hey, {}!", name)
/// }
/// }
///
/// extension_point!(
/// Farewell: FarewellTrait;
/// fn say_goodbye(&self, name: &str) -> String;
/// );
///
/// struct SimpleFarewell;
/// impl FarewellTrait for SimpleFarewell {
/// fn say_goodbye(&self, name: &str) -> String {
/// format!("Goodbye, {}!", name)
/// }
/// }
///
/// simple_plugin!(
/// /// Document your plugin like this if you want
/// GreetingPlugin,
/// "greeting_plugin",
/// "A plugin with multiple greeting implementations",
/// hooks: [
/// (Greeter, CasualGreeter, "casual"), // if there are two hooks for an extension point
/// (Greeter, FormalGreeter, "formal"), // you need to add a discriminant
/// (Farewell, SimpleFarewell)
/// ]
/// );
/// ```
///
/// # Panics
///
/// The generated [`register_hooks`](crate::Plugin::register_hooks) method may panic if hook registration fails.
};
}
/// Registers a [`Hook`](crate::hook::Hook) with a [`HookRegistry`](crate::hook::HookRegistry).
///
/// This macro simplifies the process of creating and registering a hook
/// with a hook registry.
///
/// # Parameters
///
/// - `$registry`: The hook registry to register with
/// - `$plugin_id`: The ID of the plugin
/// - `$ext_point_id`: The ID of the extension point
/// - `$discriminator`: An optional discriminator (or `None`)
/// - `$hook_trait`: The trait type for the hook
/// - `$hook_impl`: The implementation type for the hook
///
/// # Panics
///
/// This macro will panic if [`crate::hook::HookRegistry::register`] fails.
///
/// # Examples
///
/// ```
/// use steckrs::{
/// extension_point,
/// hook::{ExtensionPoint, HookRegistry},
/// register_hook,
/// };
///
/// extension_point!(
/// Calculator: CalculatorTrait;
/// fn add(&self, a: i32, b: i32) -> i32;
/// );
///
/// struct SimpleCalculator;
/// impl CalculatorTrait for SimpleCalculator {
/// fn add(&self, a: i32, b: i32) -> i32 {
/// a + b
/// }
/// }
///
/// let mut registry = HookRegistry::new();
///
/// // Register a hook
/// register_hook!(
/// registry,
/// "calculator_plugin", // a plugin id would be better
/// Calculator,
/// SimpleCalculator
/// );
///
/// // Use the registered hook
/// let hooks = registry.get_by_extension_point::<Calculator>();
/// assert_eq!(hooks.len(), 1);
/// assert_eq!(hooks[0].1.inner().add(2, 3), 5);
/// ```