revive_solc_json_interface/standard_json/input/settings/selection/
mod.rs1pub 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#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq)]
15pub struct PerFileSelection {
16 #[serde(skip_serializing_if = "BTreeMap::is_empty", flatten)]
18 pub files: BTreeMap<String, FileSelection>,
19}
20
21impl PerFileSelection {
22 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 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 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 pub fn is_empty(&self) -> bool {
63 self.files.is_empty()
64 }
65
66 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#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq)]
77pub struct Selection {
78 #[serde(default, rename = "*", skip_serializing_if = "FileSelection::is_empty")]
80 pub all: FileSelection,
81 #[serde(skip_serializing_if = "PerFileSelection::is_empty", flatten)]
83 pub files: PerFileSelection,
84}
85
86impl Selection {
87 pub fn new_all(flags: Vec<Flag>) -> Self {
89 Self {
90 all: FileSelection::new(flags),
91 files: Default::default(),
92 }
93 }
94
95 pub fn new_required_for_codegen_all() -> Self {
97 Self::new_all(Flag::codegen_requirements().into())
98 }
99
100 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 pub fn new_required_for_tests() -> Self {
126 Self {
127 all: FileSelection::new_required_for_tests(),
128 files: Default::default(),
129 }
130 }
131
132 pub fn new_yul_validation() -> Self {
134 Self::new_all(vec![Flag::EVM])
135 }
136
137 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 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 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 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}