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