revive_solc_json_interface/standard_json/output/
mod.rs

1//! The `solc --standard-json` output.
2
3pub mod contract;
4pub mod error;
5pub mod source;
6
7use std::collections::BTreeMap;
8
9use serde::Deserialize;
10use serde::Serialize;
11
12#[cfg(feature = "resolc")]
13use crate::warning::Warning;
14
15use self::contract::Contract;
16use self::error::Error as SolcStandardJsonOutputError;
17use self::source::Source;
18
19/// The `solc --standard-json` output.
20#[derive(Debug, Serialize, Deserialize, Clone, Default)]
21pub struct Output {
22    /// The file-contract hashmap.
23    #[serde(default, skip_serializing_if = "Option::is_none")]
24    pub contracts: Option<BTreeMap<String, BTreeMap<String, Contract>>>,
25    /// The source code mapping data.
26    #[serde(default, skip_serializing_if = "Option::is_none")]
27    pub sources: Option<BTreeMap<String, Source>>,
28    /// The compilation errors and warnings.
29    #[serde(default, skip_serializing_if = "Option::is_none")]
30    pub errors: Option<Vec<SolcStandardJsonOutputError>>,
31    /// The `solc` compiler version.
32    #[serde(skip_serializing_if = "Option::is_none")]
33    pub version: Option<String>,
34    /// The `solc` compiler long version.
35    #[serde(skip_serializing_if = "Option::is_none")]
36    pub long_version: Option<String>,
37    /// The `resolc` compiler version.
38    #[serde(skip_serializing_if = "Option::is_none")]
39    pub revive_version: Option<String>,
40}
41
42impl Output {
43    /// Traverses the AST and returns the list of additional errors and warnings.
44    #[cfg(feature = "resolc")]
45    pub fn preprocess_ast(&mut self, suppressed_warnings: &[Warning]) -> anyhow::Result<()> {
46        let sources = match self.sources.as_ref() {
47            Some(sources) => sources,
48            None => return Ok(()),
49        };
50
51        let mut messages = Vec::new();
52        for (path, source) in sources.iter() {
53            if let Some(ast) = source.ast.as_ref() {
54                let mut polkavm_messages = Source::get_messages(ast, suppressed_warnings);
55                for message in polkavm_messages.iter_mut() {
56                    message.push_contract_path(path.as_str());
57                }
58                messages.extend(polkavm_messages);
59            }
60        }
61        self.errors = match self.errors.take() {
62            Some(mut errors) => {
63                errors.extend(messages);
64                Some(errors)
65            }
66            None => Some(messages),
67        };
68
69        Ok(())
70    }
71}