Common/Configuration/
GetConfiguration.rs

1// File: Common/Source/Configuration/GetConfiguration.rs
2// Role: Defines the `GetConfiguration` ActionEffect.
3// Responsibilities:
4//   - Provide a declarative effect for retrieving a merged configuration value
5//     or section.
6//   - This effect abstracts the "what" (get a configuration) from the "how"
7//     (the ConfigurationProvider implementation).
8
9use std::sync::Arc;
10
11use serde_json::Value;
12
13use super::{ConfigurationProvider::ConfigurationProvider, DTO::ConfigurationOverridesDTO::ConfigurationOverridesDTO};
14use crate::{Effect::ActionEffect::ActionEffect, Error::CommonError::CommonError};
15
16/// Creates an effect that, when executed, will retrieve the final, merged
17/// configuration value for a given section, applying any specified overrides.
18///
19/// It uses the `ConfigurationProvider` capability from the environment to
20/// perform the operation.
21///
22/// # Parameters
23///
24/// * `Section`: An optional, dot-separated key to a specific configuration
25///   section. If `None`, the entire configuration object is returned.
26/// * `OverridesValue`: A `serde_json::Value` representing the
27///   `ConfigurationOverridesDTO`, which can specify a resource or language
28///   scope.
29///
30/// # Returns
31///
32/// An `ActionEffect` that resolves with a `serde_json::Value` containing the
33/// requested configuration.
34pub fn GetConfiguration(
35	Section:Option<String>,
36
37	OverridesValue:Value,
38) -> ActionEffect<Arc<dyn ConfigurationProvider>, CommonError, Value> {
39	ActionEffect::New(Arc::new(move |Provider:Arc<dyn ConfigurationProvider>| {
40		let SectionClone = Section.clone();
41
42		let OverridesValueClone = OverridesValue.clone();
43
44		Box::pin(async move {
45			let OverridesParsed:ConfigurationOverridesDTO =
46				serde_json::from_value(OverridesValueClone).map_err(|Error| {
47					CommonError::InvalidArgument {
48						ArgumentName:"OverridesValue".to_string(),
49
50						Reason:format!("Failed to parse ConfigurationOverridesDTO: {}", Error),
51					}
52				})?;
53
54			Provider.GetConfigurationValue(SectionClone, OverridesParsed).await
55		})
56	}))
57}