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
impl HookRegistry
Sourcepub fn register_storage_hook(&mut self, hook: Box<dyn StorageHook>)
pub fn register_storage_hook(&mut self, hook: Box<dyn StorageHook>)
Sourcepub fn register_read_hook(&mut self, hook: Box<dyn ReadHook>)
pub fn register_read_hook(&mut self, hook: Box<dyn ReadHook>)
注册一个读取钩子
读取钩子会在文件读取时被调用。 可以注册多个读取钩子,它们会按注册顺序串行执行。
Sourcepub fn register_metadata_hook(&mut self, hook: Box<dyn MetadataHook>)
pub fn register_metadata_hook(&mut self, hook: Box<dyn MetadataHook>)
注册一个元数据钩子
元数据钩子会在文件存储时提取和验证元数据。 可以注册多个元数据钩子,提取的元数据会被合并。
Sourcepub fn register_cleanup_hook(&mut self, hook: Box<dyn CleanupHook>)
pub fn register_cleanup_hook(&mut self, hook: Box<dyn CleanupHook>)
注册一个清理钩子
清理钩子会在文件被清理删除时被调用。 可以注册多个清理钩子,每个都会在清理前后被调用。
Sourcepub fn hook_counts(&self) -> HookCounts
pub fn hook_counts(&self) -> HookCounts
获取已注册钩子的数量统计
用于调试和监控。
Trait Implementations§
Source§impl Debug for HookRegistry
impl Debug for HookRegistry
Source§impl Default for HookRegistry
impl Default for HookRegistry
Source§fn default() -> HookRegistry
fn default() -> HookRegistry
Returns the “default value” for a type. Read more
Auto Trait Implementations§
impl Freeze for HookRegistry
impl !RefUnwindSafe for HookRegistry
impl Send for HookRegistry
impl Sync for HookRegistry
impl Unpin for HookRegistry
impl UnsafeUnpin for HookRegistry
impl !UnwindSafe for HookRegistry
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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