Skip to main content

Mountain/Environment/ConfigurationProvider/
InspectValue.rs

1//! Configuration value introspection.
2
3use CommonLibrary::{
4	Configuration::DTO::{
5		ConfigurationOverridesDTO::ConfigurationOverridesDTO,
6		InspectResultDataDTO::InspectResultDataDTO,
7	},
8	Error::CommonError::CommonError,
9};
10use serde_json::Value;
11use tauri::Manager;
12
13use crate::{Environment::Utility, dev_log};
14
15/// Inspects a configuration key to get its value from all relevant scopes.
16pub(super) async fn inspect_configuration_value(
17	environment:&crate::Environment::MountainEnvironment::MountainEnvironment,
18	key:String,
19	_overrides:ConfigurationOverridesDTO,
20) -> Result<Option<InspectResultDataDTO>, CommonError> {
21	dev_log!("config", "[ConfigurationProvider] Inspecting key: {}", key);
22
23	let user_settings_path = environment
24		.ApplicationHandle
25		.path()
26		.app_config_dir()
27		.map(|p| p.join("settings.json"))
28		.ok();
29
30	let workspace_settings_path = environment
31		.ApplicationState
32		.Workspace
33		.WorkspaceConfigurationPath
34		.lock()
35		.map_err(Utility::MapApplicationStateLockErrorToCommonError)?
36		.clone();
37
38	// Read each configuration layer individually.
39	let default_config = super::Loading::collect_default_configurations(&environment.ApplicationState)?;
40
41	let user_config = super::Loading::read_and_parse_configuration_file(environment, &user_settings_path).await?;
42
43	let workspace_config =
44		super::Loading::read_and_parse_configuration_file(environment, &workspace_settings_path).await?;
45
46	let get_value_from_dot_path =
47		|node:&Value, path:&str| -> Option<Value> { path.split('.').try_fold(node, |n, k| n.get(k)).cloned() };
48
49	let mut result_dto = InspectResultDataDTO::default();
50
51	result_dto.DefaultValue = get_value_from_dot_path(&default_config, &key);
52
53	result_dto.UserValue = get_value_from_dot_path(&user_config, &key);
54
55	result_dto.WorkspaceValue = get_value_from_dot_path(&workspace_config, &key);
56
57	// Determine the final effective value based on the correct cascade order.
58	result_dto.EffectiveValue = result_dto
59		.WorkspaceValue
60		.clone()
61		.or_else(|| result_dto.UserValue.clone())
62		.or_else(|| result_dto.DefaultValue.clone());
63
64	if result_dto.EffectiveValue.is_some() {
65		Ok(Some(result_dto))
66	} else {
67		Ok(None)
68	}
69}