miden_mast_package/
artifact.rs

1use alloc::sync::Arc;
2
3use miden_assembly_syntax::Library;
4use miden_core::{Program, Word, mast::MastForest};
5#[cfg(feature = "serde")]
6use serde::{Deserialize, Serialize};
7
8// MAST ARTIFACT
9// ================================================================================================
10
11/// The artifact produced by lowering a program or library to a Merkelized Abstract Syntax Tree
12///
13/// This type is used in compilation pipelines to abstract over the type of output requested.
14#[derive(Debug, Clone, Eq, PartialEq, derive_more::From)]
15#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
16pub enum MastArtifact {
17    /// A MAST artifact which can be executed by the VM directly
18    Executable(Arc<Program>),
19    /// A MAST artifact which can be used as a dependency by a [Program]
20    Library(Arc<Library>),
21}
22
23impl MastArtifact {
24    /// Get the underlying [Program] for this artifact, or panic if this is a [Library]
25    pub fn unwrap_program(self) -> Arc<Program> {
26        match self {
27            Self::Executable(prog) => prog,
28            Self::Library(_) => panic!("attempted to unwrap 'mast' library as program"),
29        }
30    }
31
32    /// Get the underlying [Library] for this artifact, or panic if this is a [Program]
33    pub fn unwrap_library(self) -> Arc<Library> {
34        match self {
35            Self::Executable(_) => panic!("attempted to unwrap 'mast' program as library"),
36            Self::Library(lib) => lib,
37        }
38    }
39
40    /// Get the content digest associated with this artifact
41    pub fn digest(&self) -> Word {
42        match self {
43            Self::Executable(prog) => prog.hash(),
44            Self::Library(lib) => *lib.digest(),
45        }
46    }
47
48    /// Get the underlying [MastForest] for this artifact
49    pub fn mast_forest(&self) -> &MastForest {
50        match self {
51            Self::Executable(prog) => prog.mast_forest(),
52            Self::Library(lib) => lib.mast_forest(),
53        }
54    }
55}