Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
Configuration.rs

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