multiversx_sc_meta_lib/
mxsc_file_json.rs1use serde::{Deserialize, Serialize};
2use std::{fs::File, io::Write, path::Path};
3
4use crate::{
5 abi_json::{BuildInfoAbiJson, ContractAbiJson},
6 report_info_json::ReportInfoJson,
7};
8
9#[derive(Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub struct MxscFileJson {
12 pub build_info: BuildInfoAbiJson,
13 pub abi: ContractAbiJson,
14 pub code: String,
15 pub report: ReportInfoJson,
16}
17
18pub fn serialize_mxsc_file_json(mxsc_file_json: &MxscFileJson) -> String {
19 let buf = Vec::new();
20 let formatter = serde_json::ser::PrettyFormatter::with_indent(b" ");
21 let mut ser = serde_json::Serializer::with_formatter(buf, formatter);
22 mxsc_file_json.serialize(&mut ser).unwrap();
23 let mut serialized = String::from_utf8(ser.into_inner()).unwrap();
24 serialized.push('\n');
25 serialized
26}
27
28pub fn save_mxsc_file_json(mxsc_file_json: &MxscFileJson, path: impl AsRef<Path>) {
29 let mxsc_file_string = serialize_mxsc_file_json(mxsc_file_json);
30 let mut mxsc_file = File::create(path).unwrap();
31 write!(mxsc_file, "{mxsc_file_string}").unwrap();
32}