pub mod file;
use std::collections::BTreeMap;
use serde::Deserialize;
use serde::Serialize;
use self::file::flag::Flag;
use self::file::File as FileSelection;
#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq)]
pub struct PerFileSelection {
#[serde(skip_serializing_if = "BTreeMap::is_empty", flatten)]
pub files: BTreeMap<String, FileSelection>,
}
impl PerFileSelection {
pub fn extend(&mut self, other: Self) {
for (entry, file) in other.files {
self.files
.entry(entry)
.and_modify(|v| {
v.extend(file.clone());
})
.or_insert(file);
}
}
pub fn selection_to_prune(&self) -> Self {
let files = self
.files
.iter()
.map(|(k, v)| (k.to_owned(), v.selection_to_prune()))
.collect();
Self { files }
}
pub fn contains(&self, path: &String, flag: Flag) -> Option<bool> {
if let Some(file) = self.files.get(path) {
return Some(file.contains(flag));
};
None
}
pub fn is_empty(&self) -> bool {
self.files.is_empty()
}
pub fn retain(&mut self) {
for file in self.files.values_mut() {
file.per_contract.retain(|flag| !flag.is_evm_codegen());
file.per_file.retain(|flag| !flag.is_evm_codegen());
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize, Default, PartialEq)]
pub struct Selection {
#[serde(default, rename = "*", skip_serializing_if = "FileSelection::is_empty")]
pub all: FileSelection,
#[serde(skip_serializing_if = "PerFileSelection::is_empty", flatten)]
pub files: PerFileSelection,
}
impl Selection {
pub fn new_all(flags: Vec<Flag>) -> Self {
Self {
all: FileSelection::new(flags),
files: Default::default(),
}
}
pub fn new_required_for_codegen_all() -> Self {
Self::new_all(Flag::codegen_requirements().into())
}
pub fn new_required_for_codegen(output_selection: &Self) -> Self {
if output_selection.all.requests_codegen() {
return Self::new_required_for_codegen_all();
}
let mut files = PerFileSelection::default();
for (file_name, file_selection) in &output_selection.files.files {
if file_selection.requests_codegen() {
files.files.insert(
file_name.to_owned(),
FileSelection::new_required_for_codegen(),
);
}
}
Self {
all: Default::default(),
files,
}
}
pub fn new_required_for_tests() -> Self {
Self {
all: FileSelection::new_required_for_tests(),
files: Default::default(),
}
}
pub fn new_yul_validation() -> Self {
Self::new_all(vec![Flag::EVM])
}
pub fn extend(&mut self, other: Self) -> &mut Self {
self.all.extend(other.all);
self.files.extend(other.files);
self
}
pub fn selection_to_prune(&self) -> Self {
Self {
all: self.all.selection_to_prune(),
files: self.files.selection_to_prune(),
}
}
pub fn contains(&self, path: &String, flag: Flag) -> bool {
self.files
.contains(path, flag)
.unwrap_or(self.all.contains(flag))
}
pub fn retain(&mut self) {
self.all.per_file.retain(|flag| !flag.is_evm_codegen());
self.all.per_contract.retain(|flag| !flag.is_evm_codegen());
self.files.retain();
}
}