Skip to main content

revive_solc_json_interface/standard_json/input/settings/selection/
mod.rs

1//! The `solc --standard-json` output selection.
2
3pub mod file;
4
5use std::collections::BTreeMap;
6
7use serde::Deserialize;
8use serde::Serialize;
9
10use self::file::flag::Flag;
11use self::file::File as FileSelection;
12
13/// The `solc --standard-json` per-file output selection.
14#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq)]
15pub struct PerFileSelection {
16    /// Individual file selection configuration, required for foundry.
17    #[serde(skip_serializing_if = "BTreeMap::is_empty", flatten)]
18    pub files: BTreeMap<String, FileSelection>,
19}
20
21impl PerFileSelection {
22    /// Extends the output selection with another one.
23    pub fn extend(&mut self, other: Self) {
24        for (entry, file) in other.files {
25            self.files
26                .entry(entry)
27                .and_modify(|v| {
28                    v.extend(file.clone());
29                })
30                .or_insert(file);
31        }
32    }
33
34    /// Returns flags that are going to be automatically added by the compiler,
35    /// but were not explicitly requested by the user.
36    ///
37    /// The `all` selection is first merged into each file selection.
38    ///
39    /// Afterwards, the flags are used to prune JSON output before returning it.
40    pub fn selection_to_prune(&self, all: &FileSelection) -> Self {
41        let files = self
42            .files
43            .iter()
44            .map(|(path, file)| {
45                let mut merged = file.clone();
46                merged.extend(all.clone());
47                (path.to_owned(), merged.selection_to_prune())
48            })
49            .collect();
50        Self { files }
51    }
52
53    /// Checks whether `path` contains the `flag` or `None` if there is no selection for `path`.
54    pub fn contains(&self, path: &String, flag: Flag) -> Option<bool> {
55        if let Some(file) = self.files.get(path) {
56            return Some(file.contains(flag));
57        };
58        None
59    }
60
61    /// Checks whether this is the empty per file selection.
62    pub fn is_empty(&self) -> bool {
63        self.files.is_empty()
64    }
65
66    /// Removes unneeded selections.
67    pub fn retain(&mut self) {
68        for file in self.files.values_mut() {
69            file.per_contract.retain(|flag| !flag.is_evm_codegen());
70            file.per_file.retain(|flag| !flag.is_evm_codegen());
71        }
72    }
73}
74
75/// The `solc --standard-json` output selection.
76#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq)]
77pub struct Selection {
78    /// Common configuration for all files.
79    #[serde(default, rename = "*", skip_serializing_if = "FileSelection::is_empty")]
80    pub all: FileSelection,
81    /// Individual file selection configuration, required for foundry.
82    #[serde(skip_serializing_if = "PerFileSelection::is_empty", flatten)]
83    pub files: PerFileSelection,
84}
85
86impl Selection {
87    /// Creates the selection for all contracts in all files with arbitrary `flags`.
88    pub fn new_all(flags: Vec<Flag>) -> Self {
89        Self {
90            all: FileSelection::new(flags),
91            files: Default::default(),
92        }
93    }
94
95    /// Creates the selection required by our compilation process for all contracts in all files.
96    pub fn new_required_for_codegen_all() -> Self {
97        Self::new_all(Flag::codegen_requirements().into())
98    }
99
100    /// Creates the selection required by our compilation process for each contract in
101    /// `output_selection` requesting code generation.
102    pub fn new_required_for_codegen(output_selection: &Self) -> Self {
103        if output_selection.all.requests_codegen() {
104            return Self::new_required_for_codegen_all();
105        }
106
107        let mut files = PerFileSelection::default();
108        for (file_name, file_selection) in &output_selection.files.files {
109            if file_selection.requests_codegen() {
110                files.files.insert(
111                    file_name.to_owned(),
112                    FileSelection::new_required_for_codegen(),
113                );
114            }
115        }
116
117        Self {
118            all: Default::default(),
119            files,
120        }
121    }
122
123    /// Creates the selection required for test compilation (includes EVM bytecode) for all
124    /// contracts in all files.
125    pub fn new_required_for_tests() -> Self {
126        Self {
127            all: FileSelection::new_required_for_tests(),
128            files: Default::default(),
129        }
130    }
131
132    /// Creates the selection required by Yul validation process.
133    pub fn new_yul_validation() -> Self {
134        Self::new_all(vec![Flag::EVM])
135    }
136
137    /// Extends the output selection with another one.
138    pub fn extend(&mut self, other: Self) -> &mut Self {
139        self.all.extend(other.all);
140        self.files.extend(other.files);
141        self
142    }
143
144    /// Returns flags that were not explicitly requested by the user.
145    ///
146    /// These flags are used to prune JSON output before returning it,
147    /// removing any output that was automatically added but not requested.
148    pub fn selection_to_prune(&self) -> Self {
149        Self {
150            all: self.all.selection_to_prune(),
151            files: self.files.selection_to_prune(&self.all),
152        }
153    }
154
155    /// Checks whether the `flag` is requested.
156    pub fn contains(&self, path: &String, flag: Flag) -> bool {
157        self.files
158            .contains(path, flag)
159            .unwrap_or(self.all.contains(flag))
160    }
161
162    /// Removes unneeded selections.
163    pub fn retain(&mut self) {
164        self.all.per_file.retain(|flag| !flag.is_evm_codegen());
165        self.all.per_contract.retain(|flag| !flag.is_evm_codegen());
166        self.files.retain();
167    }
168}