Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/UriComponents/
FromUrl.rs

1//! Build a `UriComponents` from a fully-formed URL string. Handles
2//! `file://` (authority-optional) and any other scheme generically
3//! (`scheme:path` + optional `//authority`). Fragment / query are split
4//! off verbatim so downstream `URI.revive()` reconstructs the same URL.
5//! Strings that don't parse as URLs fall back to `{ scheme:"file",
6//! path:<input> }` - a defensive shape the workbench tolerates for
7//! unknown-location placeholders.
8
9use serde_json::{Value, json};
10
11use crate::IPC::UriComponents::StampMidUri;
12
13pub fn Fn(Url:&str) -> Value {
14	if let Some(Rest) = Url.strip_prefix("file://") {
15		let (Authority, Path) = match Rest.find('/') {
16			Some(0) => ("", Rest),
17
18			Some(Index) => (&Rest[..Index], &Rest[Index..]),
19
20			None => ("", ""),
21		};
22
23		return StampMidUri::Fn(json!({
24			"scheme": "file",
25			"authority": Authority,
26			"path": Path,
27			"query": "",
28			"fragment": "",
29		}));
30	}
31
32	if let Some((Scheme, PathPart)) = Url.split_once(':') {
33		let Trimmed = PathPart.trim_start_matches("//");
34
35		let (Authority, Path) = match Trimmed.find('/') {
36			Some(0) => ("", Trimmed),
37
38			Some(Index) => (&Trimmed[..Index], &Trimmed[Index..]),
39
40			None => ("", Trimmed),
41		};
42
43		return StampMidUri::Fn(json!({
44			"scheme": Scheme,
45			"authority": Authority,
46			"path": Path,
47			"query": "",
48			"fragment": "",
49		}));
50	}
51
52	StampMidUri::Fn(json!({
53		"scheme": "file",
54		"authority": "",
55		"path": Url,
56		"query": "",
57		"fragment": "",
58	}))
59}