macroforge_ts/host/mod.rs
1//! # TypeScript Macro Host
2//!
3//! The macro host module provides the core infrastructure for TypeScript macro
4//! expansion. It handles the complete lifecycle of macro processing: registration,
5//! dispatch, execution, and patch application.
6//!
7//! ## Architecture Overview
8//!
9//! ```text
10//! ┌─────────────────────────────────────────────────────────────────┐
11//! │ MacroExpander │
12//! │ (Main entry point - coordinates the expansion process) │
13//! └─────────────────────────────────────────────────────────────────┘
14//! │
15//! ▼
16//! ┌─────────────────────────────────────────────────────────────────┐
17//! │ MacroDispatcher │
18//! │ (Routes macro calls to implementations with ABI checking) │
19//! └─────────────────────────────────────────────────────────────────┘
20//! │
21//! ▼
22//! ┌─────────────────────────────────────────────────────────────────┐
23//! │ MacroRegistry │
24//! │ (Thread-safe storage of registered macros using DashMap) │
25//! └─────────────────────────────────────────────────────────────────┘
26//! │
27//! ▼
28//! ┌─────────────────────────────────────────────────────────────────┐
29//! │ PatchApplicator │
30//! │ (Applies generated patches with source mapping) │
31//! └─────────────────────────────────────────────────────────────────┘
32//! ```
33//!
34//! ## Module Organization
35//!
36//! - [`config`] - Configuration loading and management (`macroforge.json`)
37//! - [`derived`] - Inventory-based registration for built-in derive macros
38//! - [`dispatch`] - Macro call routing and ABI version checking
39//! - [`error`] - Error types (`MacroError`) and `Result` type alias
40//! - [`expand`] - The main `MacroExpander` that orchestrates expansion
41//! - [`macros`] - Helper macros for macro registration
42//! - [`package_registry`] - Global registry for macro package registrars
43//! - [`patch_applicator`] - Applies code patches with source mapping
44//! - [`registry`] - Thread-safe macro storage (`MacroRegistry`)
45//! - [`traits`] - Core traits (`Macroforge`, `MacroPackage`)
46//!
47//! ## Key Types
48//!
49//! - [`MacroExpander`] - Main entry point for macro expansion
50//! - [`MacroDispatcher`] - Routes macros to implementations
51//! - [`MacroRegistry`] - Stores registered macros
52//! - [`Macroforge`] - Trait that all macros must implement
53//! - [`MacroConfig`] - Configuration options
54//! - [`MacroError`] - Error type for macro operations
55//!
56//! ## Usage Example
57//!
58//! ```ignore
59//! use macroforge_ts::host::{MacroExpander, MacroError};
60//!
61//! // Create a new expander (registers all built-in macros)
62//! let expander = MacroExpander::new()?;
63//!
64//! // Expand macros in source code
65//! let expansion = expander.expand(source_code, &ast, "file.ts")?;
66//!
67//! // Use the expanded code
68//! println!("Expanded: {}", expansion.code);
69//! ```
70
71/// Configuration loading and management.
72pub mod config;
73
74/// Inventory-based registration for built-in derive macros.
75pub mod derived;
76
77/// Macro call dispatching with ABI version checking.
78pub mod dispatch;
79
80/// Error types for macro operations.
81pub mod error;
82
83/// The main macro expansion engine.
84pub mod expand;
85
86/// Helper macros for macro registration.
87pub mod macros;
88
89/// Global registry for macro package registrars.
90pub mod package_registry;
91
92/// Patch application with source mapping.
93pub mod patch_applicator;
94
95/// Thread-safe macro storage.
96pub mod registry;
97
98/// Core traits for macro implementations.
99pub mod traits;
100
101// Primary exports for convenience
102pub use config::MacroConfig;
103pub use dispatch::MacroDispatcher;
104pub use error::{MacroError, Result};
105pub use expand::{MacroExpander, MacroExpansion, collect_import_sources};
106pub use package_registry::MacroPackageRegistration;
107pub use patch_applicator::{PatchApplicator, PatchCollector};
108pub use registry::MacroRegistry;
109pub use traits::Macroforge;
110
111// Re-export commonly used types from the ABI module for convenience.
112// These types are needed when implementing custom macros.
113pub use crate::ts_syn::abi::{Diagnostic, DiagnosticLevel, MacroKind, MacroResult, Patch};