Skip to main content

Plugin

Trait Plugin 

Source
pub trait Plugin: Send + Sync {
    // Required method
    fn name(&self) -> &str;

    // Provided methods
    fn version(&self) -> &str { ... }
    fn dependencies(&self) -> &[&str] { ... }
    fn config_schema(&self) -> Option<JsonValue> { ... }
    fn on_register<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn on_init<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
    fn on_shutdown<'life0, 'async_trait>(
        &'life0 self,
    ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
       where Self: 'async_trait,
             'life0: 'async_trait { ... }
}
Expand description

插件接口

所有插件必须实现此 trait

§示例

ⓘ
use yang_base::plugin::Plugin;
use async_trait::async_trait;

struct MyPlugin;

#[async_trait]
impl Plugin for MyPlugin {
    fn name(&self) -> &str {
        "my_plugin"
    }
     
    fn version(&self) -> &str {
        "1.0.0"
    }
     
}

Required Methods§

Source

fn name(&self) -> &str

获取插件名称

插件名称必须唯一,用于标识和查找插件

§返回
  • 插件名称字符串

Provided Methods§

Source

fn version(&self) -> &str

获取插件版本

使用语义化版本号,格式:major.minor.patch

§返回
  • 版本号字符串,默认为 “0.1.0”
Source

fn dependencies(&self) -> &[&str]

获取插件依赖列表

返回当前插件依赖的其他插件名称列表 系统会确保依赖插件先于当前插件初始化

§返回
  • 依赖插件名称列表,默认为空
Source

fn config_schema(&self) -> Option<JsonValue>

获取插件配置 Schema

返回 JSON Schema 格式的配置定义

§返回
  • Some(JsonValue): 配置 Schema
  • None: 无配置要求(默认)
Source

fn on_register<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

插件注册时的回调

在插件被注册到 PluginManager 时调用

§返回
  • Ok(()): 注册成功
  • Err: 注册失败
Source

fn on_init<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

数据库初始化后的回调

在插件的数据库表创建完成后调用 可用于插入初始数据或执行其他初始化逻辑

§返回
  • Ok(()): 初始化成功
  • Err: 初始化失败
Source

fn on_shutdown<'life0, 'async_trait>( &'life0 self, ) -> Pin<Box<dyn Future<Output = Result<(), PluginError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

系统关闭时的回调

在系统关闭时调用,用于清理资源

§返回
  • Ok(()): 关闭成功
  • Err: 关闭失败

Dyn Compatibility§

This trait is dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementors§