Skip to main content

Mountain/IPC/WindServiceHandlers/Storage/
StorageGet.rs

1//! Read a single value from persistent storage by key. The
2//! `false` first arg to `GetStorageValue` selects the
3//! workspace-scoped store; `true` would target global storage.
4
5use std::sync::Arc;
6
7use CommonLibrary::{Environment::Requires::Requires, Storage::StorageProvider::StorageProvider};
8use serde_json::Value;
9
10use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
11
12pub async fn Fn(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
13	let key = Arguments
14		.first()
15		.ok_or("Missing storage key".to_string())?
16		.as_str()
17		.ok_or("Storage key must be a string".to_string())?;
18
19	let provider:Arc<dyn StorageProvider> = RunTime.Environment.Require();
20
21	let value = provider
22		.GetStorageValue(false, key)
23		.await
24		.map_err(|Error| format!("Failed to get storage item: {}", Error))?;
25
26	dev_log!("storage", "get: {}", key);
27
28	// Return JSON null for missing keys. VS Code's storage clients use
29	// `value ?? defaultValue` which treats null and undefined identically,
30	// so null is the correct sentinel for "key not set".
31	Ok(value.unwrap_or(Value::Null))
32}