miden_assembly_syntax/library/
module.rs

1use alloc::vec::Vec;
2
3use super::LibraryPath;
4use crate::{
5    Word,
6    ast::{ProcedureIndex, ProcedureName},
7};
8
9// MODULE INFO
10// ================================================================================================
11
12#[derive(Debug, Clone, PartialEq, Eq)]
13pub struct ModuleInfo {
14    path: LibraryPath,
15    procedures: Vec<ProcedureInfo>,
16}
17
18impl ModuleInfo {
19    /// Returns a new [`ModuleInfo`] instantiated library path.
20    pub fn new(path: LibraryPath) -> Self {
21        Self { path, procedures: Vec::new() }
22    }
23
24    /// Adds a procedure to the module.
25    pub fn add_procedure(&mut self, name: ProcedureName, digest: Word) {
26        self.procedures.push(ProcedureInfo { name, digest });
27    }
28
29    /// Returns the module's library path.
30    pub fn path(&self) -> &LibraryPath {
31        &self.path
32    }
33
34    /// Returns the number of procedures in the module.
35    pub fn num_procedures(&self) -> usize {
36        self.procedures.len()
37    }
38
39    /// Returns the [`ProcedureInfo`] of the procedure at the provided index, if any.
40    pub fn get_procedure_by_index(&self, index: ProcedureIndex) -> Option<&ProcedureInfo> {
41        self.procedures.get(index.as_usize())
42    }
43
44    /// Returns the digest of the procedure with the provided name, if any.
45    pub fn get_procedure_digest_by_name(&self, name: &ProcedureName) -> Option<Word> {
46        self.procedures.iter().find_map(|proc_info| {
47            if &proc_info.name == name {
48                Some(proc_info.digest)
49            } else {
50                None
51            }
52        })
53    }
54
55    /// Returns an iterator over the procedure infos in the module with their corresponding
56    /// procedure index in the module.
57    pub fn procedures(&self) -> impl Iterator<Item = (ProcedureIndex, &ProcedureInfo)> {
58        self.procedures
59            .iter()
60            .enumerate()
61            .map(|(idx, proc)| (ProcedureIndex::new(idx), proc))
62    }
63
64    /// Returns an iterator over the MAST roots of procedures defined in this module.
65    pub fn procedure_digests(&self) -> impl Iterator<Item = Word> + '_ {
66        self.procedures.iter().map(|p| p.digest)
67    }
68}
69
70/// Stores the name and digest of a procedure.
71#[derive(Debug, Clone, PartialEq, Eq)]
72pub struct ProcedureInfo {
73    pub name: ProcedureName,
74    pub digest: Word,
75}