Skip to main content

Mountain/IPC/UriComponents/
FromUrl.rs

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