Mountain/ApplicationState/State/WorkspaceState/
WorkspaceState.rs

1//! # WorkspaceState Module (ApplicationState)
2//!
3//! ## RESPONSIBILITIES
4//! Manages workspace-related state including workspace folders, workspace
5//! trust status, workspace configuration path, window state, and the currently
6//! active document URI.
7//!
8//! ## ARCHITECTURAL ROLE
9//! WorkspaceState is part of the **state organization layer**, representing
10//! all workspace-specific state in the application. This includes:
11//! - Workspace folders currently open
12//! - Workspace configuration file path
13//! - Workspace trust/security status
14//! - Main window presentation state
15//! - Currently active document
16//!
17//! ## KEY COMPONENTS
18//! - State: Main struct containing workspace-related fields
19//! - Default: Initialization implementation
20//! - Helper methods: Workspace manipulation utilities
21//!
22//! ## ERROR HANDLING
23//! - Thread-safe access via `Arc<Mutex<...>>`
24//! - Proper lock error handling with `MapLockError` helpers
25//! - Atomic operations for simple types (IsTrusted)
26//!
27//! ## LOGGING
28//! State changes are logged at appropriate levels (debug, info, warn, error).
29//!
30//! ## PERFORMANCE CONSIDERATIONS
31//! - Lock mutexes briefly and release immediately
32//! - Avoid nested locks to prevent deadlocks
33//! - Use Arc for shared ownership across threads
34//! - Use AtomicBool for simple lock-free reads
35//!
36//! ## TODO
37//! - [ ] Add workspace validation invariants
38//! - [ ] Implement workspace change events
39//! - [ ] Add workspace metrics collection
40
41use std::sync::{
42	Arc,
43	Mutex as StandardMutex,
44	atomic::{AtomicBool, Ordering as AtomicOrdering},
45};
46
47use log::debug;
48
49use crate::ApplicationState::DTO::{WindowStateDTO::WindowStateDTO, WorkspaceFolderStateDTO::WorkspaceFolderStateDTO};
50
51/// Workspace state containing all workspace-related fields.
52#[derive(Clone)]
53pub struct State {
54	/// Currently open workspace folders.
55	pub WorkspaceFolders:Arc<StandardMutex<Vec<WorkspaceFolderStateDTO>>>,
56
57	/// Path to the workspace configuration file (if any).
58	pub WorkspaceConfigurationPath:Arc<StandardMutex<Option<std::path::PathBuf>>>,
59
60	/// Workspace trust status (security).
61	pub IsTrusted:Arc<AtomicBool>,
62
63	/// Main window presentation state.
64	pub WindowState:Arc<StandardMutex<WindowStateDTO>>,
65
66	/// Currently active document URI.
67	pub ActiveDocumentURI:Arc<StandardMutex<Option<String>>>,
68}
69
70impl Default for State {
71	fn default() -> Self {
72		debug!("[WorkspaceState] Initializing default workspace state...");
73
74		Self {
75			WorkspaceFolders:Arc::new(StandardMutex::new(Vec::new())),
76			WorkspaceConfigurationPath:Arc::new(StandardMutex::new(None)),
77			IsTrusted:Arc::new(AtomicBool::new(false)),
78			WindowState:Arc::new(StandardMutex::new(WindowStateDTO::default())),
79			ActiveDocumentURI:Arc::new(StandardMutex::new(None)),
80		}
81	}
82}
83
84impl State {
85	/// Gets the current workspace trust status.
86	pub fn GetTrustStatus(&self) -> bool { self.IsTrusted.load(AtomicOrdering::Relaxed) }
87
88	/// Sets the workspace trust status.
89	pub fn SetTrustStatus(&self, trusted:bool) {
90		self.IsTrusted.store(trusted, AtomicOrdering::Relaxed);
91		debug!("[WorkspaceState] Trust status set to: {}", trusted);
92	}
93
94	/// Gets the workspace configuration path.
95	pub fn GetConfigurationPath(&self) -> Option<std::path::PathBuf> {
96		self.WorkspaceConfigurationPath.lock().ok().and_then(|guard| guard.clone())
97	}
98
99	/// Sets the workspace configuration path.
100	pub fn SetConfigurationPath(&self, path:Option<std::path::PathBuf>) {
101		if let Ok(mut guard) = self.WorkspaceConfigurationPath.lock() {
102			*guard = path.clone();
103			debug!("[WorkspaceState] Configuration path updated to: {:?}", path);
104		}
105	}
106
107	/// Gets the currently active document URI.
108	pub fn GetActiveDocumentURI(&self) -> Option<String> {
109		self.ActiveDocumentURI.lock().ok().and_then(|guard| guard.clone())
110	}
111
112	/// Sets the currently active document URI.
113	pub fn SetActiveDocumentURI(&self, uri:Option<String>) {
114		if let Ok(mut guard) = self.ActiveDocumentURI.lock() {
115			*guard = uri.clone();
116			debug!("[WorkspaceState] Active document URI updated to: {:?}", uri);
117		}
118	}
119
120	/// Gets all workspace folders.
121	pub fn GetWorkspaceFolders(&self) -> Vec<WorkspaceFolderStateDTO> {
122		self.WorkspaceFolders.lock().ok().map(|guard| guard.clone()).unwrap_or_default()
123	}
124
125	/// Sets the workspace folders.
126	pub fn SetWorkspaceFolders(&self, folders:Vec<WorkspaceFolderStateDTO>) {
127		if let Ok(mut guard) = self.WorkspaceFolders.lock() {
128			*guard = folders;
129			debug!("[WorkspaceState] Workspace folders updated ({} folders)", guard.len());
130		}
131	}
132
133	/// Gets the window state.
134	pub fn GetWindowState(&self) -> WindowStateDTO {
135		self.WorkspaceFolders
136			.lock()
137			.ok()
138			.and_then(|_| self.WindowState.lock().ok().map(|guard| guard.clone()))
139			.unwrap_or_default()
140	}
141
142	/// Sets the window state.
143	pub fn SetWindowState(&self, state:WindowStateDTO) {
144		if let Ok(mut guard) = self.WindowState.lock() {
145			*guard = state;
146			debug!("[WorkspaceState] Window state updated");
147		}
148	}
149}