Skip to main content

Mountain/Environment/ConfigurationProvider/
GetValue.rs

1//! Configuration value retrieval.
2//!
3//! Implements `GetConfigurationValue` for `MountainEnvironment`. Reads
4//! from the pre-merged `ApplicationState::Configuration::GlobalConfiguration`
5//! cache - no disk I/O on the hot path.
6//!
7//! If `section` is `None`, the entire merged object is returned. If it
8//! is `Some("a.b.c")`, the key is split on `.` and the function walks
9//! the nested JSON tree one segment at a time, returning `Value::Null`
10//! (not an error) for any missing intermediate or leaf node. This
11//! matches VS Code's behaviour where `getConfiguration('a.b').get('c')`
12//! returns `undefined` rather than throwing.
13
14use CommonLibrary::{
15	Configuration::DTO::ConfigurationOverridesDTO::ConfigurationOverridesDTO,
16	Error::CommonError::CommonError,
17};
18use serde_json::Value;
19
20use crate::dev_log;
21
22/// Retrieves a configuration value from the cached, merged configuration.
23pub(super) async fn get_configuration_value(
24	environment:&crate::Environment::MountainEnvironment::MountainEnvironment,
25
26	section:Option<String>,
27
28	_overrides:ConfigurationOverridesDTO,
29) -> Result<Value, CommonError> {
30	dev_log!(
31		"config",
32		"[ConfigurationProvider] Getting configuration for section: {:?}",
33		section
34	);
35
36	let configuration_guard = environment
37		.ApplicationState
38		.Configuration
39		.GlobalConfiguration
40		.lock()
41		.map_err(|e| CommonError::StateLockPoisoned { Context:format!("Failed to lock configuration: {}", e) })?;
42
43	let configuration_value = match section.as_deref() {
44		None => (*configuration_guard).clone(),
45
46		Some(section_path) => {
47			// Navigate through the configuration using dot notation
48			let mut current = &*configuration_guard;
49
50			for key in section_path.split('.') {
51				current = match current.get(key) {
52					Some(value) => value,
53
54					None => {
55						dev_log!(
56							"config",
57							"warn: [ConfigurationProvider] Configuration section '{}' not found in path: {:?}",
58							key,
59							section_path
60						);
61
62						return Ok(Value::Null);
63					},
64				};
65			}
66
67			current.clone()
68		},
69	};
70
71	// Validate that the configuration value exists
72	if configuration_value.is_null() {
73		dev_log!(
74			"config",
75			"warn: [ConfigurationProvider] Configuration section not found: {:?}",
76			section
77		);
78	}
79
80	Ok(configuration_value)
81}