Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
Configuration.rs

1use 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				let Key = str_obj_or_pos(&Parameters, "key", 0).to_string();
86				let result = provider.InspectConfigurationValue(Key, Default::default()).await;
87				result
88					.map(|Inspection| serde_json::to_value(Inspection).unwrap_or(Value::Null))
89					.map_err(|e| e.to_string())
90			})
91		},
92
93		"config.update" => {
94			crate::effect!(run_time, {
95				let (Key, Value_, Target) = if let Some(Object) = Parameters.as_object() {
96					let K = Object.get("key").and_then(Value::as_str).unwrap_or("").to_string();
97					let V = Object.get("value").cloned().unwrap_or_default();
98					let T = match Object.get("target").and_then(Value::as_u64) {
99						Some(0) => ConfigurationTarget::User,
100						Some(1) => ConfigurationTarget::Workspace,
101						_ => ConfigurationTarget::User,
102					};
103					(K, V, T)
104				} else {
105					let K = string_at(&Parameters, 0);
106					let V = val_at(&Parameters, 1);
107					let T = match u64_at(&Parameters, 2) {
108						0 => ConfigurationTarget::User,
109						1 => ConfigurationTarget::Workspace,
110						_ => ConfigurationTarget::User,
111					};
112					(K, V, T)
113				};
114				UpdateConfigurationValueAndNotify(run_time, Key, Value_, Target, "config.update").await
115			})
116		},
117
118		"Configuration.Inspect" => {
119			crate::effect!(run_time, {
120				let provider:Arc<dyn ConfigurationInspector> = run_time.Environment.Require();
121				let section = string_at(&Parameters, 0);
122				let result = provider.InspectConfigurationValue(section, Default::default()).await;
123				result
124					.map(|Inspection| serde_json::to_value(Inspection).unwrap_or(Value::Null))
125					.map_err(|e| e.to_string())
126			})
127		},
128
129		"Configuration.Update" => {
130			crate::effect!(run_time, {
131				let key = string_at(&Parameters, 0);
132				let value = val_at(&Parameters, 1);
133				let target = match u64_at(&Parameters, 2) {
134					0 => ConfigurationTarget::User,
135					1 => ConfigurationTarget::Workspace,
136					_ => ConfigurationTarget::User,
137				};
138				UpdateConfigurationValueAndNotify(run_time, key, value, target, "Configuration.Update").await
139			})
140		},
141
142		_ => None,
143	}
144}