Mountain/ApplicationState/State/FeatureState/Documents/
DocumentState.rs

1//! # DocumentState Module (ApplicationState)
2//!
3//! ## RESPONSIBILITIES
4//! Manages open documents state including document metadata, content, and
5//! version tracking.
6//!
7//! ## ARCHITECTURAL ROLE
8//! DocumentState is part of the **FeatureState** module, representing
9//! open documents state organized by document URI.
10//!
11//! ## KEY COMPONENTS
12//! - DocumentState: Main struct containing open documents map
13//! - Default: Initialization implementation
14//! - Helper methods: Document manipulation utilities
15//!
16//! ## ERROR HANDLING
17//! - Thread-safe access via `Arc<Mutex<...>>`
18//! - Proper lock error handling with `MapLockError` helpers
19//!
20//! ## LOGGING
21//! State changes are logged at appropriate levels (debug, info, warn, error).
22//!
23//! ## PERFORMANCE CONSIDERATIONS
24//! - Lock mutexes briefly and release immediately
25//! - Avoid nested locks to prevent deadlocks
26//! - Use Arc for shared ownership across threads
27//!
28//! ## TODO
29//! - [ ] Add document validation invariants
30//! - [ ] Implement document lifecycle events
31//! - [ ] Add document metrics collection
32
33use std::{
34	collections::HashMap,
35	sync::{Arc, Mutex as StandardMutex},
36};
37
38use log::debug;
39
40use crate::ApplicationState::DTO::DocumentStateDTO::DocumentStateDTO;
41
42/// Open documents state containing documents by URI.
43#[derive(Clone)]
44pub struct DocumentState {
45	/// Open documents organized by URI.
46	pub OpenDocuments:Arc<StandardMutex<HashMap<String, DocumentStateDTO>>>,
47}
48
49impl Default for DocumentState {
50	fn default() -> Self {
51		debug!("[DocumentState] Initializing default document state...");
52
53		Self { OpenDocuments:Arc::new(StandardMutex::new(HashMap::new())) }
54	}
55}
56
57impl DocumentState {
58	/// Gets all open documents.
59	pub fn GetAll(&self) -> HashMap<String, DocumentStateDTO> {
60		self.OpenDocuments.lock().ok().map(|guard| guard.clone()).unwrap_or_default()
61	}
62
63	/// Gets a document by its URI.
64	pub fn Get(&self, uri:&str) -> Option<DocumentStateDTO> {
65		self.OpenDocuments.lock().ok().and_then(|guard| guard.get(uri).cloned())
66	}
67
68	/// Adds or updates a document.
69	pub fn AddOrUpdate(&self, uri:String, document:DocumentStateDTO) {
70		if let Ok(mut guard) = self.OpenDocuments.lock() {
71			guard.insert(uri, document);
72			debug!("[DocumentState] Document added/updated");
73		}
74	}
75
76	/// Removes a document by its URI.
77	pub fn Remove(&self, uri:&str) {
78		if let Ok(mut guard) = self.OpenDocuments.lock() {
79			guard.remove(uri);
80			debug!("[DocumentState] Document removed: {}", uri);
81		}
82	}
83
84	/// Clears all open documents.
85	pub fn Clear(&self) {
86		if let Ok(mut guard) = self.OpenDocuments.lock() {
87			guard.clear();
88			debug!("[DocumentState] All documents cleared");
89		}
90	}
91
92	/// Gets the count of open documents.
93	pub fn Count(&self) -> usize { self.OpenDocuments.lock().ok().map(|guard| guard.len()).unwrap_or(0) }
94
95	/// Checks if a document exists.
96	pub fn Contains(&self, uri:&str) -> bool {
97		self.OpenDocuments
98			.lock()
99			.ok()
100			.map(|guard| guard.contains_key(uri))
101			.unwrap_or(false)
102	}
103
104	/// Gets all document URIs.
105	pub fn GetURIs(&self) -> Vec<String> {
106		self.OpenDocuments
107			.lock()
108			.ok()
109			.map(|guard| guard.keys().cloned().collect())
110			.unwrap_or_default()
111	}
112}