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
30			Some(Ok(Box::new(effect)))
31		},
32
33		"Debug.RegisterConfigurationProvider" => {
34			let effect =
35				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
36					Box::pin(async move {
37						let provider:Arc<dyn DebugService> = run_time.Environment.Require();
38						let debug_type = Parameters.get(0).and_then(Value::as_str).unwrap_or("node").to_string();
39						let provider_handle = Parameters.get(1).and_then(Value::as_i64).map(|n| n as u32).unwrap_or(1);
40						let sidecar_id = Parameters.get(2).and_then(Value::as_str).unwrap_or("cocoon-main").to_string();
41						provider
42							.RegisterDebugConfigurationProvider(debug_type, provider_handle, sidecar_id)
43							.await
44							.map(|_| json!(null))
45							.map_err(|e| e.to_string())
46					})
47				};
48
49			Some(Ok(Box::new(effect)))
50		},
51
52		"Debug.Stop" => {
53			let effect =
54				move |run_time:Arc<ApplicationRunTime>| -> Pin<Box<dyn Future<Output = Result<Value, String>> + Send>> {
55					Box::pin(async move {
56						let provider:Arc<dyn DebugService> = run_time.Environment.Require();
57						let SessionId = Parameters.get(0).and_then(Value::as_str).unwrap_or("").to_string();
58						provider
59							.StopDebugging(SessionId)
60							.await
61							.map(|_| json!(null))
62							.map_err(|e| e.to_string())
63					})
64				};
65
66			Some(Ok(Box::new(effect)))
67		},
68
69		_ => None,
70	}
71}