Skip to main content

Mountain/IPC/WindServiceHandlers/Navigation/
LabelGetURI.rs

1#![allow(non_snake_case)]
2
3//! Resolve a human-readable display label for a URI. Two modes:
4//!
5//! - `Relative=false`: strip the `file://` scheme and return the raw absolute
6//!   path.
7//! - `Relative=true`: same, then trim the workspace-folder prefix so the user
8//!   sees `Source/main.rs` instead of `/Volumes/.../Mountain/Source/main.rs`.
9
10use std::sync::Arc;
11
12use serde_json::Value;
13
14use crate::RunTime::ApplicationRunTime::ApplicationRunTime;
15
16pub async fn LabelGetURI(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
17	let Uri = Arguments
18		.first()
19		.and_then(|V| V.as_str())
20		.ok_or("label:getUri requires uri".to_string())?
21		.to_owned();
22
23	let Relative = Arguments.get(1).and_then(|V| V.as_bool()).unwrap_or(false);
24
25	if !Relative {
26		let Label = if let Some(stripped) = Uri.strip_prefix("file://") {
27			stripped.to_owned()
28		} else {
29			Uri.clone()
30		};
31
32		return Ok(Value::String(Label));
33	}
34
35	let WorkspaceRoot = RunTime
36		.Environment
37		.ApplicationState
38		.Workspace
39		.GetWorkspaceFolders()
40		.into_iter()
41		.next()
42		.map(|F| F.URI.to_string())
43		.unwrap_or_default();
44
45	let RawPath = if let Some(stripped) = Uri.strip_prefix("file://") {
46		stripped.to_owned()
47	} else {
48		Uri.clone()
49	};
50
51	let RootPath = if let Some(stripped) = WorkspaceRoot.strip_prefix("file://") {
52		stripped.to_owned()
53	} else {
54		WorkspaceRoot
55	};
56
57	let Label = if !RootPath.is_empty() && RawPath.starts_with(&RootPath) {
58		RawPath[RootPath.len()..].trim_start_matches('/').to_owned()
59	} else {
60		RawPath
61	};
62
63	Ok(Value::String(Label))
64}