miden_objects/account/component/template/storage/
init_storage_data.rs

1use alloc::{collections::BTreeMap, string::String};
2
3use super::StorageValueName;
4
5/// Represents the data required to initialize storage entries when instantiating an
6/// [AccountComponent](crate::account::AccountComponent) from a
7/// [template](crate::account::AccountComponentTemplate).
8///
9/// An [`InitStorageData`] can be created from a TOML string when the `std` feature flag is set.
10#[derive(Clone, Debug, Default)]
11pub struct InitStorageData {
12    /// A mapping of storage placeholder names to their corresponding storage values.
13    storage_placeholders: BTreeMap<StorageValueName, String>,
14}
15
16impl InitStorageData {
17    /// Creates a new instance of [InitStorageData].
18    ///
19    /// A [`BTreeMap`] is constructed from the passed iterator, so duplicate keys will cause
20    /// overridden values.
21    ///
22    /// # Parameters
23    ///
24    /// - `entries`: An iterable collection of key-value pairs.
25    pub fn new(entries: impl IntoIterator<Item = (StorageValueName, String)>) -> Self {
26        InitStorageData {
27            storage_placeholders: entries
28                .into_iter()
29                .filter(|(entry_name, _)| !entry_name.as_str().is_empty())
30                .collect(),
31        }
32    }
33
34    /// Retrieves a reference to the storage placeholders.
35    pub fn placeholders(&self) -> &BTreeMap<StorageValueName, String> {
36        &self.storage_placeholders
37    }
38
39    /// Returns a reference to the name corresponding to the placeholder, or
40    /// [`Option::None`] if the placeholder is not present.
41    pub fn get(&self, key: &StorageValueName) -> Option<&String> {
42        self.storage_placeholders.get(key)
43    }
44}