Mountain/IPC/WindServiceHandlers/Navigation/
LabelGetURI.rs1
2use std::sync::Arc;
10
11use serde_json::Value;
12
13use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
14
15pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
16 let Uri = Arguments
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 = Arguments.get(1).and_then(|V| V.as_bool()).unwrap_or(false);
23
24 if !Relative {
25 let Label = if let Some(stripped) = Uri.strip_prefix("file://") {
26 stripped.to_owned()
27 } else {
28 Uri.clone()
29 };
30
31 return Ok(Value::String(Label));
32 }
33
34 let WorkspaceRoot = RunTime
35 .Environment
36 .ApplicationState
37 .Workspace
38 .GetWorkspaceFolders()
39 .into_iter()
40 .next()
41 .map(|F| F.URI.to_string())
42 .unwrap_or_default();
43
44 let RawPath = if let Some(stripped) = Uri.strip_prefix("file://") {
45 stripped.to_owned()
46 } else {
47 Uri.clone()
48 };
49
50 let RootPath = if let Some(stripped) = WorkspaceRoot.strip_prefix("file://") {
51 stripped.to_owned()
52 } else {
53 WorkspaceRoot
54 };
55
56 let Label = if !RootPath.is_empty() && RawPath.starts_with(&RootPath) {
57 RawPath[RootPath.len()..].trim_start_matches('/').to_owned()
58 } else {
59 RawPath
60 };
61
62 Ok(Value::String(Label))
63}