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