pub struct MacroDispatcher { /* private fields */ }Expand description
Routes macro calls to registered implementations.
The dispatcher is the bridge between the expansion engine and individual macro implementations. It handles lookup, validation, and execution.
§Safety
The dispatcher catches all panics from macro execution, converting them to error diagnostics. This ensures a single misbehaving macro doesn’t crash the entire expansion process.
§Example
use macroforge_ts::host::{MacroRegistry, MacroDispatcher, DiagnosticLevel, MacroKind};
use macroforge_ts::ts_syn::abi::{MacroContextIR, SpanIR, TargetIR, ClassIR};
let registry = MacroRegistry::new();
// ... register macros in the registry ...
let dispatcher = MacroDispatcher::new(registry);
// Create a macro context (normally built by the expander)
let class_ir = ClassIR {
name: "User".to_string(),
span: SpanIR::new(10, 50),
body_span: SpanIR::new(20, 49),
is_abstract: false,
type_params: vec![],
heritage: vec![],
decorators: vec![],
decorators_ast: vec![],
fields: vec![],
methods: vec![],
members: vec![],
};
let macro_context = MacroContextIR {
abi_version: 1,
macro_kind: MacroKind::Derive,
macro_name: "Debug".to_string(),
module_path: "builtin".to_string(),
decorator_span: SpanIR::new(0, 10),
macro_name_span: None,
target_span: SpanIR::new(10, 50),
file_name: "test.ts".to_string(),
target: TargetIR::Class(class_ir),
target_source: "class User {}".to_string(),
};
// Dispatch the macro call
let result = dispatcher.dispatch(macro_context);
// Handle diagnostics
for diag in &result.diagnostics {
if diag.level == DiagnosticLevel::Error {
eprintln!("Error: {}", diag.message);
}
}Implementations§
Source§impl MacroDispatcher
impl MacroDispatcher
Sourcepub fn new(registry: MacroRegistry) -> Self
pub fn new(registry: MacroRegistry) -> Self
Creates a new dispatcher with the given registry.
§Arguments
registry- The macro registry to use for lookups
Sourcepub fn dispatch(&self, ctx: MacroContextIR) -> MacroResult
pub fn dispatch(&self, ctx: MacroContextIR) -> MacroResult
Dispatches a macro call to its registered implementation.
This is the main entry point for macro execution. It performs:
- Registry lookup with fallback resolution
- ABI version compatibility checking
- TsStream creation from context
- Macro execution with panic catching
§Arguments
ctx- The macro invocation context containing all information needed for execution (macro name, target code, file info, etc.)
§Returns
A MacroResult containing:
runtime_patches- Code patches to apply to the sourcetype_patches- Patches for type declarationsdiagnostics- Errors, warnings, and info messages
§Error Handling
All errors are returned as diagnostics, never as panics or Results:
- Unknown macro → Error diagnostic
- ABI mismatch → Error diagnostic with versions
- Execution panic → Error diagnostic with panic message
Sourcepub fn registry(&self) -> &MacroRegistry
pub fn registry(&self) -> &MacroRegistry
Returns a reference to the underlying registry.
Useful for debugging and introspection.
Auto Trait Implementations§
impl Freeze for MacroDispatcher
impl !RefUnwindSafe for MacroDispatcher
impl Send for MacroDispatcher
impl Sync for MacroDispatcher
impl Unpin for MacroDispatcher
impl !UnwindSafe for MacroDispatcher
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more