Mountain/IPC/WindServiceHandler/
Workspace.rs1#![allow(non_snake_case)]
2
3use std::sync::Arc;
6
7use serde_json::{Value, json};
8
9use crate::{
10 ApplicationState::DTO::WorkspaceFolderStateDTO::WorkspaceFolderStateDTO,
11 RunTime::ApplicationRunTime::ApplicationRunTime,
12};
13
14pub async fn handle_workspaces_get_folders(Runtime:Arc<ApplicationRunTime>) -> Result<Value, String> {
16 let Workspace = &Runtime.Environment.ApplicationState.Workspace;
17 let Folders = Workspace.GetWorkspaceFolders();
18
19 let FolderList:Vec<Value> = Folders
20 .iter()
21 .enumerate()
22 .map(|(Index, Folder)| {
23 json!({
24 "uri": Folder.URI.to_string(),
25 "name": Folder.Name,
26 "index": Index,
27 })
28 })
29 .collect();
30
31 Ok(json!(FolderList))
32}
33
34pub async fn handle_workspaces_add_folder(Runtime:Arc<ApplicationRunTime>, Args:Vec<Value>) -> Result<Value, String> {
36 use url::Url;
37
38 let UriStr = Args
39 .first()
40 .and_then(|V| V.as_str())
41 .ok_or("workspaces:addFolder requires uri as first argument".to_string())?
42 .to_string();
43
44 let Name = Args.get(1).and_then(|V| V.as_str()).unwrap_or("").to_string();
45
46 let Workspace = &Runtime.Environment.ApplicationState.Workspace;
47 let mut Folders = Workspace.GetWorkspaceFolders();
48 let Index = Folders.len();
49 let URI = Url::parse(&UriStr).map_err(|E| format!("workspaces:addFolder invalid URI: {}", E))?;
50 if let Ok(Folder) = WorkspaceFolderStateDTO::New(URI, Name, Index) {
51 Folders.push(Folder);
52 Workspace.SetWorkspaceFolders(Folders);
53 }
54
55 Ok(Value::Null)
56}
57
58pub async fn handle_workspaces_remove_folder(
60 Runtime:Arc<ApplicationRunTime>,
61 Args:Vec<Value>,
62) -> Result<Value, String> {
63 let UriStr = Args
64 .first()
65 .and_then(|V| V.as_str())
66 .ok_or("workspaces:removeFolder requires uri as first argument".to_string())?
67 .to_string();
68
69 let Workspace = &Runtime.Environment.ApplicationState.Workspace;
70 let mut Folders = Workspace.GetWorkspaceFolders();
71 Folders.retain(|F| F.URI.to_string() != UriStr);
72 for (I, F) in Folders.iter_mut().enumerate() {
73 F.Index = I;
74 }
75 Workspace.SetWorkspaceFolders(Folders);
76
77 Ok(Value::Null)
78}
79
80pub async fn handle_workspaces_get_name(Runtime:Arc<ApplicationRunTime>) -> Result<Value, String> {
82 let Name = Runtime
83 .Environment
84 .ApplicationState
85 .Workspace
86 .GetWorkspaceFolders()
87 .into_iter()
88 .next()
89 .map(|F| F.GetDisplayName());
90
91 Ok(Name.map(|N| json!(N)).unwrap_or(Value::Null))
92}