chore(solc): add more convenience functions (#810)

* chore(solc): simplify solcconfig

* chore: more helper functions

* chore: copy all

* fix: add trailing slash

* chore: make clippy happy
This commit is contained in:
Matthias Seitz 2022-01-20 20:41:19 +01:00 committed by GitHub
parent fe12d10f29
commit 6d7875a44c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 72 additions and 31 deletions

View File

@ -307,8 +307,7 @@ impl SolFilesCacheBuilder {
cache_file: Option<PathBuf>, cache_file: Option<PathBuf>,
) -> Result<SolFilesCache> { ) -> Result<SolFilesCache> {
let format = self.format.unwrap_or_else(|| ETHERS_FORMAT_VERSION.to_string()); let format = self.format.unwrap_or_else(|| ETHERS_FORMAT_VERSION.to_string());
let solc_config = let solc_config = self.solc_config.unwrap_or_else(|| SolcConfig::builder().build());
self.solc_config.map(Ok).unwrap_or_else(|| SolcConfig::builder().build())?;
let root = self let root = self
.root .root

View File

@ -55,9 +55,8 @@ static LOCK: Lazy<Mutex<()>> = Lazy::new(|| Mutex::new(()));
/// installation is detected. /// installation is detected.
#[cfg(any(test, feature = "tests"))] #[cfg(any(test, feature = "tests"))]
#[allow(unused)] #[allow(unused)]
pub(crate) fn take_solc_installer_lock() -> std::sync::LockResult<std::sync::MutexGuard<'static, ()>> pub(crate) fn take_solc_installer_lock() -> std::sync::MutexGuard<'static, ()> {
{ LOCK.lock().unwrap()
LOCK.lock()
} }
#[cfg(all(feature = "svm", feature = "async"))] #[cfg(all(feature = "svm", feature = "async"))]

View File

@ -415,7 +415,7 @@ impl SolcConfig {
/// ///
/// ```rust /// ```rust
/// use ethers_solc::SolcConfig; /// use ethers_solc::SolcConfig;
/// let config = SolcConfig::builder().build().unwrap(); /// let config = SolcConfig::builder().build();
/// ``` /// ```
pub fn builder() -> SolcConfigBuilder { pub fn builder() -> SolcConfigBuilder {
SolcConfigBuilder::default() SolcConfigBuilder::default()
@ -436,9 +436,9 @@ impl SolcConfigBuilder {
/// Creates the solc config /// Creates the solc config
/// ///
/// If no solc version is configured then it will be determined by calling `solc --version`. /// If no solc version is configured then it will be determined by calling `solc --version`.
pub fn build(self) -> Result<SolcConfig> { pub fn build(self) -> SolcConfig {
let Self { settings } = self; let Self { settings } = self;
Ok(SolcConfig { settings: settings.unwrap_or_default() }) SolcConfig { settings: settings.unwrap_or_default() }
} }
} }

View File

@ -46,7 +46,7 @@ impl SolcError {
pub(crate) fn solc(msg: impl Into<String>) -> Self { pub(crate) fn solc(msg: impl Into<String>) -> Self {
SolcError::SolcError(msg.into()) SolcError::SolcError(msg.into())
} }
pub(crate) fn msg(msg: impl Into<String>) -> Self { pub fn msg(msg: impl Into<String>) -> Self {
SolcError::Message(msg.into()) SolcError::Message(msg.into())
} }
} }

View File

@ -576,25 +576,51 @@ impl<Artifacts: ArtifactOutput> ProjectBuilder<Artifacts> {
self self
} }
#[must_use]
pub fn ignore_error_codes(mut self, codes: impl IntoIterator<Item = u64>) -> Self {
for code in codes {
self = self.ignore_error_code(code);
}
self
}
/// Disables cached builds /// Disables cached builds
#[must_use] #[must_use]
pub fn ephemeral(mut self) -> Self { pub fn ephemeral(self) -> Self {
self.cached = false; self.set_cached(false)
}
/// Sets the cache status
#[must_use]
pub fn set_cached(mut self, cached: bool) -> Self {
self.cached = cached;
self self
} }
/// Disables writing artifacts to disk /// Disables writing artifacts to disk
#[must_use] #[must_use]
pub fn no_artifacts(mut self) -> Self { pub fn no_artifacts(self) -> Self {
self.no_artifacts = true; self.set_no_artifacts(true)
}
/// Sets the no artifacts status
#[must_use]
pub fn set_no_artifacts(mut self, artifacts: bool) -> Self {
self.no_artifacts = artifacts;
self
}
/// Sets automatic solc version detection
#[must_use]
pub fn set_auto_detect(mut self, auto_detect: bool) -> Self {
self.auto_detect = auto_detect;
self self
} }
/// Disables automatic solc version detection /// Disables automatic solc version detection
#[must_use] #[must_use]
pub fn no_auto_detect(mut self) -> Self { pub fn no_auto_detect(self) -> Self {
self.auto_detect = false; self.set_auto_detect(false)
self
} }
/// Sets the maximum number of parallel `solc` processes to run simultaneously. /// Sets the maximum number of parallel `solc` processes to run simultaneously.
@ -678,7 +704,7 @@ impl<Artifacts: ArtifactOutput> ProjectBuilder<Artifacts> {
} = self; } = self;
let solc = solc.unwrap_or_default(); let solc = solc.unwrap_or_default();
let solc_config = solc_config.map(Ok).unwrap_or_else(|| SolcConfig::builder().build())?; let solc_config = solc_config.unwrap_or_else(|| SolcConfig::builder().build());
let paths = paths.map(Ok).unwrap_or_else(ProjectPathsConfig::current_hardhat)?; let paths = paths.map(Ok).unwrap_or_else(ProjectPathsConfig::current_hardhat)?;

View File

@ -199,7 +199,7 @@ fn dir_copy_options() -> dir::CopyOptions {
skip_exist: false, skip_exist: false,
buffer_size: 64000, //64kb buffer_size: 64000, //64kb
copy_inside: true, copy_inside: true,
content_only: false, content_only: true,
depth: 0, depth: 0,
} }
} }

View File

@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};
use std::{ use std::{
collections::{hash_map::Entry, HashMap}, collections::{hash_map::Entry, HashMap},
fmt, fmt,
fmt::Write,
path::{Path, PathBuf}, path::{Path, PathBuf},
str::FromStr, str::FromStr,
}; };
@ -93,7 +94,11 @@ impl<'de> Deserialize<'de> for Remapping {
// Remappings are printed as `prefix=target` // Remappings are printed as `prefix=target`
impl fmt::Display for Remapping { impl fmt::Display for Remapping {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}={}", self.name, self.path) write!(f, "{}={}", self.name, self.path)?;
if !self.path.ends_with('/') {
f.write_char('/')?;
}
Ok(())
} }
} }
@ -220,13 +225,25 @@ impl RelativeRemapping {
// Remappings are printed as `prefix=target` // Remappings are printed as `prefix=target`
impl fmt::Display for RelativeRemapping { impl fmt::Display for RelativeRemapping {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}={}", self.name, self.path.original().display()) let mut s = format!("{}={}", self.name, self.path.original().display());
if !s.ends_with('/') {
s.push('/');
}
f.write_str(&s)
} }
} }
impl From<RelativeRemapping> for Remapping { impl From<RelativeRemapping> for Remapping {
fn from(r: RelativeRemapping) -> Self { fn from(r: RelativeRemapping) -> Self {
Remapping { name: r.name, path: r.path.relative().to_string_lossy().to_string() } let RelativeRemapping { mut name, path } = r;
let mut path = format!("{}", path.relative().display());
if !path.ends_with('/') {
path.push('/');
}
if !name.ends_with('/') {
name.push('/');
}
Remapping { name, path }
} }
} }

View File

@ -322,8 +322,8 @@ fn can_flatten_file() {
assert!(result.is_ok()); assert!(result.is_ok());
let result = result.unwrap(); let result = result.unwrap();
assert!(result.find("contract Foo").is_some()); assert!(result.contains("contract Foo"));
assert!(result.find("contract Bar").is_some()); assert!(result.contains("contract Bar"));
} }
#[test] #[test]
@ -340,8 +340,8 @@ fn can_flatten_file_with_external_lib() {
assert!(result.is_ok()); assert!(result.is_ok());
let result = result.unwrap(); let result = result.unwrap();
assert!(result.find("library console").is_some()); assert!(result.contains("library console"));
assert!(result.find("contract Greeter").is_some()); assert!(result.contains("contract Greeter"));
} }
#[test] #[test]
@ -356,9 +356,9 @@ fn can_flatten_file_in_dapp_sample() {
assert!(result.is_ok()); assert!(result.is_ok());
let result = result.unwrap(); let result = result.unwrap();
assert!(result.find("contract DSTest").is_some()); assert!(result.contains("contract DSTest"));
assert!(result.find("contract Dapp").is_some()); assert!(result.contains("contract Dapp"));
assert!(result.find("contract DappTest").is_some()); assert!(result.contains("contract DappTest"));
} }
#[test] #[test]
@ -373,7 +373,7 @@ fn can_flatten_file_with_duplicates() {
assert!(result.is_ok()); assert!(result.is_ok());
let result = result.unwrap(); let result = result.unwrap();
assert!(result.matches("contract Foo {").collect::<Vec<_>>().len() == 1); assert_eq!(result.matches("contract Foo {").count(), 1);
assert!(result.matches("contract Bar {").collect::<Vec<_>>().len() == 1); assert_eq!(result.matches("contract Bar {").count(), 1);
assert!(result.matches("contract FooBar {").collect::<Vec<_>>().len() == 1); assert_eq!(result.matches("contract FooBar {").count(), 1);
} }