Skip to main content

miden_protocol/account/component/storage/schema/
value_slot.rs

1use alloc::collections::BTreeMap;
2use alloc::string::String;
3
4use super::super::type_registry::SchemaRequirement;
5use super::super::{InitStorageData, StorageValueName};
6use super::{WordSchema, validate_description_ascii};
7use crate::Word;
8use crate::account::StorageSlotName;
9use crate::errors::ComponentMetadataError;
10use crate::utils::serde::{
11    ByteReader,
12    ByteWriter,
13    Deserializable,
14    DeserializationError,
15    Serializable,
16};
17
18// VALUE SLOT SCHEMA
19// ================================================================================================
20
21/// Describes the schema for a storage value slot.
22#[derive(Debug, Clone, PartialEq, Eq)]
23pub struct ValueSlotSchema {
24    description: Option<String>,
25    word: WordSchema,
26}
27
28impl ValueSlotSchema {
29    pub fn new(description: Option<String>, word: WordSchema) -> Self {
30        Self { description, word }
31    }
32
33    pub fn description(&self) -> Option<&String> {
34        self.description.as_ref()
35    }
36
37    pub fn word(&self) -> &WordSchema {
38        &self.word
39    }
40
41    pub(super) fn collect_init_value_requirements(
42        &self,
43        value_name: StorageValueName,
44        requirements: &mut BTreeMap<StorageValueName, SchemaRequirement>,
45    ) -> Result<(), ComponentMetadataError> {
46        self.word.collect_init_value_requirements(
47            value_name,
48            self.description.clone(),
49            requirements,
50        )
51    }
52
53    /// Builds a [Word] from the provided initialization data using the inner word schema.
54    pub fn try_build_word(
55        &self,
56        init_storage_data: &InitStorageData,
57        slot_name: &StorageSlotName,
58    ) -> Result<Word, ComponentMetadataError> {
59        self.word.try_build_word(init_storage_data, slot_name)
60    }
61
62    pub(super) fn write_into_with_optional_defaults<W: ByteWriter>(
63        &self,
64        target: &mut W,
65        include_defaults: bool,
66    ) {
67        target.write(&self.description);
68        self.word.write_into_with_optional_defaults(target, include_defaults);
69    }
70
71    pub(super) fn validate(&self) -> Result<(), ComponentMetadataError> {
72        if let Some(description) = self.description.as_deref() {
73            validate_description_ascii(description)?;
74        }
75        self.word.validate()?;
76        Ok(())
77    }
78}
79
80impl Serializable for ValueSlotSchema {
81    fn write_into<W: ByteWriter>(&self, target: &mut W) {
82        self.write_into_with_optional_defaults(target, true);
83    }
84}
85
86impl Deserializable for ValueSlotSchema {
87    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
88        let description = Option::<String>::read_from(source)?;
89        let word = WordSchema::read_from(source)?;
90        Ok(ValueSlotSchema::new(description, word))
91    }
92}