radix_transactions/manifest/
dumper.rs

1use crate::internal_prelude::*;
2use radix_common::network::NetworkDefinition;
3use std::fs::create_dir_all;
4use std::path::{Path, PathBuf};
5
6use super::decompiler::decompile;
7
8pub fn dump_manifest_to_file_system<P>(
9    manifest: &impl TypedReadableManifest,
10    directory_path: P,
11    name: Option<&str>,
12    network_definition: &NetworkDefinition,
13) -> Result<(), DumpManifestError>
14where
15    P: AsRef<Path>,
16{
17    let path = directory_path.as_ref().to_owned();
18
19    // Check that the path is a directory and not a file
20    if path.is_file() {
21        return Err(DumpManifestError::PathPointsToAFile(path));
22    }
23
24    // If the directory does not exist, then create it.
25    create_dir_all(&path)?;
26
27    // Decompile the transaction manifest to the manifest string and then write it to the
28    // directory
29    {
30        let manifest_string = decompile(manifest, network_definition)?;
31        let manifest_path = path.join(format!("{}.rtm", name.unwrap_or("transaction")));
32        std::fs::write(manifest_path, manifest_string)?;
33    }
34
35    // Write all of the blobs to the specified path
36    let blob_prefix = name.map(|n| format!("{n}-")).unwrap_or_default();
37    for (hash, blob_content) in manifest.get_blobs() {
38        let blob_path = path.join(format!("{blob_prefix}{hash}.blob"));
39        std::fs::write(blob_path, blob_content)?;
40    }
41
42    manifest.validate(ValidationRuleset::all())?;
43
44    Ok(())
45}
46
47#[derive(Debug)]
48pub enum DumpManifestError {
49    PathPointsToAFile(PathBuf),
50    IoError(std::io::Error),
51    DecompileError(DecompileError),
52    ManifestValidationError(ManifestValidationError),
53}
54
55impl From<std::io::Error> for DumpManifestError {
56    fn from(value: std::io::Error) -> Self {
57        Self::IoError(value)
58    }
59}
60
61impl From<DecompileError> for DumpManifestError {
62    fn from(value: DecompileError) -> Self {
63        Self::DecompileError(value)
64    }
65}
66
67impl From<ManifestValidationError> for DumpManifestError {
68    fn from(value: ManifestValidationError) -> Self {
69        Self::ManifestValidationError(value)
70    }
71}