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