Skip to main content

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