1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//! `nsh/storage`: the app's key-value storage, strings by string key kept
//! on the device (`crate::storage`).
use crate::element::{JsError, codes, module, with_storage};
/// The app's key-value storage: strings by string key, kept on the device
/// across launches (up to 5 MB). Calls run in order, so a read after a
/// write sees it.
///
/// # Rust
///
/// Every function is `async` (a promise), as `nsh/storage`'s callers
/// await them alike.
#[module]
pub(crate) struct Storage;
#[module]
impl Storage {
/// The value of `key`, `null` when it has none.
pub(crate) async fn get_item(key: String) -> Option<String> {
with_storage(|s| s.get(&key))
}
/// Sets `key` to `value`; rejects with `ERR_NSH_QUOTA_EXCEEDED` when
/// the storage would be full (the value is not set).
pub(crate) async fn set_item(key: String, value: String) -> Result<(), JsError> {
with_storage(|s| s.set(key, value))
.map_err(|full| JsError::Error(full.to_string()).with_code(codes::QUOTA_EXCEEDED))
}
/// Removes `key`.
pub(crate) async fn remove_item(key: String) -> Result<(), String> {
with_storage(|s| s.remove(&key));
Ok(())
}
/// Every key, in order.
pub(crate) async fn keys() -> Vec<String> {
with_storage(|s| s.keys())
}
/// Removes every key.
pub(crate) async fn clear() -> Result<(), String> {
with_storage(|s| s.clear());
Ok(())
}
}