Skip to main content

Mountain/IPC/UriComponents/
FromUrl.rs

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