rain_metadata/solc/
mod.rs

1use serde_json::Value;
2use strum::EnumString;
3use crate::error::Error;
4
5/// Represent section of a solidity artifact to extract
6#[derive(Copy, Clone, EnumString, strum::Display)]
7#[strum(serialize_all = "kebab-case")]
8pub enum ArtifactComponent {
9    Abi,
10    Bytecode,
11    DeployedBytecode,
12}
13
14/// extracts the given section of a solidity artifact as [Value]
15///
16/// does not perform any checks on the returned [Value] such as if
17/// it is null or not.
18/// The given data should be utf8 encoded json string bytes
19pub fn extract_artifact_component_json(
20    component: ArtifactComponent,
21    data: &[u8],
22) -> Result<Value, Error> {
23    let json = serde_json::from_str::<Value>(std::str::from_utf8(data)?)?;
24    match component {
25        ArtifactComponent::Abi => Ok(json["abi"].clone()),
26        ArtifactComponent::Bytecode => Ok(json["bytecode"].clone()),
27        ArtifactComponent::DeployedBytecode => Ok(json["deployedBytecode"].clone()),
28    }
29}