Mountain/IPC/WindServiceHandler/
Label.rs1#![allow(non_snake_case)]
2
3use std::sync::Arc;
6
7use serde_json::Value;
8
9use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
10
11pub async fn handle_label_get_uri(Runtime:Arc<ApplicationRunTime>, Args:Vec<Value>) -> Result<Value, String> {
16 let Uri = Args
17 .first()
18 .and_then(|V| V.as_str())
19 .ok_or("label:getUri requires uri".to_string())?
20 .to_owned();
21
22 let Relative = Args.get(1).and_then(|V| V.as_bool()).unwrap_or(false);
23
24 if !Relative {
25 let Label = if Uri.starts_with("file://") {
26 Uri.trim_start_matches("file://").to_owned()
27 } else {
28 Uri.clone()
29 };
30 return Ok(Value::String(Label));
31 }
32
33 let WorkspaceRoot = Runtime
34 .Environment
35 .ApplicationState
36 .Workspace
37 .GetWorkspaceFolders()
38 .into_iter()
39 .next()
40 .map(|F| F.URI.to_string())
41 .unwrap_or_default();
42
43 let RawPath = if Uri.starts_with("file://") {
44 Uri.trim_start_matches("file://").to_owned()
45 } else {
46 Uri.clone()
47 };
48
49 let RootPath = if WorkspaceRoot.starts_with("file://") {
50 WorkspaceRoot.trim_start_matches("file://").to_owned()
51 } else {
52 WorkspaceRoot
53 };
54
55 let Label = if !RootPath.is_empty() && RawPath.starts_with(&RootPath) {
56 RawPath[RootPath.len()..].trim_start_matches('/').to_owned()
57 } else {
58 RawPath
59 };
60
61 Ok(Value::String(Label))
62}
63
64pub async fn handle_label_get_workspace(Runtime:Arc<ApplicationRunTime>) -> Result<Value, String> {
66 let Label = Runtime
67 .Environment
68 .ApplicationState
69 .Workspace
70 .GetWorkspaceFolders()
71 .into_iter()
72 .next()
73 .map(|F| {
74 if !F.Name.is_empty() {
75 F.Name
76 } else {
77 F.URI
78 .path_segments()
79 .and_then(|mut S| S.next_back())
80 .map(|S| S.to_owned())
81 .unwrap_or_else(|| F.URI.to_string())
82 }
83 })
84 .unwrap_or_default();
85
86 Ok(Value::String(Label))
87}
88
89pub async fn handle_label_get_base(Args:Vec<Value>) -> Result<Value, String> {
91 let Uri = Args
92 .first()
93 .and_then(|V| V.as_str())
94 .ok_or("label:getBase requires uri".to_string())?;
95
96 let Base = Uri.split('/').next_back().unwrap_or(Uri);
97 Ok(Value::String(Base.to_owned()))
98}