Skip to main content

reovim_kernel/api/
buffer_manager.rs

1//! Buffer manager trait for kernel-driver communication.
2//!
3//! Defines the interface for buffer storage and retrieval. The kernel provides
4//! pure storage mechanisms; drivers handle I/O operations (loading, saving).
5
6use std::{fmt, sync::Arc};
7
8use reovim_arch::sync::RwLock;
9
10use crate::mm::{Buffer, BufferId};
11
12// ============================================================================
13// Error Types
14// ============================================================================
15
16/// Error type for buffer manager operations.
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum BufferError {
19    /// Buffer with the given ID was not found.
20    NotFound(BufferId),
21    /// Buffer with the given ID already exists.
22    AlreadyExists(BufferId),
23    /// Invalid operation attempted.
24    InvalidOperation(&'static str),
25}
26
27impl fmt::Display for BufferError {
28    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29        match self {
30            Self::NotFound(id) => write!(f, "buffer not found: {id:?}"),
31            Self::AlreadyExists(id) => write!(f, "buffer already exists: {id:?}"),
32            Self::InvalidOperation(msg) => write!(f, "invalid operation: {msg}"),
33        }
34    }
35}
36
37impl std::error::Error for BufferError {}
38
39// ============================================================================
40// BufferManager Trait
41// ============================================================================
42
43/// Buffer manager interface for kernel-driver communication.
44///
45/// This trait defines how buffers are stored and retrieved. The kernel provides
46/// pure storage mechanisms, while drivers implement I/O operations.
47///
48/// # Design Philosophy
49///
50/// - **No I/O operations**: No `open()/save()` methods - VFS driver handles these
51/// - **No focus tracking**: Active buffer tracking is a runtime/window concern
52/// - **Thread-safe**: Uses `Arc<RwLock<Buffer>>` for concurrent access
53/// - **Kernel purity**: Pure mechanisms only, no external dependencies
54///
55/// # Example
56///
57/// ```ignore
58/// use reovim_kernel::api::v1::{BufferManager, Buffer, BufferId};
59///
60/// struct SimpleBufferManager { /* ... */ }
61///
62/// impl BufferManager for SimpleBufferManager {
63///     fn get(&self, id: BufferId) -> Option<Arc<RwLock<Buffer>>> {
64///         // Implementation
65///     }
66///     // ... other methods
67/// }
68/// ```
69pub trait BufferManager: Send + Sync {
70    /// Get buffer by ID.
71    ///
72    /// Returns `None` if the buffer does not exist.
73    fn get(&self, id: BufferId) -> Option<Arc<RwLock<Buffer>>>;
74
75    /// Create a new empty buffer.
76    ///
77    /// Returns the ID of the newly created buffer.
78    fn create(&self) -> BufferId;
79
80    /// Register an existing buffer (used by drivers after loading).
81    ///
82    /// Returns the ID assigned to the registered buffer.
83    fn register(&self, buffer: Buffer) -> BufferId;
84
85    /// Unregister buffer, returning ownership.
86    ///
87    /// # Errors
88    ///
89    /// Returns `Err(BufferError::NotFound)` if the buffer does not exist.
90    fn unregister(&self, id: BufferId) -> Result<Buffer, BufferError>;
91
92    /// List all buffer IDs. Order is not guaranteed; callers that need
93    /// deterministic ordering must sort the result.
94    fn list(&self) -> Vec<BufferId>;
95
96    /// Get count of buffers.
97    fn count(&self) -> usize;
98}