multiversx_sc_meta_lib/abi_json/
build_info_abi_json.rs

1use multiversx_sc::abi::{BuildInfoAbi, ContractCrateBuildAbi, FrameworkBuildAbi};
2use serde::{Deserialize, Serialize};
3
4#[derive(Serialize, Deserialize)]
5#[serde(rename_all = "camelCase")]
6pub struct BuildInfoAbiJson {
7    pub rustc: RustcAbiJson,
8    pub contract_crate: ContractCrateBuildAbiJson,
9    pub framework: FrameworkBuildAbiJson,
10}
11
12impl From<&BuildInfoAbi> for BuildInfoAbiJson {
13    fn from(abi: &BuildInfoAbi) -> Self {
14        BuildInfoAbiJson {
15            rustc: RustcAbiJson::create(),
16            contract_crate: ContractCrateBuildAbiJson::from(&abi.contract_crate),
17            framework: FrameworkBuildAbiJson::from(&abi.framework),
18        }
19    }
20}
21
22#[derive(Serialize, Deserialize)]
23#[serde(rename_all = "camelCase")]
24pub struct RustcAbiJson {
25    pub version: String,
26    pub commit_hash: String,
27    pub commit_date: String,
28    pub channel: String,
29    pub short: String,
30}
31
32impl RustcAbiJson {
33    pub fn create() -> Self {
34        let meta = rustc_version::version_meta().unwrap();
35        RustcAbiJson {
36            version: meta.semver.to_string(),
37            commit_hash: meta.commit_hash.unwrap_or_default(),
38            commit_date: meta.commit_date.unwrap_or_default(),
39            channel: format!("{:?}", meta.channel),
40            short: meta.short_version_string,
41        }
42    }
43}
44
45#[derive(Serialize, Deserialize)]
46#[serde(rename_all = "camelCase")]
47pub struct ContractCrateBuildAbiJson {
48    pub name: String,
49    pub version: String,
50    #[serde(default)]
51    #[serde(skip_serializing_if = "String::is_empty")]
52    pub git_version: String,
53}
54
55impl From<&ContractCrateBuildAbi> for ContractCrateBuildAbiJson {
56    fn from(abi: &ContractCrateBuildAbi) -> Self {
57        ContractCrateBuildAbiJson {
58            name: abi.name.to_string(),
59            version: abi.version.to_string(),
60            git_version: abi.git_version.to_string(),
61        }
62    }
63}
64
65#[derive(Serialize, Deserialize)]
66pub struct FrameworkBuildAbiJson {
67    pub name: String,
68    pub version: String,
69}
70
71impl From<&FrameworkBuildAbi> for FrameworkBuildAbiJson {
72    fn from(abi: &FrameworkBuildAbi) -> Self {
73        FrameworkBuildAbiJson {
74            name: abi.name.to_string(),
75            version: abi.version.to_string(),
76        }
77    }
78}