Skip to main content

DevelopmentNodeEnvironment_MicrosoftVSCodeDependency_22NodeVersion_Bundle_Clean_Debug_ElectronProfile_EsbuildCompiler_Mountain/IPC/WindServiceHandlers/Utilities/
JsonValueHelpers.rs

1//! Serde-Value helpers shared across Wind handlers. `v_str` (`Fn`) extracts a
2//! string from either a raw JSON string or a VS Code `UriComponents` object
3//! (`external` / `path` field). The `arg_*` family extracts typed scalars
4//! from `&[Value]` (Wind handler argument lists) at a given position index.
5//! Any new cross-cutting coercer for both shapes belongs here.
6
7use serde_json::Value;
8
9pub fn Fn(Value:&Value) -> Option<String> {
10	if let Some(s) = Value.as_str() {
11		return Some(s.to_string());
12	}
13
14	if let Some(Object) = Value.as_object() {
15		if let Some(s) = Object.get("external").and_then(|V| V.as_str()) {
16			return Some(s.to_string());
17		}
18
19		if let Some(s) = Object.get("path").and_then(|V| V.as_str()) {
20			return Some(s.to_string());
21		}
22	}
23
24	None
25}
26
27pub fn arg_str(args:&[Value], n:usize) -> &str { args.get(n).and_then(Value::as_str).unwrap_or("") }
28
29pub fn arg_string(args:&[Value], n:usize) -> String { arg_str(args, n).to_string() }
30
31pub fn arg_string_or(args:&[Value], n:usize, default:&str) -> String {
32	args.get(n).and_then(Value::as_str).unwrap_or(default).to_string()
33}
34
35pub fn arg_val(args:&[Value], n:usize) -> Value { args.get(n).cloned().unwrap_or(Value::Null) }
36
37pub fn arg_u64(args:&[Value], n:usize) -> u64 { args.get(n).and_then(Value::as_u64).unwrap_or(0) }
38
39pub fn arg_u64_or(args:&[Value], n:usize, default:u64) -> u64 { args.get(n).and_then(Value::as_u64).unwrap_or(default) }
40
41pub fn arg_i64(args:&[Value], n:usize) -> i64 { args.get(n).and_then(Value::as_i64).unwrap_or(0) }
42
43pub fn arg_f64(args:&[Value], n:usize) -> f64 { args.get(n).and_then(Value::as_f64).unwrap_or(0.0) }
44
45pub fn arg_bool(args:&[Value], n:usize) -> bool { args.get(n).and_then(Value::as_bool).unwrap_or(false) }
46
47pub fn arg_bool_true(args:&[Value], n:usize) -> bool { args.get(n).and_then(Value::as_bool).unwrap_or(true) }
48
49pub fn req_str<'a>(args:&'a [Value], n:usize, msg:&str) -> Result<&'a str, String> {
50	args.get(n).and_then(Value::as_str).ok_or_else(|| msg.to_string())
51}
52
53pub fn req_string(args:&[Value], n:usize, msg:&str) -> Result<String, String> {
54	req_str(args, n, msg).map(str::to_string)
55}