Skip to main content

Mountain/ApplicationState/State/FeatureState/TreeViews/
TreeViewState.rs

1//! # TreeViewState Module (ApplicationState)
2//!
3//! ## RESPONSIBILITIES
4//! Manages tree view providers state including tree view metadata, data,
5//! and presentation state.
6//!
7//! ## ARCHITECTURAL ROLE
8//! TreeViewState is part of the **FeatureState** module, representing
9//! tree view providers state organized by tree view ID.
10//!
11//! ## KEY COMPONENTS
12//! - TreeViewState: Main struct containing active tree views map
13//! - Default: Initialization implementation
14//! - Helper methods: Tree view 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 tree view validation invariants
30//! - [ ] Implement tree view lifecycle events
31//! - [ ] Add tree view metrics collection
32
33use std::{
34	collections::HashMap,
35	sync::{Arc, Mutex as StandardMutex},
36};
37
38use crate::{ApplicationState::DTO::TreeViewStateDTO::TreeViewStateDTO, dev_log};
39
40/// Active tree views state containing tree views by ID.
41#[derive(Clone)]
42pub struct TreeViewState {
43	/// Active tree views organized by ID.
44	pub ActiveTreeViews:Arc<StandardMutex<HashMap<String, TreeViewStateDTO>>>,
45}
46
47impl Default for TreeViewState {
48	fn default() -> Self {
49		dev_log!("extensions", "[TreeViewState] Initializing default tree view state...");
50
51		Self { ActiveTreeViews:Arc::new(StandardMutex::new(HashMap::new())) }
52	}
53}
54
55impl TreeViewState {
56	/// Gets all active tree views.
57	pub fn GetAll(&self) -> HashMap<String, TreeViewStateDTO> {
58		self.ActiveTreeViews.lock().ok().map(|guard| guard.clone()).unwrap_or_default()
59	}
60
61	/// Gets a tree view by its ID.
62	pub fn Get(&self, id:&str) -> Option<TreeViewStateDTO> {
63		self.ActiveTreeViews.lock().ok().and_then(|guard| guard.get(id).cloned())
64	}
65
66	/// Adds or updates a tree view.
67	pub fn AddOrUpdate(&self, id:String, tree_view:TreeViewStateDTO) {
68		if let Ok(mut guard) = self.ActiveTreeViews.lock() {
69			guard.insert(id, tree_view);
70			dev_log!("extensions", "[TreeViewState] Tree view added/updated");
71		}
72	}
73
74	/// Removes a tree view by its ID.
75	pub fn Remove(&self, id:&str) {
76		if let Ok(mut guard) = self.ActiveTreeViews.lock() {
77			guard.remove(id);
78			dev_log!("extensions", "[TreeViewState] Tree view removed: {}", id);
79		}
80	}
81
82	/// Clears all active tree views.
83	pub fn Clear(&self) {
84		if let Ok(mut guard) = self.ActiveTreeViews.lock() {
85			guard.clear();
86			dev_log!("extensions", "[TreeViewState] All tree views cleared");
87		}
88	}
89
90	/// Gets the count of active tree views.
91	pub fn Count(&self) -> usize { self.ActiveTreeViews.lock().ok().map(|guard| guard.len()).unwrap_or(0) }
92
93	/// Checks if a tree view exists.
94	pub fn Contains(&self, id:&str) -> bool {
95		self.ActiveTreeViews
96			.lock()
97			.ok()
98			.map(|guard| guard.contains_key(id))
99			.unwrap_or(false)
100	}
101}