Skip to main content

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 crate::{ApplicationState::DTO::ExtensionDescriptionStateDTO::ExtensionDescriptionStateDTO, dev_log};
39
40/// Scanned extensions containing discovered extension metadata.
41#[derive(Clone)]
42pub struct ScannedExtensionCollection {
43	/// Scanned extensions by identifier.
44	pub ScannedExtensions:Arc<StandardMutex<HashMap<String, ExtensionDescriptionStateDTO>>>,
45}
46
47impl Default for ScannedExtensionCollection {
48	fn default() -> Self {
49		dev_log!("extensions", "[ScannedExtensions] Initializing default scanned extensions...");
50
51		Self { ScannedExtensions:Arc::new(StandardMutex::new(HashMap::new())) }
52	}
53}
54
55impl ScannedExtensionCollection {
56	/// Gets all scanned extensions.
57	pub fn GetAll(&self) -> HashMap<String, ExtensionDescriptionStateDTO> {
58		self.ScannedExtensions
59			.lock()
60			.ok()
61			.map(|guard| guard.clone())
62			.unwrap_or_default()
63	}
64
65	/// Gets an extension by its identifier.
66	pub fn Get(&self, identifier:&str) -> Option<ExtensionDescriptionStateDTO> {
67		self.ScannedExtensions
68			.lock()
69			.ok()
70			.and_then(|guard| guard.get(identifier).cloned())
71	}
72
73	/// Sets all scanned extensions.
74	pub fn SetAll(&self, extensions:HashMap<String, ExtensionDescriptionStateDTO>) {
75		if let Ok(mut guard) = self.ScannedExtensions.lock() {
76			*guard = extensions;
77			dev_log!(
78				"extensions",
79				"[ScannedExtensions] Scanned extensions updated ({} extensions)",
80				guard.len()
81			);
82		}
83	}
84
85	/// Adds or updates an extension.
86	pub fn AddOrUpdate(&self, identifier:String, extension:ExtensionDescriptionStateDTO) {
87		if let Ok(mut guard) = self.ScannedExtensions.lock() {
88			guard.insert(identifier, extension);
89			dev_log!("extensions", "[ScannedExtensions] Extension added/updated");
90		}
91	}
92
93	/// Removes an extension by its identifier.
94	pub fn Remove(&self, identifier:&str) {
95		if let Ok(mut guard) = self.ScannedExtensions.lock() {
96			guard.remove(identifier);
97			dev_log!("extensions", "[ScannedExtensions] Extension removed: {}", identifier);
98		}
99	}
100
101	/// Clears all scanned extensions.
102	pub fn Clear(&self) {
103		if let Ok(mut guard) = self.ScannedExtensions.lock() {
104			guard.clear();
105			dev_log!("extensions", "[ScannedExtensions] All extensions cleared");
106		}
107	}
108
109	/// Gets the count of scanned extensions.
110	pub fn Count(&self) -> usize { self.ScannedExtensions.lock().ok().map(|guard| guard.len()).unwrap_or(0) }
111
112	/// Checks if an extension exists.
113	pub fn Contains(&self, identifier:&str) -> bool {
114		self.ScannedExtensions
115			.lock()
116			.ok()
117			.map(|guard| guard.contains_key(identifier))
118			.unwrap_or(false)
119	}
120}