baobao_codegen/pipeline/plugin.rs
1//! Pipeline plugin trait for extensibility.
2
3use eyre::Result;
4
5use super::CompilationContext;
6
7/// A plugin that can hook into the compilation pipeline.
8///
9/// Plugins receive callbacks before and after each phase runs, allowing
10/// them to inspect or modify the compilation context.
11///
12/// # Example
13///
14/// ```ignore
15/// struct TimingPlugin {
16/// start_times: RefCell<HashMap<String, Instant>>,
17/// }
18///
19/// impl Plugin for TimingPlugin {
20/// fn name(&self) -> &'static str { "timing" }
21///
22/// fn on_before_phase(&self, phase: &str, _ctx: &mut CompilationContext) -> Result<()> {
23/// self.start_times.borrow_mut().insert(phase.to_string(), Instant::now());
24/// Ok(())
25/// }
26///
27/// fn on_after_phase(&self, phase: &str, _ctx: &mut CompilationContext) -> Result<()> {
28/// if let Some(start) = self.start_times.borrow().get(phase) {
29/// println!("{} took {:?}", phase, start.elapsed());
30/// }
31/// Ok(())
32/// }
33/// }
34/// ```
35pub trait Plugin: Send + Sync {
36 /// The name of this plugin (for debugging and logging).
37 fn name(&self) -> &'static str;
38
39 /// Called before a phase runs.
40 ///
41 /// # Arguments
42 ///
43 /// * `phase` - The name of the phase about to run
44 /// * `ctx` - The compilation context (can be modified)
45 ///
46 /// # Errors
47 ///
48 /// Return an error to abort the pipeline.
49 #[allow(unused_variables)]
50 fn on_before_phase(&self, phase: &str, ctx: &mut CompilationContext) -> Result<()> {
51 Ok(())
52 }
53
54 /// Called after a phase completes successfully.
55 ///
56 /// # Arguments
57 ///
58 /// * `phase` - The name of the phase that just completed
59 /// * `ctx` - The compilation context (can be modified)
60 ///
61 /// # Errors
62 ///
63 /// Return an error to abort the pipeline.
64 #[allow(unused_variables)]
65 fn on_after_phase(&self, phase: &str, ctx: &mut CompilationContext) -> Result<()> {
66 Ok(())
67 }
68}