Skip to main content

nargo_transformer/passes/
example_plugin.rs

1use nargo_types::Result;
2
3/// 示例插件,用于演示插件系统的使用
4pub struct ExamplePlugin {
5    /// 插件配置
6    config: Option<super::super::PluginConfig>,
7}
8
9impl ExamplePlugin {
10    /// 创建新的示例插件
11    pub fn new() -> Self {
12        Self { config: None }
13    }
14}
15
16impl super::super::Plugin for ExamplePlugin {
17    /// 获取插件名称
18    fn name(&self) -> String {
19        "example-plugin".to_string()
20    }
21
22    /// 获取插件描述
23    fn description(&self) -> String {
24        "示例插件,用于演示插件系统的使用".to_string()
25    }
26
27    /// 初始化插件
28    fn init(&mut self, config: &super::super::PluginConfig) -> Result<()> {
29        self.config = Some(config.clone());
30        println!("ExamplePlugin initialized with config: {:?}", config);
31        Ok(())
32    }
33
34    /// 执行插件逻辑
35    fn run(&mut self, _ir: &mut super::super::IRModule, lifecycle: super::super::PluginLifecycle) -> Result<()> {
36        println!("ExamplePlugin running at lifecycle: {:?}", lifecycle);
37
38        // 根据不同的生命周期阶段执行不同的逻辑
39        match lifecycle {
40            super::super::PluginLifecycle::Init => {
41                println!("ExamplePlugin: Init phase");
42            }
43            super::super::PluginLifecycle::PreTransform => {
44                println!("ExamplePlugin: PreTransform phase");
45            }
46            super::super::PluginLifecycle::Transform => {
47                println!("ExamplePlugin: Transform phase");
48            }
49            super::super::PluginLifecycle::PostTransform => {
50                println!("ExamplePlugin: PostTransform phase");
51            }
52            super::super::PluginLifecycle::Cleanup => {
53                println!("ExamplePlugin: Cleanup phase");
54            }
55        }
56
57        Ok(())
58    }
59
60    /// 清理插件资源
61    fn cleanup(&mut self) -> Result<()> {
62        println!("ExamplePlugin cleaned up");
63        Ok(())
64    }
65}