revive_solc_json_interface/combined_json/
mod.rs1pub mod contract;
4
5use std::collections::BTreeMap;
6use std::fs::File;
7use std::io::Write;
8use std::path::Path;
9
10use serde::Deserialize;
11use serde::Serialize;
12
13use self::contract::Contract;
14
15#[derive(Debug, Serialize, Deserialize)]
17pub struct CombinedJson {
18 pub contracts: BTreeMap<String, Contract>,
20 #[serde(rename = "sourceList")]
22 #[serde(skip_serializing_if = "Option::is_none")]
23 pub source_list: Option<Vec<String>>,
24 #[serde(skip_serializing_if = "Option::is_none")]
26 pub sources: Option<serde_json::Value>,
27 pub version: String,
29 #[serde(skip_serializing_if = "Option::is_none")]
31 pub revive_version: Option<String>,
32}
33
34impl CombinedJson {
35 pub fn entry(&self, path: &str, entry: &str) -> u32 {
37 self.contracts
38 .iter()
39 .find_map(|(name, contract)| {
40 if name.starts_with(path) {
41 Some(contract)
42 } else {
43 None
44 }
45 })
46 .expect("Always exists")
47 .entry(entry)
48 }
49
50 pub fn get_full_path(&self, name: &str) -> Option<String> {
52 self.contracts.iter().find_map(|(path, _value)| {
53 if let Some(last_slash_position) = path.rfind('/') {
54 if let Some(colon_position) = path.rfind(':') {
55 if &path[last_slash_position + 1..colon_position] == name {
56 return Some(path.to_owned());
57 }
58 }
59 }
60
61 None
62 })
63 }
64
65 pub fn remove_evm(&mut self) {
67 for (_, contract) in self.contracts.iter_mut() {
68 contract.bin = None;
69 contract.bin_runtime = None;
70 }
71 }
72
73 pub fn write_to_directory(
75 self,
76 output_directory: &Path,
77 overwrite: bool,
78 ) -> anyhow::Result<()> {
79 let mut file_path = output_directory.to_owned();
80 file_path.push(format!("combined.{}", revive_common::EXTENSION_JSON));
81
82 if file_path.exists() && !overwrite {
83 anyhow::bail!(
84 "Refusing to overwrite an existing file {file_path:?} (use --overwrite to force)."
85 );
86 }
87
88 File::create(&file_path)
89 .map_err(|error| anyhow::anyhow!("File {:?} creating error: {}", file_path, error))?
90 .write_all(serde_json::to_vec(&self).expect("Always valid").as_slice())
91 .map_err(|error| anyhow::anyhow!("File {:?} writing error: {}", file_path, error))?;
92
93 Ok(())
94 }
95}