feat(solc): add versioned artifacts helper (#1752)

This commit is contained in:
Matthias Seitz 2022-09-28 20:58:12 +02:00 committed by GitHub
parent 599cdb1541
commit 3ee1b6796e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 20 additions and 1 deletions

View File

@ -80,11 +80,30 @@ impl<T: ArtifactOutput> ProjectCompileOutput<T> {
/// let artifacts: BTreeMap<String, &ConfigurableContractArtifact> = project.compile().unwrap().artifacts().collect(); /// let artifacts: BTreeMap<String, &ConfigurableContractArtifact> = project.compile().unwrap().artifacts().collect();
/// ``` /// ```
pub fn artifacts(&self) -> impl Iterator<Item = (String, &T::Artifact)> { pub fn artifacts(&self) -> impl Iterator<Item = (String, &T::Artifact)> {
self.versioned_artifacts().map(|(name, (artifact, _))| (name, artifact))
}
/// This returns a chained iterator of both cached and recompiled contract artifacts that yields
/// the contract name and the corresponding artifact
///
/// # Example
///
/// ```no_run
/// use std::collections::btree_map::BTreeMap;
/// use semver::Version;
/// use ethers_solc::ConfigurableContractArtifact;
/// use ethers_solc::Project;
///
/// let project = Project::builder().build().unwrap();
/// let artifacts: BTreeMap<String, (&ConfigurableContractArtifact, &Version)> = project.compile().unwrap().versioned_artifacts().collect();
/// ```
pub fn versioned_artifacts(&self) -> impl Iterator<Item = (String, (&T::Artifact, &Version))> {
self.cached_artifacts self.cached_artifacts
.artifact_files() .artifact_files()
.chain(self.compiled_artifacts.artifact_files()) .chain(self.compiled_artifacts.artifact_files())
.filter_map(|artifact| { .filter_map(|artifact| {
T::contract_name(&artifact.file).map(|name| (name, &artifact.artifact)) T::contract_name(&artifact.file)
.map(|name| (name, (&artifact.artifact, &artifact.version)))
}) })
} }