Mountain/IPC/WindServiceHandlers/Storage/StorageGet.rs
1#![allow(non_snake_case)]
2
3//! Read a single value from persistent storage by key. The
4//! `false` first arg to `GetStorageValue` selects the
5//! workspace-scoped store; `true` would target global storage.
6
7use std::sync::Arc;
8
9use CommonLibrary::{Environment::Requires::Requires, Storage::StorageProvider::StorageProvider};
10use serde_json::Value;
11
12use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, dev_log};
13
14pub async fn StorageGet(RunTime:Arc<ApplicationRunTime>, Arguments:Vec<Value>) -> Result<Value, String> {
15 let key = Arguments
16 .first()
17 .ok_or("Missing storage key".to_string())?
18 .as_str()
19 .ok_or("Storage key must be a string".to_string())?;
20
21 let provider:Arc<dyn StorageProvider> = RunTime.Environment.Require();
22 let value = provider
23 .GetStorageValue(false, key)
24 .await
25 .map_err(|Error| format!("Failed to get storage item: {}", Error))?;
26
27 dev_log!("storage", "get: {}", key);
28 Ok(value.unwrap_or(Value::Null))
29}