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