multiversx_sc_meta_lib/
mxsc_file_json.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use serde::{Deserialize, Serialize};
use std::{fs::File, io::Write, path::Path};

use crate::{
    abi_json::{BuildInfoAbiJson, ContractAbiJson},
    report_info_json::ReportInfoJson,
};

#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MxscFileJson {
    pub build_info: BuildInfoAbiJson,
    pub abi: ContractAbiJson,
    pub code: String,
    pub report: ReportInfoJson,
}

pub fn serialize_mxsc_file_json(mxsc_file_json: &MxscFileJson) -> String {
    let buf = Vec::new();
    let formatter = serde_json::ser::PrettyFormatter::with_indent(b"    ");
    let mut ser = serde_json::Serializer::with_formatter(buf, formatter);
    mxsc_file_json.serialize(&mut ser).unwrap();
    let mut serialized = String::from_utf8(ser.into_inner()).unwrap();
    serialized.push('\n');
    serialized
}

pub fn save_mxsc_file_json(mxsc_file_json: &MxscFileJson, path: impl AsRef<Path>) {
    let mxsc_file_string = serialize_mxsc_file_json(mxsc_file_json);
    let mut mxsc_file = File::create(path).unwrap();
    write!(mxsc_file, "{mxsc_file_string}").unwrap();
}