Skip to main content

Mountain/Track/Effect/CreateEffectForRequest/
Debug.rs

1#![allow(non_snake_case, unused_variables, dead_code, unused_imports)]
2
3use std::{future::Future, pin::Pin, sync::Arc};
4
5use CommonLibrary::{Debug::DebugService::DebugService, Environment::Requires::Requires};
6use serde_json::{Value, json};
7use tauri::Runtime;
8use url::Url;
9
10use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, Track::Effect::MappedEffectType::MappedEffect};
11
12pub fn CreateEffect<R:Runtime>(MethodName:&str, Parameters:Value) -> Option<Result<MappedEffect, String>> {
13	match MethodName {
14		"Debug.Start" => {
15			let effect =
16				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
17					Box::pin(async move {
18						let provider:Arc<dyn DebugService> = run_time.Environment.Require();
19						let folder_uri_str = Parameters.get(0).and_then(Value::as_str).unwrap_or("");
20						let folder_uri = if folder_uri_str.is_empty() { None } else { Url::parse(folder_uri_str).ok() };
21						let configuration = Parameters.get(1).cloned().unwrap_or_else(|| json!({ "type": "node" }));
22						provider
23							.StartDebugging(folder_uri, configuration)
24							.await
25							.map(|session_id| json!(session_id))
26							.map_err(|e| e.to_string())
27					})
28				};
29			Some(Ok(Box::new(effect)))
30		},
31
32		"Debug.RegisterConfigurationProvider" => {
33			let effect =
34				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
35					Box::pin(async move {
36						let provider:Arc<dyn DebugService> = run_time.Environment.Require();
37						let debug_type = Parameters.get(0).and_then(Value::as_str).unwrap_or("node").to_string();
38						let provider_handle = Parameters.get(1).and_then(Value::as_i64).map(|n| n as u32).unwrap_or(1);
39						let sidecar_id = Parameters.get(2).and_then(Value::as_str).unwrap_or("cocoon-main").to_string();
40						provider
41							.RegisterDebugConfigurationProvider(debug_type, provider_handle, sidecar_id)
42							.await
43							.map(|_| json!(null))
44							.map_err(|e| e.to_string())
45					})
46				};
47			Some(Ok(Box::new(effect)))
48		},
49
50		"Debug.Stop" => {
51			let effect =
52				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
53					Box::pin(async move {
54						let provider:Arc<dyn DebugService> = run_time.Environment.Require();
55						let SessionId = Parameters.get(0).and_then(Value::as_str).unwrap_or("").to_string();
56						provider
57							.StopDebugging(SessionId)
58							.await
59							.map(|_| json!(null))
60							.map_err(|e| e.to_string())
61					})
62				};
63			Some(Ok(Box::new(effect)))
64		},
65
66		_ => None,
67	}
68}