Common/Configuration/UpdateConfiguration.rs
1// File: Common/Source/Configuration/UpdateConfiguration.rs
2// Role: Defines the `UpdateConfiguration` ActionEffect.
3// Responsibilities:
4// - Provide a declarative effect for updating a configuration value in a
5// specific target scope.
6// - This effect abstracts the "what" (update a configuration) from the "how"
7// (the ConfigurationProvider implementation).
8
9//! # UpdateConfiguration Effect
10//!
11//! Defines the `ActionEffect` for updating a configuration value in a specific
12//! target scope.
13
14use std::sync::Arc;
15
16use serde_json::Value;
17
18use super::{
19 ConfigurationProvider::ConfigurationProvider,
20 DTO::{ConfigurationOverridesDTO::ConfigurationOverridesDTO, ConfigurationTarget::ConfigurationTarget},
21};
22use crate::{Effect::ActionEffect::ActionEffect, Error::CommonError::CommonError};
23
24/// Creates an effect that, when executed, will update a configuration value at
25/// a specific key and target scope (e.g., User or WorkSpace settings).
26///
27/// It uses the `ConfigurationProvider` capability from the environment to
28/// perform the operation, which will typically involve modifying a
29/// `settings.json` file on disk.
30///
31/// # Parameters
32///
33/// * `Key`: The dot-separated configuration key to update.
34/// * `ValueToSet`: The new `serde_json::Value` to set for the key.
35/// * `TargetAsU32`: The integer representation of the `ConfigurationTarget`
36/// enum, used for cross-language serialization.
37/// * `OverridesValue`: A DTO specifying scope overrides (e.g., for a specific
38/// language).
39/// * `ScopeToLanguage`: An optional flag related to language-specific settings.
40///
41/// # Returns
42///
43/// An `ActionEffect` that resolves to `()` on success.
44pub fn UpdateConfiguration(
45 Key:String,
46
47 ValueToSet:Value,
48
49 TargetAsU32:u32,
50
51 OverridesValue:Value,
52
53 ScopeToLanguage:Option<bool>,
54) -> ActionEffect<Arc<dyn ConfigurationProvider>, CommonError, ()> {
55 ActionEffect::New(Arc::new(move |Provider:Arc<dyn ConfigurationProvider>| {
56 let KeyClone = Key.clone();
57
58 let ValueToSetClone = ValueToSet.clone();
59
60 let OverridesValueClone = OverridesValue.clone();
61
62 let ScopeToLanguageClone = ScopeToLanguage;
63
64 Box::pin(async move {
65 // Deserialize the integer target into the enum.
66 let TargetParsed:ConfigurationTarget =
67 serde_json::from_value(Value::from(TargetAsU32)).map_err(|Error| {
68 CommonError::InvalidArgument {
69 ArgumentName:"Target".to_string(),
70
71 Reason:format!("Failed to parse ConfigurationTarget from u32 {}: {}", TargetAsU32, Error),
72 }
73 })?;
74
75 // Deserialize the overrides DTO.
76 let OverridesParsed:ConfigurationOverridesDTO =
77 serde_json::from_value(OverridesValueClone).map_err(|Error| {
78 CommonError::InvalidArgument {
79 ArgumentName:"OverridesValue".to_string(),
80
81 Reason:format!("Failed to parse ConfigurationOverridesDTO: {}", Error),
82 }
83 })?;
84
85 Provider
86 .UpdateConfigurationValue(
87 KeyClone,
88 ValueToSetClone,
89 TargetParsed,
90 OverridesParsed,
91 ScopeToLanguageClone,
92 )
93 .await
94 })
95 }))
96}