Mountain/ApplicationState/State/FeatureState/Documents/
DocumentState.rs1use std::{
34 collections::HashMap,
35 sync::{Arc, Mutex as StandardMutex},
36};
37
38use crate::{ApplicationState::DTO::DocumentStateDTO::DocumentStateDTO, dev_log};
39
40#[derive(Clone)]
42pub struct DocumentState {
43 pub OpenDocuments:Arc<StandardMutex<HashMap<String, DocumentStateDTO>>>,
45}
46
47impl Default for DocumentState {
48 fn default() -> Self {
49 dev_log!("model", "[DocumentState] Initializing default document state...");
50
51 Self { OpenDocuments:Arc::new(StandardMutex::new(HashMap::new())) }
52 }
53}
54
55impl DocumentState {
56 pub fn GetAll(&self) -> HashMap<String, DocumentStateDTO> {
58 self.OpenDocuments.lock().ok().map(|guard| guard.clone()).unwrap_or_default()
59 }
60
61 pub fn Get(&self, uri:&str) -> Option<DocumentStateDTO> {
63 self.OpenDocuments.lock().ok().and_then(|guard| guard.get(uri).cloned())
64 }
65
66 pub fn AddOrUpdate(&self, uri:String, document:DocumentStateDTO) {
68 if let Ok(mut guard) = self.OpenDocuments.lock() {
69 guard.insert(uri, document);
70
71 dev_log!("model", "[DocumentState] Document added/updated");
72 }
73 }
74
75 pub fn Remove(&self, uri:&str) {
77 if let Ok(mut guard) = self.OpenDocuments.lock() {
78 guard.remove(uri);
79
80 dev_log!("model", "[DocumentState] Document removed: {}", uri);
81 }
82 }
83
84 pub fn Clear(&self) {
86 if let Ok(mut guard) = self.OpenDocuments.lock() {
87 guard.clear();
88
89 dev_log!("model", "[DocumentState] All documents cleared");
90 }
91 }
92
93 pub fn Count(&self) -> usize { self.OpenDocuments.lock().ok().map(|guard| guard.len()).unwrap_or(0) }
95
96 pub fn Contains(&self, uri:&str) -> bool {
98 self.OpenDocuments
99 .lock()
100 .ok()
101 .map(|guard| guard.contains_key(uri))
102 .unwrap_or(false)
103 }
104
105 pub fn GetURIs(&self) -> Vec<String> {
107 self.OpenDocuments
108 .lock()
109 .ok()
110 .map(|guard| guard.keys().cloned().collect())
111 .unwrap_or_default()
112 }
113}