Mountain/ApplicationState/State/ExtensionState/ScannedExtensions/
ScannedExtensions.rs1use std::{
34 collections::HashMap,
35 sync::{Arc, Mutex as StandardMutex},
36};
37
38use log::debug;
39
40use crate::ApplicationState::DTO::ExtensionDescriptionStateDTO::ExtensionDescriptionStateDTO;
41
42#[derive(Clone)]
44pub struct Extensions {
45 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 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 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 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 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 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 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 pub fn Count(&self) -> usize { self.ScannedExtensions.lock().ok().map(|guard| guard.len()).unwrap_or(0) }
109
110 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}