Skip to main content

HookRegistry

Struct HookRegistry 

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

钩子注册器 - 统一管理所有类型的钩子

HookRegistry 是钩子系统的中央组件,负责:

  • 存储所有注册的钩子实例
  • 按顺序执行钩子
  • 协调各类型钩子的执行

§线程安全

HookRegistry 内部使用 std::sync::Mutex 保护状态, 因此可以在多线程环境中安全使用。

§示例:完整的钩子注册流程

use agent_diva_files::hooks::{HookRegistry, StorageHook, ReadHook, CleanupHook};
use agent_diva_files::hooks::CompressionHook;
use agent_diva_files::FileManager;

// 创建注册器
let mut registry = HookRegistry::new();

// 注册存储钩子
registry.register_storage_hook(Box::new(CompressionHook::new()));

// 注册读取钩子
registry.register_read_hook(Box::new(DecryptionHook::new()));

// 注册清理钩子
registry.register_cleanup_hook(Box::new(RetentionPolicyHook::new()));

// 将注册器传给 FileManager
let manager = FileManager::new(config, registry).await?;

§执行顺序

当多个同类型钩子注册时,它们按照注册顺序串行执行。 前一个钩子的输出(修改后的数据)会传给下一个钩子作为输入。

例如:注册了 [HookA, HookB],存储流程是:

data → HookA.before_store → HookB.before_store → [存储]

Implementations§

Source§

impl HookRegistry

Source

pub fn new() -> Self

创建新的空钩子注册器

Source

pub fn register_storage_hook(&mut self, hook: Box<dyn StorageHook>)

注册一个存储钩子

存储钩子会在文件存储时被调用。 可以注册多个存储钩子,它们会按注册顺序串行执行。

§参数
  • hook: 实现 StorageHook trait 的 Box 指针
§示例
registry.register_storage_hook(Box::new(MyStorageHook));
Source

pub fn register_read_hook(&mut self, hook: Box<dyn ReadHook>)

注册一个读取钩子

读取钩子会在文件读取时被调用。 可以注册多个读取钩子,它们会按注册顺序串行执行。

Source

pub fn register_metadata_hook(&mut self, hook: Box<dyn MetadataHook>)

注册一个元数据钩子

元数据钩子会在文件存储时提取和验证元数据。 可以注册多个元数据钩子,提取的元数据会被合并。

Source

pub fn register_cleanup_hook(&mut self, hook: Box<dyn CleanupHook>)

注册一个清理钩子

清理钩子会在文件被清理删除时被调用。 可以注册多个清理钩子,每个都会在清理前后被调用。

Source

pub fn hook_counts(&self) -> HookCounts

获取已注册钩子的数量统计

用于调试和监控。

Trait Implementations§

Source§

impl Debug for HookRegistry

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for HookRegistry

Source§

fn default() -> HookRegistry

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> 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 = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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<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