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 dev_log!("model", "[DocumentState] Document added/updated");
71 }
72 }
73
74 pub fn Remove(&self, uri:&str) {
76 if let Ok(mut guard) = self.OpenDocuments.lock() {
77 guard.remove(uri);
78 dev_log!("model", "[DocumentState] Document removed: {}", uri);
79 }
80 }
81
82 pub fn Clear(&self) {
84 if let Ok(mut guard) = self.OpenDocuments.lock() {
85 guard.clear();
86 dev_log!("model", "[DocumentState] All documents cleared");
87 }
88 }
89
90 pub fn Count(&self) -> usize { self.OpenDocuments.lock().ok().map(|guard| guard.len()).unwrap_or(0) }
92
93 pub fn Contains(&self, uri:&str) -> bool {
95 self.OpenDocuments
96 .lock()
97 .ok()
98 .map(|guard| guard.contains_key(uri))
99 .unwrap_or(false)
100 }
101
102 pub fn GetURIs(&self) -> Vec<String> {
104 self.OpenDocuments
105 .lock()
106 .ok()
107 .map(|guard| guard.keys().cloned().collect())
108 .unwrap_or_default()
109 }
110}