feat(solc): add remove_contract utility function (#1436)
This commit is contained in:
parent
9fc55c0aed
commit
13e67aaa4a
|
@ -1,5 +1,5 @@
|
||||||
//! Commonly used identifiers for contracts in the compiled output
|
//! Commonly used identifiers for contracts in the compiled output
|
||||||
use std::{convert::TryFrom, fmt, str::FromStr};
|
use std::{borrow::Cow, convert::TryFrom, fmt, str::FromStr};
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
|
||||||
#[error("{0}")]
|
#[error("{0}")]
|
||||||
|
@ -77,6 +77,39 @@ impl From<FullContractInfo> for ContractInfo {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// The reference type for `ContractInfo`
|
||||||
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
||||||
|
pub struct ContractInfoRef<'a> {
|
||||||
|
pub path: Option<Cow<'a, str>>,
|
||||||
|
pub name: Cow<'a, str>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<ContractInfo> for ContractInfoRef<'a> {
|
||||||
|
fn from(info: ContractInfo) -> Self {
|
||||||
|
ContractInfoRef { path: info.path.map(Into::into), name: info.name.into() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a ContractInfo> for ContractInfoRef<'a> {
|
||||||
|
fn from(info: &'a ContractInfo) -> Self {
|
||||||
|
ContractInfoRef {
|
||||||
|
path: info.path.as_deref().map(Into::into),
|
||||||
|
name: info.name.as_str().into(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<'a> From<FullContractInfo> for ContractInfoRef<'a> {
|
||||||
|
fn from(info: FullContractInfo) -> Self {
|
||||||
|
ContractInfoRef { path: Some(info.path.into()), name: info.name.into() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> From<&'a FullContractInfo> for ContractInfoRef<'a> {
|
||||||
|
fn from(info: &'a FullContractInfo) -> Self {
|
||||||
|
ContractInfoRef { path: Some(info.path.as_str().into()), name: info.name.as_str().into() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Represents the common contract argument pattern `<path>:<contractname>`
|
/// Represents the common contract argument pattern `<path>:<contractname>`
|
||||||
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
#[derive(Clone, Debug, Eq, PartialEq, Hash)]
|
||||||
pub struct FullContractInfo {
|
pub struct FullContractInfo {
|
||||||
|
|
|
@ -6,6 +6,7 @@ use crate::{
|
||||||
Error,
|
Error,
|
||||||
},
|
},
|
||||||
buildinfo::RawBuildInfo,
|
buildinfo::RawBuildInfo,
|
||||||
|
info::ContractInfoRef,
|
||||||
sources::{VersionedSourceFile, VersionedSourceFiles},
|
sources::{VersionedSourceFile, VersionedSourceFiles},
|
||||||
ArtifactId, ArtifactOutput, Artifacts, CompilerOutput, ConfigurableArtifacts, SolcIoError,
|
ArtifactId, ArtifactOutput, Artifacts, CompilerOutput, ConfigurableArtifacts, SolcIoError,
|
||||||
};
|
};
|
||||||
|
@ -378,6 +379,38 @@ impl AggregatedCompilerOutput {
|
||||||
self.contracts.remove(path, contract)
|
self.contracts.remove(path, contract)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Removes the contract with matching path and name using the `<path>:<contractname>` pattern
|
||||||
|
/// where `path` is optional.
|
||||||
|
///
|
||||||
|
/// If the `path` segment is `None`, then the first matching `Contract` is returned, see
|
||||||
|
/// [Self::remove_first]
|
||||||
|
///
|
||||||
|
///
|
||||||
|
///
|
||||||
|
/// # Example
|
||||||
|
///
|
||||||
|
/// ```
|
||||||
|
/// use ethers_solc::Project;
|
||||||
|
/// use ethers_solc::artifacts::*;
|
||||||
|
/// use ethers_solc::info::ContractInfo;
|
||||||
|
/// # fn demo(project: Project) {
|
||||||
|
/// let mut output = project.compile().unwrap().output();
|
||||||
|
/// let info = ContractInfo::new("src/Greeter.sol:Greeter");
|
||||||
|
/// let contract = output.remove_contract(&info).unwrap();
|
||||||
|
/// # }
|
||||||
|
/// ```
|
||||||
|
pub fn remove_contract<'a>(
|
||||||
|
&mut self,
|
||||||
|
info: impl Into<ContractInfoRef<'a>>,
|
||||||
|
) -> Option<Contract> {
|
||||||
|
let ContractInfoRef { path, name } = info.into();
|
||||||
|
if let Some(path) = path {
|
||||||
|
self.remove(path, name)
|
||||||
|
} else {
|
||||||
|
self.remove_first(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Iterate over all contracts and their names
|
/// Iterate over all contracts and their names
|
||||||
pub fn contracts_iter(&self) -> impl Iterator<Item = (&String, &Contract)> {
|
pub fn contracts_iter(&self) -> impl Iterator<Item = (&String, &Contract)> {
|
||||||
self.contracts.contracts()
|
self.contracts.contracts()
|
||||||
|
|
|
@ -15,6 +15,7 @@ use ethers_solc::{
|
||||||
},
|
},
|
||||||
buildinfo::BuildInfo,
|
buildinfo::BuildInfo,
|
||||||
cache::{SolFilesCache, SOLIDITY_FILES_CACHE_FILENAME},
|
cache::{SolFilesCache, SOLIDITY_FILES_CACHE_FILENAME},
|
||||||
|
info::ContractInfo,
|
||||||
project_util::*,
|
project_util::*,
|
||||||
remappings::Remapping,
|
remappings::Remapping,
|
||||||
CompilerInput, ConfigurableArtifacts, ExtraOutputValues, Graph, Project, ProjectCompileOutput,
|
CompilerInput, ConfigurableArtifacts, ExtraOutputValues, Graph, Project, ProjectCompileOutput,
|
||||||
|
@ -1548,9 +1549,16 @@ fn can_compile_sparse_with_link_references() {
|
||||||
let lib = dup.remove_first("MyLib");
|
let lib = dup.remove_first("MyLib");
|
||||||
assert!(lib.is_none());
|
assert!(lib.is_none());
|
||||||
|
|
||||||
let lib = output.remove(my_lib_path.to_string_lossy(), "MyLib");
|
dup = output.clone();
|
||||||
|
let lib = dup.remove(my_lib_path.to_string_lossy(), "MyLib");
|
||||||
assert!(lib.is_some());
|
assert!(lib.is_some());
|
||||||
let lib = output.remove(my_lib_path.to_string_lossy(), "MyLib");
|
let lib = dup.remove(my_lib_path.to_string_lossy(), "MyLib");
|
||||||
|
assert!(lib.is_none());
|
||||||
|
|
||||||
|
let info = ContractInfo::new(format!("{}:{}", my_lib_path.to_string_lossy(), "MyLib"));
|
||||||
|
let lib = output.remove_contract(&info);
|
||||||
|
assert!(lib.is_some());
|
||||||
|
let lib = output.remove_contract(&info);
|
||||||
assert!(lib.is_none());
|
assert!(lib.is_none());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue