radix_transactions/manifest/
compiler.rs

1use crate::internal_prelude::*;
2
3#[derive(Debug, Clone, PartialEq, Eq)]
4pub enum CompileError {
5    LexerError(lexer::LexerError),
6    ParserError(parser::ParserError),
7    GeneratorError(generator::GeneratorError),
8}
9
10#[derive(Debug, Copy, Clone, PartialEq, Eq)]
11pub enum CompileErrorDiagnosticsStyle {
12    PlainText,
13    TextTerminalColors,
14}
15
16pub fn compile_error_diagnostics(
17    s: &str,
18    err: CompileError,
19    style: CompileErrorDiagnosticsStyle,
20) -> String {
21    match err {
22        CompileError::LexerError(err) => lexer::lexer_error_diagnostics(s, err, style),
23        CompileError::ParserError(err) => parser::parser_error_diagnostics(s, err, style),
24        CompileError::GeneratorError(err) => generator::generator_error_diagnostics(s, err, style),
25    }
26}
27
28// Kept for backwards compatibility of downstream clients / integrators
29pub use compile_manifest_v1 as compile;
30
31pub fn compile_manifest_v1(
32    manifest_string: &str,
33    network: &NetworkDefinition,
34    blobs: impl IsBlobProvider,
35) -> Result<TransactionManifestV1, CompileError> {
36    compile_manifest(manifest_string, network, blobs)
37}
38
39pub fn compile_any_manifest_with_pretty_error(
40    manifest_string: &str,
41    manifest_kind: ManifestKind,
42    network: &NetworkDefinition,
43    blobs: impl IsBlobProvider,
44    error_style: CompileErrorDiagnosticsStyle,
45) -> Result<AnyManifest, String> {
46    compile_any_manifest(manifest_string, manifest_kind, network, blobs)
47        .map_err(|err| compile_error_diagnostics(manifest_string, err, error_style))
48}
49
50pub fn compile_manifest_with_pretty_error<M: BuildableManifest>(
51    manifest_string: &str,
52    network: &NetworkDefinition,
53    blobs: impl IsBlobProvider,
54    error_style: CompileErrorDiagnosticsStyle,
55) -> Result<M, String> {
56    compile_manifest(manifest_string, network, blobs)
57        .map_err(|err| compile_error_diagnostics(manifest_string, err, error_style))
58}
59
60pub fn compile_any_manifest(
61    manifest_string: &str,
62    manifest_kind: ManifestKind,
63    network: &NetworkDefinition,
64    blobs: impl IsBlobProvider,
65) -> Result<AnyManifest, CompileError> {
66    let manifest = match manifest_kind {
67        ManifestKind::V1 => {
68            compile_manifest::<TransactionManifestV1>(manifest_string, network, blobs)?.into()
69        }
70        ManifestKind::SystemV1 => {
71            compile_manifest::<SystemTransactionManifestV1>(manifest_string, network, blobs)?.into()
72        }
73        ManifestKind::V2 => {
74            compile_manifest::<TransactionManifestV2>(manifest_string, network, blobs)?.into()
75        }
76        ManifestKind::SubintentV2 => {
77            compile_manifest::<SubintentManifestV2>(manifest_string, network, blobs)?.into()
78        }
79    };
80
81    Ok(manifest)
82}
83
84pub fn compile_manifest<M: BuildableManifest>(
85    s: &str,
86    network: &NetworkDefinition,
87    blobs: impl IsBlobProvider,
88) -> Result<M, CompileError> {
89    let address_bech32_decoder = AddressBech32Decoder::new(network);
90    let transaction_bech32_decoder = TransactionHashBech32Decoder::new(network);
91
92    let tokens = lexer::tokenize(s).map_err(CompileError::LexerError)?;
93    let instructions = parser::Parser::new(tokens, parser::PARSER_MAX_DEPTH)
94        .map_err(CompileError::ParserError)?
95        .parse_manifest()
96        .map_err(CompileError::ParserError)?;
97    generator::generate_manifest(
98        &instructions,
99        &address_bech32_decoder,
100        &transaction_bech32_decoder,
101        blobs,
102    )
103    .map_err(CompileError::GeneratorError)
104}