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			Some(Index) => (&Rest[..Index], &Rest[Index..]),
20			None => ("", ""),
21		};
22		return StampMidUri::Fn(json!({
23			"scheme": "file",
24			"authority": Authority,
25			"path": Path,
26			"query": "",
27			"fragment": "",
28		}));
29	}
30	if let Some((Scheme, PathPart)) = Url.split_once(':') {
31		let Trimmed = PathPart.trim_start_matches("//");
32		let (Authority, Path) = match Trimmed.find('/') {
33			Some(0) => ("", Trimmed),
34			Some(Index) => (&Trimmed[..Index], &Trimmed[Index..]),
35			None => ("", Trimmed),
36		};
37		return StampMidUri::Fn(json!({
38			"scheme": Scheme,
39			"authority": Authority,
40			"path": Path,
41			"query": "",
42			"fragment": "",
43		}));
44	}
45	StampMidUri::Fn(json!({
46		"scheme": "file",
47		"authority": "",
48		"path": Url,
49		"query": "",
50		"fragment": "",
51	}))
52}