Mountain/ApplicationState/State/ExtensionState/ScannedExtensions/
ScannedExtensions.rs

1//! # ScannedExtensions Module (ApplicationState)
2//!
3//! ## RESPONSIBILITIES
4//! Manages scanned extensions metadata state including extension descriptions,
5//! capabilities, and identifiers.
6//!
7//! ## ARCHITECTURAL ROLE
8//! ScannedExtensions is part of the **ExtensionState** module, representing
9//! discovered extensions metadata state.
10//!
11//! ## KEY COMPONENTS
12//! - Extensions: Main struct containing scanned extensions map
13//! - Default: Initialization implementation
14//! - Helper methods: Extension 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 extension validation invariants
30//! - [ ] Implement extension discovery events
31//! - [ ] Add extension metrics collection
32
33use std::{
34	collections::HashMap,
35	sync::{Arc, Mutex as StandardMutex},
36};
37
38use log::debug;
39
40use crate::ApplicationState::DTO::ExtensionDescriptionStateDTO::ExtensionDescriptionStateDTO;
41
42/// Scanned extensions containing discovered extension metadata.
43#[derive(Clone)]
44pub struct Extensions {
45	/// Scanned extensions by identifier.
46	pub ScannedExtensions:Arc<StandardMutex<HashMap<String, ExtensionDescriptionStateDTO>>>,
47}
48
49impl Default for Extensions {
50	fn default() -> Self {
51		debug!("[ScannedExtensions] Initializing default scanned extensions...");
52
53		Self { ScannedExtensions:Arc::new(StandardMutex::new(HashMap::new())) }
54	}
55}
56
57impl Extensions {
58	/// Gets all scanned extensions.
59	pub fn GetAll(&self) -> HashMap<String, ExtensionDescriptionStateDTO> {
60		self.ScannedExtensions
61			.lock()
62			.ok()
63			.map(|guard| guard.clone())
64			.unwrap_or_default()
65	}
66
67	/// Gets an extension by its identifier.
68	pub fn Get(&self, identifier:&str) -> Option<ExtensionDescriptionStateDTO> {
69		self.ScannedExtensions
70			.lock()
71			.ok()
72			.and_then(|guard| guard.get(identifier).cloned())
73	}
74
75	/// Sets all scanned extensions.
76	pub fn SetAll(&self, extensions:HashMap<String, ExtensionDescriptionStateDTO>) {
77		if let Ok(mut guard) = self.ScannedExtensions.lock() {
78			*guard = extensions;
79			debug!("[ScannedExtensions] Scanned extensions updated ({} extensions)", guard.len());
80		}
81	}
82
83	/// Adds or updates an extension.
84	pub fn AddOrUpdate(&self, identifier:String, extension:ExtensionDescriptionStateDTO) {
85		if let Ok(mut guard) = self.ScannedExtensions.lock() {
86			guard.insert(identifier, extension);
87			debug!("[ScannedExtensions] Extension added/updated");
88		}
89	}
90
91	/// Removes an extension by its identifier.
92	pub fn Remove(&self, identifier:&str) {
93		if let Ok(mut guard) = self.ScannedExtensions.lock() {
94			guard.remove(identifier);
95			debug!("[ScannedExtensions] Extension removed: {}", identifier);
96		}
97	}
98
99	/// Clears all scanned extensions.
100	pub fn Clear(&self) {
101		if let Ok(mut guard) = self.ScannedExtensions.lock() {
102			guard.clear();
103			debug!("[ScannedExtensions] All extensions cleared");
104		}
105	}
106
107	/// Gets the count of scanned extensions.
108	pub fn Count(&self) -> usize { self.ScannedExtensions.lock().ok().map(|guard| guard.len()).unwrap_or(0) }
109
110	/// Checks if an extension exists.
111	pub fn Contains(&self, identifier:&str) -> bool {
112		self.ScannedExtensions
113			.lock()
114			.ok()
115			.map(|guard| guard.contains_key(identifier))
116			.unwrap_or(false)
117	}
118}