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
//! # Core Macro Traits
//!
//! This module defines the fundamental traits that all macros must implement
//! to integrate with the Macroforge TypeScript macro system.
//!
//! ## Trait Hierarchy
//!
//! - [`Macroforge`] - The core trait for individual macro implementations
//! - `MacroPackage` - Groups multiple macros into a distributable package
//!
//! ## Implementing a Custom Macro
//!
//! To create a custom macro, implement the [`Macroforge`] trait:
//!
//! ```rust,no_run
//! use macroforge_ts::host::Macroforge;
//! use macroforge_ts::ts_syn::{TsStream, abi::{MacroKind, MacroResult}};
//!
//! struct MyMacro;
//!
//! impl Macroforge for MyMacro {
//! fn name(&self) -> &str { "MyMacro" }
//! fn kind(&self) -> MacroKind { MacroKind::Derive }
//!
//! fn run(&self, input: TsStream) -> MacroResult {
//! // Process the input and generate patches
//! MacroResult::default()
//! }
//!
//! fn description(&self) -> &str {
//! "My custom macro that does something useful"
//! }
//! }
//! ```
//!
//! ## ABI Versioning
//!
//! The ABI version ensures compatibility between macros and the host.
//! If a macro's ABI version doesn't match the host's expected version,
//! execution will be refused to prevent crashes or undefined behavior.
use crateTsStream;
use crate;
/// The core trait that all TypeScript macros must implement.
///
/// This trait defines the contract between a macro implementation and the
/// macro host. Macros receive a [`TsStream`] containing the decorated item
/// (class, interface, etc.) and return a [`MacroResult`] with patches and
/// diagnostics.
///
/// # Thread Safety
///
/// Macros must be `Send + Sync` because:
/// - Multiple files may be processed concurrently
/// - The macro registry is shared across threads
/// - Macro instances are stored in a `DashMap`
///
/// # Example
///
/// ```rust,no_run
/// use macroforge_ts::host::{Macroforge, MacroKind, MacroResult, Patch};
/// use macroforge_ts::ts_syn::{TsStream, TargetIR, SpanIR};
///
/// struct GreetMacro;
///
/// impl Macroforge for GreetMacro {
/// fn name(&self) -> &str { "Greet" }
/// fn kind(&self) -> MacroKind { MacroKind::Derive }
///
/// fn run(&self, input: TsStream) -> MacroResult {
/// // Get the macro context (contains class/enum info)
/// let ctx = match input.context() {
/// Some(ctx) => ctx,
/// None => return MacroResult::default(),
/// };
///
/// // Extract the class name and body span
/// let (class_name, body_span) = match &ctx.target {
/// TargetIR::Class(class) => (&class.name, class.body_span),
/// _ => return MacroResult::default(),
/// };
///
/// // Generate a greeting method
/// let method = format!(
/// "greet(): string {{ return \"Hello from {}!\"; }}",
/// class_name
/// );
///
/// // Return a patch to insert the method into the class body
/// MacroResult {
/// runtime_patches: vec![Patch::InsertRaw {
/// at: body_span,
/// code: method,
/// context: Some("method".to_string()),
/// source_macro: Some("Greet".to_string()),
/// }],
/// ..Default::default()
/// }
/// }
/// }
/// ```
/// Trait for macro packages that provide multiple macros as a unit.
///
/// A macro package groups related macros together for distribution and
/// registration. This is useful for:
///
/// - Distributing a set of related macros (e.g., all serde-related macros)
/// - Versioning a collection of macros together
/// - Loading external macro packages dynamically
///
/// # Example
///
/// ```rust,no_run
/// use macroforge_ts::host::{Macroforge, MacroKind, MacroResult};
/// use macroforge_ts::host::traits::MacroPackage;
/// use macroforge_ts::ts_syn::TsStream;
///
/// // Define some macros
/// struct DebugMacro;
/// struct CloneMacro;
///
/// impl Macroforge for DebugMacro {
/// fn name(&self) -> &str { "Debug" }
/// fn kind(&self) -> MacroKind { MacroKind::Derive }
/// fn run(&self, _: TsStream) -> MacroResult { MacroResult::default() }
/// }
///
/// impl Macroforge for CloneMacro {
/// fn name(&self) -> &str { "Clone" }
/// fn kind(&self) -> MacroKind { MacroKind::Derive }
/// fn run(&self, _: TsStream) -> MacroResult { MacroResult::default() }
/// }
///
/// // Create a package containing multiple macros
/// struct MyPackage;
///
/// impl MacroPackage for MyPackage {
/// fn package_name(&self) -> &str { "my-macros" }
///
/// fn macros(&self) -> Vec<Box<dyn Macroforge>> {
/// vec![
/// Box::new(DebugMacro),
/// Box::new(CloneMacro),
/// ]
/// }
///
/// fn version(&self) -> &str { "1.0.0" }
/// }
/// ```