Skip to main content

miden_core/mast/serialization/
view.rs

1use alloc::vec::Vec;
2
3use super::{MastNodeEntry, MastNodeInfo};
4use crate::{Word, mast::MastNodeId, serde::DeserializationError};
5
6/// Read-only view over serialization-oriented MAST node metadata.
7///
8/// This trait lives alongside [`super::SerializedMastForest`] because its surface is defined in
9/// terms of serialized-equivalent node entries and digests, even though both
10/// [`super::SerializedMastForest`] and in-memory [`crate::mast::MastForest`] implement it.
11pub trait MastForestView {
12    /// Returns the number of nodes in the forest.
13    fn node_count(&self) -> usize;
14
15    /// Returns fixed-width structural metadata for a node at the specified index.
16    ///
17    /// Returns an error if `index >= self.node_count()`.
18    fn node_entry_at(&self, index: usize) -> Result<MastNodeEntry, DeserializationError>;
19
20    /// Returns the digest of the node at the specified index.
21    ///
22    /// Returns an error if `index >= self.node_count()`.
23    fn node_digest_at(&self, index: usize) -> Result<Word, DeserializationError>;
24
25    /// Returns serialized-equivalent metadata for a node at the specified index.
26    ///
27    /// Returns an error if `index >= self.node_count()`.
28    fn node_info_at(&self, index: usize) -> Result<MastNodeInfo, DeserializationError> {
29        Ok(MastNodeInfo::from_entry(
30            self.node_entry_at(index)?,
31            self.node_digest_at(index)?,
32        ))
33    }
34
35    /// Returns the number of procedure roots in the forest.
36    fn procedure_root_count(&self) -> usize;
37
38    /// Returns the procedure root id at the specified index.
39    ///
40    /// Returns an error if `index >= self.procedure_root_count()`.
41    fn procedure_root_at(&self, index: usize) -> Result<MastNodeId, DeserializationError>;
42
43    /// Returns true when the forest contains no nodes.
44    fn is_empty(&self) -> bool {
45        self.node_count() == 0
46    }
47
48    /// Returns true when `index` is a valid node index.
49    fn has_node(&self, index: usize) -> bool {
50        index < self.node_count()
51    }
52
53    /// Returns all node infos in index order.
54    fn all_node_infos(&self) -> Result<Vec<MastNodeInfo>, DeserializationError> {
55        (0..self.node_count()).map(|index| self.node_info_at(index)).collect()
56    }
57
58    /// Returns all procedure roots in index order.
59    fn procedure_roots(&self) -> Result<Vec<MastNodeId>, DeserializationError> {
60        (0..self.procedure_root_count())
61            .map(|index| self.procedure_root_at(index))
62            .collect()
63    }
64}