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