ricecoder_hooks/registry/mod.rs
1//! Hook registry for storing and managing hooks
2//!
3//! The hook registry is responsible for storing, retrieving, and managing hooks.
4//! It provides a central place to register hooks that will be triggered on specific events.
5//!
6//! # Examples
7//!
8//! ```ignore
9//! use ricecoder_hooks::registry::InMemoryHookRegistry;
10//! use ricecoder_hooks::types::Hook;
11//! use ricecoder_hooks::HookRegistry;
12//!
13//! let mut registry = InMemoryHookRegistry::new();
14//!
15//! // Register a hook
16//! let hook = Hook {
17//! id: "hook-1".to_string(),
18//! name: "Format on save".to_string(),
19//! event: "file_modified".to_string(),
20//! enabled: true,
21//! // ... other fields
22//! };
23//!
24//! let hook_id = registry.register_hook(hook)?;
25//! println!("Registered hook: {}", hook_id);
26//!
27//! // List all hooks
28//! let hooks = registry.list_hooks()?;
29//! println!("Total hooks: {}", hooks.len());
30//!
31//! // List hooks for a specific event
32//! let file_hooks = registry.list_hooks_for_event("file_modified")?;
33//! println!("Hooks for file_modified: {}", file_hooks.len());
34//!
35//! // Enable/disable a hook
36//! registry.disable_hook(&hook_id)?;
37//! registry.enable_hook(&hook_id)?;
38//! # Ok::<(), Box<dyn std::error::Error>>(())
39//! ```
40
41pub mod storage;
42
43pub use storage::InMemoryHookRegistry;
44
45use crate::error::Result;
46use crate::types::Hook;
47
48/// Trait for managing hooks
49///
50/// The `HookRegistry` trait defines the interface for storing and managing hooks.
51/// Implementations must support registering, unregistering, querying, and enabling/disabling hooks.
52///
53/// # Thread Safety
54///
55/// All implementations must be thread-safe (`Send + Sync`) to support concurrent access.
56pub trait HookRegistry: Send + Sync {
57 /// Register a new hook
58 ///
59 /// Stores a hook in the registry and returns its unique ID.
60 /// The hook will be assigned a unique ID if not already set.
61 ///
62 /// # Arguments
63 ///
64 /// * `hook` - The hook to register
65 ///
66 /// # Returns
67 ///
68 /// The unique ID of the registered hook
69 ///
70 /// # Errors
71 ///
72 /// Returns an error if the hook is invalid or registration fails
73 fn register_hook(&mut self, hook: Hook) -> Result<String>;
74
75 /// Unregister a hook by ID
76 ///
77 /// Removes a hook from the registry.
78 ///
79 /// # Arguments
80 ///
81 /// * `hook_id` - The ID of the hook to unregister
82 ///
83 /// # Errors
84 ///
85 /// Returns an error if the hook is not found
86 fn unregister_hook(&self, hook_id: &str) -> Result<()>;
87
88 /// Get a hook by ID
89 ///
90 /// Retrieves a hook from the registry by its ID.
91 ///
92 /// # Arguments
93 ///
94 /// * `hook_id` - The ID of the hook to retrieve
95 ///
96 /// # Returns
97 ///
98 /// The hook with the specified ID
99 ///
100 /// # Errors
101 ///
102 /// Returns an error if the hook is not found
103 fn get_hook(&self, hook_id: &str) -> Result<Hook>;
104
105 /// List all hooks
106 ///
107 /// Returns all hooks in the registry.
108 ///
109 /// # Returns
110 ///
111 /// A vector of all hooks
112 ///
113 /// # Errors
114 ///
115 /// Returns an error if listing fails
116 fn list_hooks(&self) -> Result<Vec<Hook>>;
117
118 /// List hooks for a specific event
119 ///
120 /// Returns all hooks registered for a specific event type.
121 /// Only enabled hooks are returned.
122 ///
123 /// # Arguments
124 ///
125 /// * `event` - The event type to filter by
126 ///
127 /// # Returns
128 ///
129 /// A vector of hooks for the specified event
130 ///
131 /// # Errors
132 ///
133 /// Returns an error if listing fails
134 fn list_hooks_for_event(&self, event: &str) -> Result<Vec<Hook>>;
135
136 /// Enable a hook
137 ///
138 /// Enables a hook so it will be triggered when its event occurs.
139 ///
140 /// # Arguments
141 ///
142 /// * `hook_id` - The ID of the hook to enable
143 ///
144 /// # Errors
145 ///
146 /// Returns an error if the hook is not found
147 fn enable_hook(&mut self, hook_id: &str) -> Result<()>;
148
149 /// Disable a hook
150 ///
151 /// Disables a hook so it will not be triggered when its event occurs.
152 ///
153 /// # Arguments
154 ///
155 /// * `hook_id` - The ID of the hook to disable
156 ///
157 /// # Errors
158 ///
159 /// Returns an error if the hook is not found
160 fn disable_hook(&mut self, hook_id: &str) -> Result<()>;
161}