reovim_driver_command/provider.rs
1//! Command provider trait for module command registration.
2//!
3//! This trait allows modules to provide command handlers for registration
4//! without modifying the kernel Module trait. It maintains kernel purity
5//! by keeping driver-layer concerns in the driver layer.
6//!
7//! # Architecture
8//!
9//! ```text
10//! Kernel Layer Driver Layer
11//! ┌──────────────────┐ ┌─────────────────────┐
12//! │ Module trait │ │ CommandProvider │
13//! │ (identity, deps) │ │ (handlers) │
14//! └──────────────────┘ └─────────────────────┘
15//! │ │
16//! └───────────┬───────────────────┘
17//! │
18//! ▼
19//! ┌───────────────┐
20//! │ EditorModule │ implements both
21//! └───────────────┘
22//! ```
23//!
24//! # Example
25//!
26//! ```ignore
27//! use reovim_driver_command::{CommandHandler, CommandProvider};
28//!
29//! struct EditorModule;
30//!
31//! impl CommandProvider for EditorModule {
32//! fn command_handlers(&self) -> Vec<Box<dyn CommandHandler>> {
33//! vec![
34//! Box::new(CursorUp),
35//! Box::new(CursorDown),
36//! // ...
37//! ]
38//! }
39//! }
40//! ```
41
42use super::CommandHandler;
43
44/// Trait for modules that provide command handlers.
45///
46/// Modules implement this trait to register their command handlers.
47/// The runner queries this trait during module loading to wire commands.
48///
49/// # Design
50///
51/// This trait is separate from the kernel's `Module` trait to maintain
52/// kernel purity. The kernel defines module identity and lifecycle;
53/// command handlers are a driver-layer concern.
54///
55/// # Thread Safety
56///
57/// The trait requires `Send + Sync` because modules may be accessed
58/// from multiple threads during command registration and execution.
59///
60/// # Example
61///
62/// ```ignore
63/// use reovim_driver_command::{CommandHandler, CommandProvider};
64///
65/// struct EditorModule;
66///
67/// impl CommandProvider for EditorModule {
68/// fn command_handlers(&self) -> Vec<Box<dyn CommandHandler>> {
69/// vec![
70/// Box::new(CursorUp),
71/// Box::new(CursorDown),
72/// Box::new(DeleteChar),
73/// ]
74/// }
75/// }
76/// ```
77pub trait CommandProvider: Send + Sync {
78 /// Get command handlers to register.
79 ///
80 /// Returns boxed handlers that the runner will register in the
81 /// command registry. Each handler must implement `CommandHandler`.
82 ///
83 /// # Implementation Notes
84 ///
85 /// - Return a fresh `Vec` each time (handlers are moved into the registry)
86 /// - Use `Box::new()` for each command implementation
87 /// - Commands should use `CommandId` constants from the module's `ids` module
88 fn command_handlers(&self) -> Vec<Box<dyn CommandHandler>>;
89}
90
91#[cfg(test)]
92#[path = "provider_tests.rs"]
93mod tests;