miden_client/settings/
mod.rs

1//! The `settings` module provides methods for managing arbitrary setting values that are persisted
2//! in the client's store.
3
4use alloc::string::String;
5use alloc::vec::Vec;
6
7use miden_tx::utils::{Deserializable, Serializable};
8
9use super::Client;
10use crate::errors::ClientError;
11
12// CLIENT METHODS
13// ================================================================================================
14
15/// This section of the [Client] contains methods for:
16///
17/// - **Settings accessors:** Methods to get, set, and delete setting values from the store.
18/// - **Default account ID:** Methods to get, set, and delete the default account ID. This is a
19///   wrapper around a specific setting value.
20impl<AUTH> Client<AUTH> {
21    // SETTINGS ACCESSORS
22    // --------------------------------------------------------------------------------------------
23
24    /// Sets a setting value in the store. It can then be retrieved using `get_setting`.
25    pub async fn set_setting<T: Serializable>(
26        &mut self,
27        key: String,
28        value: T,
29    ) -> Result<(), ClientError> {
30        self.store.set_setting(key, value.to_bytes()).await.map_err(Into::into)
31    }
32
33    /// Retrieves the value for `key`, or `None` if it hasn’t been set.
34    pub async fn get_setting<T: Deserializable>(
35        &self,
36        key: String,
37    ) -> Result<Option<T>, ClientError> {
38        self.store
39            .get_setting(key)
40            .await
41            .map(|value| value.map(|value| Deserializable::read_from_bytes(&value)))?
42            .transpose()
43            .map_err(Into::into)
44    }
45
46    /// Deletes the setting value from the store.
47    pub async fn remove_setting(&mut self, key: String) -> Result<(), ClientError> {
48        self.store.remove_setting(key).await.map_err(Into::into)
49    }
50
51    /// Returns all the setting keys from the store.
52    pub async fn list_setting_keys(&self) -> Result<Vec<String>, ClientError> {
53        self.store.list_setting_keys().await.map_err(Into::into)
54    }
55}