miden-client 0.16.0

Client library that facilitates interaction with the Miden network
Documentation
//! The `settings` module provides methods for managing arbitrary setting values that are persisted
//! in the client's store.

use alloc::string::String;
use alloc::vec::Vec;

use miden_tx::utils::serde::{Deserializable, Serializable};

use super::Client;
use crate::errors::ClientError;
use crate::store::SettingScope;

// CLIENT METHODS
// ================================================================================================

/// This section of the [Client] contains methods for:
///
/// - **Settings accessors:** Methods to get, set, and delete setting values from the store.
/// - **Default account ID:** Methods to get, set, and delete the default account ID. This is a
///   wrapper around a specific setting value.
impl<AUTH> Client<AUTH> {
    // SETTINGS ACCESSORS
    // --------------------------------------------------------------------------------------------

    /// Sets a setting value in the store. It can then be retrieved using `get_setting`.
    pub async fn set_setting<T: Serializable>(
        &self,
        key: String,
        value: T,
    ) -> Result<(), ClientError> {
        self.store
            .set_setting(SettingScope::User, key, value.to_bytes())
            .await
            .map_err(Into::into)
    }

    /// Retrieves the value for `key`, or `None` if it hasn’t been set.
    pub async fn get_setting<T: Deserializable>(
        &self,
        key: String,
    ) -> Result<Option<T>, ClientError> {
        self.store
            .get_setting(SettingScope::User, key)
            .await
            .map(|value| value.map(|value| Deserializable::read_from_bytes(&value)))?
            .transpose()
            .map_err(Into::into)
    }

    /// Deletes the setting value from the store. Returns `true` if `key` had a value set.
    pub async fn remove_setting(&self, key: String) -> Result<bool, ClientError> {
        self.store.remove_setting(SettingScope::User, key).await.map_err(Into::into)
    }

    /// Returns all the setting keys from the store.
    pub async fn list_setting_keys(&self) -> Result<Vec<String>, ClientError> {
        self.store.list_setting_keys(SettingScope::User).await.map_err(Into::into)
    }
}