Skip to main content

PluginManager

Struct PluginManager 

Source
pub struct PluginManager { /* private fields */ }
Expand description

插件管理器

负责插件的注册、查找和生命周期管理

§示例

ⓘ
use yang_base::plugin::{Plugin, PluginManager};

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let manager = PluginManager::new();
     
    // 注册插件
    manager.register(MyPlugin).await?;
     
    // 查找插件
    if let Some(plugin) = manager.get("my_plugin").await {
        println!("找到插件: {}", plugin.name());
    }
     
    // 获取所有插件(按依赖顺序)
    let plugins = manager.get_all().await;
     
    // 关闭所有插件
    manager.shutdown().await?;
     
    Ok(())
}

Implementations§

Source§

impl PluginManager

Source

pub fn new() -> Self

创建新的插件管理器

§返回
  • 新的 PluginManager 实例
§示例
use yang_base::plugin::PluginManager;

let manager = PluginManager::new();
Source

pub async fn register<P: Plugin + 'static>( &self, plugin: P, ) -> Result<(), BaseError>

注册插件

§参数
  • plugin: 插件实例
§返回
  • Ok(()): 注册成功
  • Err(BaseError): 注册失败(如插件名称重复)
§示例
ⓘ
use yang_base::plugin::{Plugin, PluginManager};

let manager = PluginManager::new();
manager.register(MyPlugin).await?;
Source

pub async fn get(&self, name: &str) -> Option<Arc<dyn Plugin>>

查找插件

§参数
  • name: 插件名称
§返回
  • Some(Arc<dyn Plugin>): 找到插件
  • None: 插件不存在
§示例
ⓘ
use yang_base::plugin::PluginManager;

let manager = PluginManager::new();
if let Some(plugin) = manager.get("my_plugin").await {
    println!("找到插件: {}", plugin.name());
}
Source

pub async fn get_all(&self) -> Vec<Arc<dyn Plugin>>

获取所有已注册插件

§返回
  • 插件列表(按依赖关系排序)
§示例
ⓘ
use yang_base::plugin::PluginManager;

let manager = PluginManager::new();
let plugins = manager.get_all().await;
for plugin in plugins {
    println!("插件: {} v{}", plugin.name(), plugin.version());
}
Source

pub async fn load_config( &self, name: &str, config: JsonValue, ) -> Result<(), BaseError>

加载插件配置

§参数
  • name: 插件名称
  • config: 配置 JSON
§返回
  • Ok(()): 加载成功
  • Err(BaseError): 加载失败(如配置验证失败)
§示例
ⓘ
use yang_base::plugin::PluginManager;
use serde_json::json;

let manager = PluginManager::new();
let config = json!({"key": "value"});
manager.load_config("my_plugin", config).await?;
Source

pub async fn get_config(&self, name: &str) -> Option<JsonValue>

获取插件配置

§参数
  • name: 插件名称
§返回
  • Some(JsonValue): 插件配置
  • None: 配置不存在
§示例
ⓘ
use yang_base::plugin::PluginManager;

let manager = PluginManager::new();
if let Some(config) = manager.get_config("my_plugin").await {
    println!("配置: {:?}", config);
}
Source

pub async fn shutdown(&self) -> Result<(), BaseError>

关闭所有插件

按照依赖关系的逆序调用插件的 on_shutdown 方法

§返回
  • Ok(()): 关闭成功
  • Err(BaseError): 关闭失败
§示例
ⓘ
use yang_base::plugin::PluginManager;

let manager = PluginManager::new();
// ... 注册插件 ...
manager.shutdown().await?;

Trait Implementations§

Source§

impl Default for PluginManager

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more