Skip to main content

Mountain/IPC/WindServiceHandler/
Workspace.rs

1#![allow(non_snake_case)]
2
3//! Workspace domain handlers for Wind IPC.
4
5use std::sync::Arc;
6
7use serde_json::{Value, json};
8
9use crate::{
10	ApplicationState::DTO::WorkspaceFolderStateDTO::WorkspaceFolderStateDTO,
11	RunTime::ApplicationRunTime::ApplicationRunTime,
12};
13
14/// Return the current workspace folders.
15pub 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
34/// Add a workspace folder.
35pub 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
58/// Remove a workspace folder by URI.
59pub 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
80/// Return the workspace name (basename of root folder, or None if untitled).
81pub 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}