Skip to content

Commit

Permalink
feat: parse inline config with solang (#7431)
Browse files Browse the repository at this point in the history
* feat: parse inline config with solang

* fix: multiple single docs are merged

* fix: dont merge lines

* fix: read absolute path

* perf: fast path with src.contains

* fix: split on dots

* chore: clippy
  • Loading branch information
DaniPopes authored Mar 18, 2024
1 parent 04fca21 commit 6dfc6e7
Show file tree
Hide file tree
Showing 7 changed files with 267 additions and 139 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ debug = 1
# Solc and artifacts
foundry-compilers.opt-level = 3
solang-parser.opt-level = 3
lalrpop-util.opt-level = 3
serde_json.opt-level = 3

# EVM
Expand Down
2 changes: 2 additions & 0 deletions crates/config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ alloy-chains = { workspace = true, features = ["serde"] }
alloy-primitives = { workspace = true, features = ["serde"] }
revm-primitives = { workspace = true, default-features = false, features = ["std"] }

solang-parser.workspace = true

dirs-next = "2"
dunce = "1"
eyre.workspace = true
Expand Down
28 changes: 5 additions & 23 deletions crates/config/src/inline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub use conf_parser::{parse_config_bool, parse_config_u32, validate_profiles, In
pub use error::{InlineConfigError, InlineConfigParserError};
pub use natspec::NatSpec;
use once_cell::sync::Lazy;
use std::{borrow::Cow, collections::HashMap};
use std::collections::HashMap;

mod conf_parser;
mod error;
Expand All @@ -25,22 +25,14 @@ static INLINE_CONFIG_PREFIX_SELECTED_PROFILE: Lazy<String> = Lazy::new(|| {
pub struct InlineConfig<T> {
/// Maps a (test-contract, test-function) pair
/// to a specific configuration provided by the user.
configs: HashMap<InlineConfigKey<'static>, T>,
configs: HashMap<(String, String), T>,
}

impl<T> InlineConfig<T> {
/// Returns an inline configuration, if any, for a test function.
/// Configuration is identified by the pair "contract", "function".
pub fn get<C, F>(&self, contract_id: C, fn_name: F) -> Option<&T>
where
C: Into<String>,
F: Into<String>,
{
// TODO use borrow
let key = InlineConfigKey {
contract: Cow::Owned(contract_id.into()),
function: Cow::Owned(fn_name.into()),
};
pub fn get(&self, contract_id: &str, fn_name: &str) -> Option<&T> {
let key = (contract_id.to_string(), fn_name.to_string());
self.configs.get(&key)
}

Expand All @@ -51,21 +43,11 @@ impl<T> InlineConfig<T> {
C: Into<String>,
F: Into<String>,
{
let key = InlineConfigKey {
contract: Cow::Owned(contract_id.into()),
function: Cow::Owned(fn_name.into()),
};
let key = (contract_id.into(), fn_name.into());
self.configs.insert(key, config);
}
}

/// Represents a (test-contract, test-function) pair
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
struct InlineConfigKey<'a> {
contract: Cow<'a, str>,
function: Cow<'a, str>,
}

pub(crate) fn remove_whitespaces(s: &str) -> String {
s.chars().filter(|c| !c.is_whitespace()).collect()
}
Loading

0 comments on commit 6dfc6e7

Please sign in to comment.