revive_solc_json_interface/standard_json/input/
mod.rs

1//! The `solc --standard-json` input.
2
3pub mod language;
4pub mod settings;
5pub mod source;
6
7use std::collections::BTreeMap;
8use std::collections::BTreeSet;
9use std::path::PathBuf;
10
11#[cfg(all(feature = "parallel", feature = "resolc"))]
12use rayon::iter::{IntoParallelIterator, ParallelIterator};
13use serde::Deserialize;
14use serde::Serialize;
15
16use crate::standard_json::input::settings::metadata::Metadata as SolcStandardJsonInputSettingsMetadata;
17use crate::standard_json::input::settings::optimizer::Optimizer as SolcStandardJsonInputSettingsOptimizer;
18use crate::standard_json::input::settings::selection::Selection as SolcStandardJsonInputSettingsSelection;
19#[cfg(feature = "resolc")]
20use crate::warning::Warning;
21use crate::SolcStandardJsonInputSettingsPolkaVM;
22
23use self::language::Language;
24use self::settings::Settings;
25use self::source::Source;
26
27/// The `solc --standard-json` input.
28#[derive(Clone, Debug, Serialize, Deserialize)]
29#[serde(rename_all = "camelCase")]
30pub struct Input {
31    /// The input language.
32    pub language: Language,
33    /// The input source code files hashmap.
34    pub sources: BTreeMap<String, Source>,
35    /// The compiler settings.
36    pub settings: Settings,
37    /// The suppressed warnings.
38    #[cfg(feature = "resolc")]
39    #[serde(skip_serializing)]
40    pub suppressed_warnings: Option<Vec<Warning>>,
41}
42
43impl Input {
44    /// A shortcut constructor from stdin.
45    pub fn try_from_stdin() -> anyhow::Result<Self> {
46        let mut input: Self = serde_json::from_reader(std::io::BufReader::new(std::io::stdin()))?;
47        input
48            .settings
49            .output_selection
50            .get_or_insert_with(SolcStandardJsonInputSettingsSelection::default)
51            .extend_with_required();
52        Ok(input)
53    }
54
55    /// A shortcut constructor from paths.
56    #[allow(clippy::too_many_arguments)]
57    pub fn try_from_paths(
58        language: Language,
59        evm_version: Option<revive_common::EVMVersion>,
60        paths: &[PathBuf],
61        library_map: Vec<String>,
62        remappings: Option<BTreeSet<String>>,
63        output_selection: SolcStandardJsonInputSettingsSelection,
64        optimizer: SolcStandardJsonInputSettingsOptimizer,
65        metadata: Option<SolcStandardJsonInputSettingsMetadata>,
66        #[cfg(feature = "resolc")] suppressed_warnings: Option<Vec<Warning>>,
67        polkavm: Option<SolcStandardJsonInputSettingsPolkaVM>,
68    ) -> anyhow::Result<Self> {
69        let mut paths: BTreeSet<PathBuf> = paths.iter().cloned().collect();
70        let libraries = Settings::parse_libraries(library_map)?;
71        for library_file in libraries.keys() {
72            paths.insert(PathBuf::from(library_file));
73        }
74
75        let sources = paths
76            .iter()
77            .map(|path| {
78                let source = Source::try_from(path.as_path()).unwrap_or_else(|error| {
79                    panic!("Source code file {path:?} reading error: {error}")
80                });
81                (path.to_string_lossy().to_string(), source)
82            })
83            .collect();
84
85        Ok(Self {
86            language,
87            sources,
88            settings: Settings::new(
89                evm_version,
90                libraries,
91                remappings,
92                output_selection,
93                optimizer,
94                metadata,
95                polkavm,
96            ),
97            #[cfg(feature = "resolc")]
98            suppressed_warnings,
99        })
100    }
101
102    /// A shortcut constructor from source code.
103    /// Only for the integration test purposes.
104    #[cfg(feature = "resolc")]
105    #[allow(clippy::too_many_arguments)]
106    pub fn try_from_sources(
107        evm_version: Option<revive_common::EVMVersion>,
108        sources: BTreeMap<String, String>,
109        libraries: BTreeMap<String, BTreeMap<String, String>>,
110        remappings: Option<BTreeSet<String>>,
111        output_selection: SolcStandardJsonInputSettingsSelection,
112        optimizer: SolcStandardJsonInputSettingsOptimizer,
113        metadata: Option<SolcStandardJsonInputSettingsMetadata>,
114        suppressed_warnings: Option<Vec<Warning>>,
115        polkavm: Option<SolcStandardJsonInputSettingsPolkaVM>,
116    ) -> anyhow::Result<Self> {
117        #[cfg(feature = "parallel")]
118        let iter = sources.into_par_iter(); // Parallel iterator
119
120        #[cfg(not(feature = "parallel"))]
121        let iter = sources.into_iter(); // Sequential iterator
122        let sources = iter
123            .map(|(path, content)| (path, Source::from(content)))
124            .collect();
125
126        Ok(Self {
127            language: Language::Solidity,
128            sources,
129            settings: Settings::new(
130                evm_version,
131                libraries,
132                remappings,
133                output_selection,
134                optimizer,
135                metadata,
136                polkavm,
137            ),
138            suppressed_warnings,
139        })
140    }
141
142    /// Sets the necessary defaults.
143    pub fn normalize(&mut self, version: &semver::Version) {
144        self.settings.normalize(version);
145    }
146}