Skip to main content

Mountain/IPC/WindServiceHandlers/Storage/
StorageSet.rs

1#![allow(non_snake_case)]
2
3//! Write a single value to workspace-scoped storage. Atomic per
4//! key - concurrent set/get against the same key serialise
5//! through the StorageProvider's lock.
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 StorageSet(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 value = Arguments.get(1).ok_or("Missing storage value".to_string())?.clone();
22
23	let provider:Arc<dyn StorageProvider> = RunTime.Environment.Require();
24	provider
25		.UpdateStorageValue(false, key.to_string(), Some(value))
26		.await
27		.map_err(|Error| format!("Failed to set storage item: {}", Error))?;
28
29	dev_log!("storage", "set: {}", key);
30	Ok(Value::Null)
31}