Mountain/Track/Effect/CreateEffectForRequest/
Configuration.rs1use std::sync::Arc;
2
3use CommonLibrary::{
4 Configuration::{
5 ConfigurationInspector::ConfigurationInspector,
6 ConfigurationProvider::ConfigurationProvider,
7 DTO::ConfigurationTarget::ConfigurationTarget,
8 },
9 Environment::Requires::Requires,
10 IPC::IPCProvider::IPCProvider as IPCProviderTrait,
11};
12use serde_json::{Value, json};
13use tauri::Runtime;
14
15use crate::{
16 RunTime::ApplicationRunTime::ApplicationRunTime,
17 Track::Effect::{
18 CreateEffectForRequest::Utilities::Params::{str_obj_or_pos, string_at, u64_at, val_at},
19 MappedEffectType::MappedEffect,
20 },
21 dev_log,
22};
23
24async fn UpdateConfigurationValueAndNotify(
25 run_time:Arc<ApplicationRunTime>,
26
27 key:String,
28
29 value:Value,
30
31 target:ConfigurationTarget,
32
33 log_prefix:&str,
34) -> Result<Value, String> {
35 use tauri::Emitter;
36
37 let provider:Arc<dyn ConfigurationProvider> = run_time.Environment.Require();
38
39 let KeyForEvents = key.clone();
40
41 let result = provider
42 .UpdateConfigurationValue(key, value, target, Default::default(), None)
43 .await;
44
45 if result.is_ok() {
46 let Payload = json!({
47 "keys": [KeyForEvents.clone()],
48 "affected": [KeyForEvents.clone()],
49 });
50
51 let AppHandle = run_time.Environment.ApplicationHandle.clone();
52
53 if let Err(Error) = AppHandle.emit("sky://configuration/changed", Payload.clone()) {
54 dev_log!(
55 "config",
56 "warn: [{}] sky://configuration/changed emit failed: {}",
57 log_prefix,
58 Error
59 );
60 }
61
62 let IPCProvider:Arc<dyn IPCProviderTrait> = run_time.Environment.Require();
63
64 if let Err(Error) = IPCProvider
65 .SendNotificationToSideCar("cocoon-main".to_string(), "configuration.change".to_string(), Payload)
66 .await
67 {
68 dev_log!(
69 "config",
70 "warn: [{}] Cocoon configuration.change notification failed: {}",
71 log_prefix,
72 Error
73 );
74 }
75 }
76
77 result.map(|_| json!(null)).map_err(|e| e.to_string())
78}
79
80pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
81 match MethodName {
82 "config.get" => {
83 crate::effect!(run_time, {
84 let provider:Arc<dyn ConfigurationInspector> = run_time.Environment.Require();
85
86 let Key = str_obj_or_pos(&Parameters, "key", 0).to_string();
87
88 let result = provider.InspectConfigurationValue(Key, Default::default()).await;
89
90 result
91 .map(|Inspection| serde_json::to_value(Inspection).unwrap_or(Value::Null))
92 .map_err(|e| e.to_string())
93 })
94 },
95
96 "config.update" => {
97 crate::effect!(run_time, {
98 let (Key, Value_, Target) = if let Some(Object) = Parameters.as_object() {
99 let K = Object.get("key").and_then(Value::as_str).unwrap_or("").to_string();
100
101 let V = Object.get("value").cloned().unwrap_or_default();
102
103 let T = match Object.get("target").and_then(Value::as_u64) {
104 Some(0) => ConfigurationTarget::User,
105 Some(1) => ConfigurationTarget::Workspace,
106 _ => ConfigurationTarget::User,
107 };
108
109 (K, V, T)
110 } else {
111 let K = string_at(&Parameters, 0);
112
113 let V = val_at(&Parameters, 1);
114
115 let T = match u64_at(&Parameters, 2) {
116 0 => ConfigurationTarget::User,
117 1 => ConfigurationTarget::Workspace,
118 _ => ConfigurationTarget::User,
119 };
120
121 (K, V, T)
122 };
123
124 UpdateConfigurationValueAndNotify(run_time, Key, Value_, Target, "config.update").await
125 })
126 },
127
128 "Configuration.Inspect" => {
129 crate::effect!(run_time, {
130 let provider:Arc<dyn ConfigurationInspector> = run_time.Environment.Require();
131
132 let section = string_at(&Parameters, 0);
133
134 let result = provider.InspectConfigurationValue(section, Default::default()).await;
135
136 result
137 .map(|Inspection| serde_json::to_value(Inspection).unwrap_or(Value::Null))
138 .map_err(|e| e.to_string())
139 })
140 },
141
142 "Configuration.Update" => {
143 crate::effect!(run_time, {
144 let key = string_at(&Parameters, 0);
145
146 let value = val_at(&Parameters, 1);
147
148 let target = match u64_at(&Parameters, 2) {
149 0 => ConfigurationTarget::User,
150 1 => ConfigurationTarget::Workspace,
151 _ => ConfigurationTarget::User,
152 };
153
154 UpdateConfigurationValueAndNotify(run_time, key, value, target, "Configuration.Update").await
155 })
156 },
157
158 _ => None,
159 }
160}