pub trait Plugin: Send + Sync {
// Required method
fn name(&self) -> &'static str;
// Provided methods
fn on_before_phase(
&self,
phase: &str,
ctx: &mut CompilationContext,
) -> Result<()> { ... }
fn on_after_phase(
&self,
phase: &str,
ctx: &mut CompilationContext,
) -> Result<()> { ... }
}Expand description
A plugin that can hook into the compilation pipeline.
Plugins receive callbacks before and after each phase runs, allowing them to inspect or modify the compilation context.
§Example
ⓘ
struct TimingPlugin {
start_times: RefCell<HashMap<String, Instant>>,
}
impl Plugin for TimingPlugin {
fn name(&self) -> &'static str { "timing" }
fn on_before_phase(&self, phase: &str, _ctx: &mut CompilationContext) -> Result<()> {
self.start_times.borrow_mut().insert(phase.to_string(), Instant::now());
Ok(())
}
fn on_after_phase(&self, phase: &str, _ctx: &mut CompilationContext) -> Result<()> {
if let Some(start) = self.start_times.borrow().get(phase) {
println!("{} took {:?}", phase, start.elapsed());
}
Ok(())
}
}