Skip to main content

Mountain/Environment/Utility/
GlobPattern.rs

1#![allow(unused_variables, dead_code, unused_imports)]
2
3//! Glob pattern extraction helpers for `WorkspaceProvider::FindFiles` calls.
4//!
5//! VS Code passes glob patterns in several shapes depending on the caller:
6//! - Bare string: `"**/*.rs"`
7//! - `RelativePattern` object: `{ base, pattern }` or `{ baseUri, pattern }`
8//! - Legacy serialised form: `{ value: "**/*.rs" }`
9//!
10//! These helpers normalise all shapes to a `String` and extract the optional
11//! `base` directory for bounded walks.
12
13use serde_json::Value;
14
15/// Extract a glob string from any shape the caller can supply:
16/// - Bare string → returned as-is.
17/// - Object with `pattern` field (VS Code `RelativePattern`).
18/// - Object with `value` field (legacy serialised form).
19/// - Object with `Pattern` field (PascalCase variant).
20pub(crate) fn ExtractGlobPattern(Pattern:&Value) -> Option<String> {
21	if let Some(S) = Pattern.as_str() {
22		return Some(S.to_string());
23	}
24
25	if let Some(Obj) = Pattern.as_object() {
26		if let Some(P) = Obj.get("pattern").and_then(Value::as_str) {
27			return Some(P.to_string());
28		}
29
30		if let Some(P) = Obj.get("value").and_then(Value::as_str) {
31			return Some(P.to_string());
32		}
33
34		if let Some(P) = Obj.get("Pattern").and_then(Value::as_str) {
35			return Some(P.to_string());
36		}
37	}
38
39	None
40}
41
42/// Extract a `base` directory from a `RelativePattern`-shaped value.
43/// VS Code's `RelativePattern` carries `{ base, pattern }` or
44/// `{ baseUri, pattern }`. When present, the file walk is restricted to
45/// `base`. Returns `None` for plain glob strings.
46pub(crate) fn ExtractRelativeBase(Pattern:&Value) -> Option<String> {
47	let Obj = Pattern.as_object()?;
48
49	if let Some(B) = Obj.get("base").and_then(Value::as_str) {
50		return Some(B.to_string());
51	}
52
53	if let Some(B) = Obj.get("baseUri") {
54		if let Some(S) = B.as_str() {
55			if let Some(Stripped) = S.strip_prefix("file://") {
56				return Some(Stripped.to_string());
57			}
58
59			return Some(S.to_string());
60		}
61
62		if let Some(P) = B.as_object().and_then(|O| O.get("path")).and_then(Value::as_str) {
63			return Some(P.to_string());
64		}
65
66		if let Some(P) = B.as_object().and_then(|O| O.get("fsPath")).and_then(Value::as_str) {
67			return Some(P.to_string());
68		}
69	}
70
71	None
72}