use super::SyncDocument;
use crate::core::errors::EditorError;
use crate::core::{EditorDocument, Result};
#[cfg(feature = "std")]
use std::sync::{Arc, RwLock};
#[cfg(not(feature = "std"))]
use alloc::sync::Arc;
#[cfg(feature = "concurrency")]
#[derive(Debug, Clone)]
pub struct DocumentPool {
documents: Arc<RwLock<std::collections::HashMap<String, SyncDocument>>>,
}
#[cfg(feature = "concurrency")]
impl DocumentPool {
pub fn new() -> Self {
Self {
documents: Arc::new(RwLock::new(std::collections::HashMap::new())),
}
}
pub fn add_document(&self, document: EditorDocument) -> Result<String> {
let id = document.id().to_string();
let sync_doc = SyncDocument::new(document);
let mut docs = self
.documents
.write()
.map_err(|_| EditorError::ThreadSafetyError {
message: "Failed to acquire pool write lock".to_string(),
})?;
docs.insert(id.clone(), sync_doc);
Ok(id)
}
pub fn get_document(&self, id: &str) -> Result<SyncDocument> {
let docs = self
.documents
.read()
.map_err(|_| EditorError::ThreadSafetyError {
message: "Failed to acquire pool read lock".to_string(),
})?;
docs.get(id)
.cloned()
.ok_or_else(|| EditorError::ValidationError {
message: format!("Document not found: {id}"),
})
}
pub fn remove_document(&self, id: &str) -> Result<()> {
let mut docs = self
.documents
.write()
.map_err(|_| EditorError::ThreadSafetyError {
message: "Failed to acquire pool write lock".to_string(),
})?;
docs.remove(id);
Ok(())
}
pub fn list_documents(&self) -> Result<Vec<String>> {
let docs = self
.documents
.read()
.map_err(|_| EditorError::ThreadSafetyError {
message: "Failed to acquire pool read lock".to_string(),
})?;
Ok(docs.keys().cloned().collect())
}
pub fn document_count(&self) -> Result<usize> {
let docs = self
.documents
.read()
.map_err(|_| EditorError::ThreadSafetyError {
message: "Failed to acquire pool read lock".to_string(),
})?;
Ok(docs.len())
}
}
#[cfg(feature = "concurrency")]
impl Default for DocumentPool {
fn default() -> Self {
Self::new()
}
}