Skip to main content

Mountain/ApplicationState/State/FeatureState/Debug/
DebugState.rs

1//! # DebugState Module (ApplicationState)
2
3//! ## RESPONSIBILITIES
4//! Manages debug provider state including debug configuration providers and
5//! adapter descriptor factories.
6
7//! ## ARCHITECTURAL ROLE
8//! DebugState is part of the **FeatureState** module, storing debug provider
9//! registrations keyed by debug type.
10
11//! ## KEY COMPONENTS
12//! - DebugState: Main struct containing debug provider registrations
13//! - Default: Initialization implementation
14//! - Helper methods: Debug registration management
15
16//! ## ERROR HANDLING
17//! - Thread-safe access via `Arc<tokio::sync::RwLock<...>>`
18//! - Proper lock error handling
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//! - Use Arc for shared ownership across threads
26
27use std::{
28	collections::HashMap,
29	sync::{Arc, Mutex as StandardMutex},
30};
31
32use crate::dev_log;
33
34/// Debug configuration provider registration info
35#[derive(Clone, Debug)]
36pub struct DebugConfigurationProviderRegistration {
37	/// The provider handle
38	pub ProviderHandle:u32,
39	/// The sidecar identifier hosting this provider
40	pub SideCarIdentifier:String,
41}
42
43/// Debug adapter descriptor factory registration info
44#[derive(Clone, Debug)]
45pub struct DebugAdapterDescriptorFactoryRegistration {
46	/// The factory handle
47	pub FactoryHandle:u32,
48	/// The sidecar identifier hosting this factory
49	pub SideCarIdentifier:String,
50}
51
52/// Debug state containing debug provider registrations.
53#[derive(Clone)]
54pub struct DebugState {
55	/// Debug configuration providers organized by debug type.
56	pub DebugConfigurationProviders:Arc<StandardMutex<HashMap<String, DebugConfigurationProviderRegistration>>>,
57	/// Debug adapter descriptor factories organized by debug type.
58	pub DebugAdapterDescriptorFactories:Arc<StandardMutex<HashMap<String, DebugAdapterDescriptorFactoryRegistration>>>,
59}
60
61impl Default for DebugState {
62	fn default() -> Self {
63		dev_log!("exthost", "[DebugState] Initializing default debug state...");
64
65		Self {
66			DebugConfigurationProviders:Arc::new(StandardMutex::new(HashMap::new())),
67			DebugAdapterDescriptorFactories:Arc::new(StandardMutex::new(HashMap::new())),
68		}
69	}
70}
71
72impl DebugState {
73	/// Registers a debug configuration provider.
74	pub fn RegisterDebugConfigurationProvider(
75		&self,
76		debug_type:String,
77		provider_handle:u32,
78		sidecar_identifier:String,
79	) -> Result<(), String> {
80		let mut guard = self
81			.DebugConfigurationProviders
82			.lock()
83			.map_err(|e| format!("Failed to lock debug configuration providers: {}", e))?;
84
85		guard.insert(
86			debug_type,
87			DebugConfigurationProviderRegistration {
88				ProviderHandle:provider_handle,
89				SideCarIdentifier:sidecar_identifier,
90			},
91		);
92
93		Ok(())
94	}
95
96	/// Gets a debug configuration provider registration by debug type.
97	pub fn GetDebugConfigurationProvider(&self, debug_type:&str) -> Option<DebugConfigurationProviderRegistration> {
98		self.DebugConfigurationProviders
99			.lock()
100			.ok()
101			.and_then(|guard| guard.get(debug_type).cloned())
102	}
103
104	/// Registers a debug adapter descriptor factory.
105	pub fn RegisterDebugAdapterDescriptorFactory(
106		&self,
107		debug_type:String,
108		factory_handle:u32,
109		sidecar_identifier:String,
110	) -> Result<(), String> {
111		let mut guard = self
112			.DebugAdapterDescriptorFactories
113			.lock()
114			.map_err(|e| format!("Failed to lock debug adapter descriptor factories: {}", e))?;
115
116		guard.insert(
117			debug_type,
118			DebugAdapterDescriptorFactoryRegistration {
119				FactoryHandle:factory_handle,
120				SideCarIdentifier:sidecar_identifier,
121			},
122		);
123
124		Ok(())
125	}
126
127	/// Gets a debug adapter descriptor factory registration by debug type.
128	pub fn GetDebugAdapterDescriptorFactory(
129		&self,
130		debug_type:&str,
131	) -> Option<DebugAdapterDescriptorFactoryRegistration> {
132		self.DebugAdapterDescriptorFactories
133			.lock()
134			.ok()
135			.and_then(|guard| guard.get(debug_type).cloned())
136	}
137
138	/// Gets all registered debug configuration providers.
139	pub fn GetAllDebugConfigurationProviders(&self) -> HashMap<String, DebugConfigurationProviderRegistration> {
140		self.DebugConfigurationProviders
141			.lock()
142			.ok()
143			.map(|guard| guard.clone())
144			.unwrap_or_default()
145	}
146
147	/// Gets all registered debug adapter descriptor factories.
148	pub fn GetAllDebugAdapterDescriptorFactories(&self) -> HashMap<String, DebugAdapterDescriptorFactoryRegistration> {
149		self.DebugAdapterDescriptorFactories
150			.lock()
151			.ok()
152			.map(|guard| guard.clone())
153			.unwrap_or_default()
154	}
155}