mirror of
https://github.com/pezkuwichain/revive.git
synced 2026-04-23 18:58:00 +00:00
factor out solc JSON interface crate (#264)
The differential testing framework will make a second consumer. There seems to be no re-usable Rust crate for this. But we already have everything here, just needs a small refactor to make it fully re-usable. - Mostly decouple the solc JSON-input-output interface types from the `solidity` frontend crate - Expose the JSON-input-output interface types in a dedicated crate --------- Signed-off-by: Cyrill Leutwiler <bigcyrill@hotmail.com>
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
//! The `solc --combined-json` contract.
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
/// The contract.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "kebab-case")]
|
||||
pub struct Contract {
|
||||
/// The `solc` hashes output.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub hashes: Option<BTreeMap<String, String>>,
|
||||
/// The `solc` ABI output.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub abi: Option<serde_json::Value>,
|
||||
/// The `solc` metadata output.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub metadata: Option<String>,
|
||||
/// The `solc` developer documentation output.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub devdoc: Option<serde_json::Value>,
|
||||
/// The `solc` user documentation output.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub userdoc: Option<serde_json::Value>,
|
||||
/// The `solc` storage layout output.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub storage_layout: Option<serde_json::Value>,
|
||||
/// The `solc` AST output.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub ast: Option<serde_json::Value>,
|
||||
/// The `solc` assembly output.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub asm: Option<serde_json::Value>,
|
||||
/// The `solc` hexadecimal binary output.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub bin: Option<String>,
|
||||
/// The `solc` hexadecimal binary runtime part output.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub bin_runtime: Option<String>,
|
||||
/// The factory dependencies.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub factory_deps: Option<BTreeMap<String, String>>,
|
||||
/// The missing libraries.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub missing_libraries: Option<HashSet<String>>,
|
||||
}
|
||||
|
||||
impl Contract {
|
||||
/// Returns the signature hash of the specified contract entry.
|
||||
/// # Panics
|
||||
/// If the hashes have not been requested in the `solc` call.
|
||||
pub fn entry(&self, entry: &str) -> u32 {
|
||||
self.hashes
|
||||
.as_ref()
|
||||
.expect("Always exists")
|
||||
.iter()
|
||||
.find_map(|(contract_entry, hash)| {
|
||||
if contract_entry.starts_with(entry) {
|
||||
Some(
|
||||
u32::from_str_radix(hash.as_str(), revive_common::BASE_HEXADECIMAL)
|
||||
.expect("Test hash is always valid"),
|
||||
)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| panic!("Entry `{entry}` not found"))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
//! The `solc --combined-json` output.
|
||||
|
||||
pub mod contract;
|
||||
|
||||
use std::collections::BTreeMap;
|
||||
use std::fs::File;
|
||||
use std::io::Write;
|
||||
use std::path::Path;
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
use self::contract::Contract;
|
||||
|
||||
/// The `solc --combined-json` output.
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct CombinedJson {
|
||||
/// The contract entries.
|
||||
pub contracts: BTreeMap<String, Contract>,
|
||||
/// The list of source files.
|
||||
#[serde(rename = "sourceList")]
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub source_list: Option<Vec<String>>,
|
||||
/// The source code extra data, including the AST.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub sources: Option<serde_json::Value>,
|
||||
/// The `solc` compiler version.
|
||||
pub version: String,
|
||||
/// The `resolc` compiler version.
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub revive_version: Option<String>,
|
||||
}
|
||||
|
||||
impl CombinedJson {
|
||||
/// Returns the signature hash of the specified contract and entry.
|
||||
pub fn entry(&self, path: &str, entry: &str) -> u32 {
|
||||
self.contracts
|
||||
.iter()
|
||||
.find_map(|(name, contract)| {
|
||||
if name.starts_with(path) {
|
||||
Some(contract)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
.expect("Always exists")
|
||||
.entry(entry)
|
||||
}
|
||||
|
||||
/// Returns the full contract path which can be found in `combined-json` output.
|
||||
pub fn get_full_path(&self, name: &str) -> Option<String> {
|
||||
self.contracts.iter().find_map(|(path, _value)| {
|
||||
if let Some(last_slash_position) = path.rfind('/') {
|
||||
if let Some(colon_position) = path.rfind(':') {
|
||||
if &path[last_slash_position + 1..colon_position] == name {
|
||||
return Some(path.to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
})
|
||||
}
|
||||
|
||||
/// Removes EVM artifacts to prevent their accidental usage.
|
||||
pub fn remove_evm(&mut self) {
|
||||
for (_, contract) in self.contracts.iter_mut() {
|
||||
contract.bin = None;
|
||||
contract.bin_runtime = None;
|
||||
}
|
||||
}
|
||||
|
||||
/// Writes the JSON to the specified directory.
|
||||
pub fn write_to_directory(
|
||||
self,
|
||||
output_directory: &Path,
|
||||
overwrite: bool,
|
||||
) -> anyhow::Result<()> {
|
||||
let mut file_path = output_directory.to_owned();
|
||||
file_path.push(format!("combined.{}", revive_common::EXTENSION_JSON));
|
||||
|
||||
if file_path.exists() && !overwrite {
|
||||
anyhow::bail!(
|
||||
"Refusing to overwrite an existing file {file_path:?} (use --overwrite to force)."
|
||||
);
|
||||
}
|
||||
|
||||
File::create(&file_path)
|
||||
.map_err(|error| anyhow::anyhow!("File {:?} creating error: {}", file_path, error))?
|
||||
.write_all(serde_json::to_vec(&self).expect("Always valid").as_slice())
|
||||
.map_err(|error| anyhow::anyhow!("File {:?} writing error: {}", file_path, error))?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user