revive_solc_json_interface/combined_json/
contract.rs

1//! The `solc --combined-json` contract.
2
3use std::collections::BTreeMap;
4use std::collections::HashSet;
5
6use serde::Deserialize;
7use serde::Serialize;
8
9/// The contract.
10#[derive(Debug, Serialize, Deserialize)]
11#[serde(rename_all = "kebab-case")]
12pub struct Contract {
13    /// The `solc` hashes output.
14    #[serde(skip_serializing_if = "Option::is_none")]
15    pub hashes: Option<BTreeMap<String, String>>,
16    /// The `solc` ABI output.
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub abi: Option<serde_json::Value>,
19    /// The `solc` metadata output.
20    #[serde(skip_serializing_if = "Option::is_none")]
21    pub metadata: Option<String>,
22    /// The `solc` developer documentation output.
23    #[serde(skip_serializing_if = "Option::is_none")]
24    pub devdoc: Option<serde_json::Value>,
25    /// The `solc` user documentation output.
26    #[serde(skip_serializing_if = "Option::is_none")]
27    pub userdoc: Option<serde_json::Value>,
28    /// The `solc` storage layout output.
29    #[serde(skip_serializing_if = "Option::is_none")]
30    pub storage_layout: Option<serde_json::Value>,
31    /// The `solc` AST output.
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub ast: Option<serde_json::Value>,
34    /// The `solc` assembly output.
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub asm: Option<serde_json::Value>,
37    /// The `solc` hexadecimal binary output.
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub bin: Option<String>,
40    /// The `solc` hexadecimal binary runtime part output.
41    #[serde(skip_serializing_if = "Option::is_none")]
42    pub bin_runtime: Option<String>,
43    /// The factory dependencies.
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub factory_deps: Option<BTreeMap<String, String>>,
46    /// The missing libraries.
47    #[serde(skip_serializing_if = "Option::is_none")]
48    pub missing_libraries: Option<HashSet<String>>,
49}
50
51impl Contract {
52    /// Returns the signature hash of the specified contract entry.
53    /// # Panics
54    /// If the hashes have not been requested in the `solc` call.
55    pub fn entry(&self, entry: &str) -> u32 {
56        self.hashes
57            .as_ref()
58            .expect("Always exists")
59            .iter()
60            .find_map(|(contract_entry, hash)| {
61                if contract_entry.starts_with(entry) {
62                    Some(
63                        u32::from_str_radix(hash.as_str(), revive_common::BASE_HEXADECIMAL)
64                            .expect("Test hash is always valid"),
65                    )
66                } else {
67                    None
68                }
69            })
70            .unwrap_or_else(|| panic!("Entry `{entry}` not found"))
71    }
72}