Skip to content
This repository has been archived by the owner on Oct 19, 2024. It is now read-only.

Commit

Permalink
feat(solc): add more borrowed artifact iterators (#2562)
Browse files Browse the repository at this point in the history
* feat(solc): add more borrowed artifact iterators

* chore: clippy
  • Loading branch information
DaniPopes authored Aug 21, 2023
1 parent 179891d commit bdb44cf
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ethers-providers/src/rpc/transports/ws/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::fmt;

mod types;
pub use types::ConnectionDetails;
pub(self) use types::*;
use types::*;

mod error;
pub use error::*;
Expand Down
40 changes: 40 additions & 0 deletions ethers-solc/src/artifact_output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,33 @@ impl<T> Artifacts<T> {
self.0.values_mut().flat_map(BTreeMap::values_mut).flatten()
}

/// Returns an iterator over _all_ artifacts and `<file name:contract name>`.
///
/// Borrowed version of [`Self::into_artifacts`].
pub fn artifacts<O: ArtifactOutput<Artifact = T>>(
&self,
) -> impl Iterator<Item = (ArtifactId, &T)> + '_ {
self.0.iter().flat_map(|(file, contract_artifacts)| {
contract_artifacts.iter().flat_map(move |(_contract_name, artifacts)| {
let source = PathBuf::from(file.clone());
artifacts.iter().filter_map(move |artifact| {
O::contract_name(&artifact.file).map(|name| {
(
ArtifactId {
path: PathBuf::from(&artifact.file),
name,
source: source.clone(),
version: artifact.version.clone(),
}
.with_slashed_paths(),
&artifact.artifact,
)
})
})
})
})
}

/// Returns an iterator over _all_ artifacts and `<file name:contract name>`
pub fn into_artifacts<O: ArtifactOutput<Artifact = T>>(
self,
Expand All @@ -288,6 +315,19 @@ impl<T> Artifacts<T> {
})
}

/// Returns an iterator that yields the tuple `(file, contract name, artifact)`
///
/// **NOTE** this returns the path as is
///
/// Borrowed version of [`Self::into_artifacts_with_files`].
pub fn artifacts_with_files(&self) -> impl Iterator<Item = (&String, &String, &T)> + '_ {
self.0.iter().flat_map(|(f, contract_artifacts)| {
contract_artifacts.iter().flat_map(move |(name, artifacts)| {
artifacts.iter().map(move |artifact| (f, name, &artifact.artifact))
})
})
}

/// Returns an iterator that yields the tuple `(file, contract name, artifact)`
///
/// **NOTE** this returns the path as is
Expand Down
26 changes: 26 additions & 0 deletions ethers-solc/src/compile/output/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ impl<T: ArtifactOutput> ProjectCompileOutput<T> {
self
}

/// All artifacts together with their contract file name and name `<file name>:<name>`.
///
/// This returns a chained iterator of both cached and recompiled contract artifacts.
///
/// Borrowed version of [`Self::into_artifacts`].
pub fn artifact_ids(&self) -> impl Iterator<Item = (ArtifactId, &T::Artifact)> + '_ {
let Self { cached_artifacts, compiled_artifacts, .. } = self;
cached_artifacts.artifacts::<T>().chain(compiled_artifacts.artifacts::<T>())
}

/// All artifacts together with their contract file name and name `<file name>:<name>`
///
/// This returns a chained iterator of both cached and recompiled contract artifacts
Expand Down Expand Up @@ -111,6 +121,22 @@ impl<T: ArtifactOutput> ProjectCompileOutput<T> {
})
}

/// All artifacts together with their contract file and name as tuple `(file, contract
/// name, artifact)`
///
/// This returns a chained iterator of both cached and recompiled contract artifacts
///
/// Borrowed version of [`Self::into_artifacts_with_files`].
///
/// **NOTE** the `file` will be returned as is, see also
/// [`Self::with_stripped_file_prefixes()`].
pub fn artifacts_with_files(
&self,
) -> impl Iterator<Item = (&String, &String, &T::Artifact)> + '_ {
let Self { cached_artifacts, compiled_artifacts, .. } = self;
cached_artifacts.artifacts_with_files().chain(compiled_artifacts.artifacts_with_files())
}

/// All artifacts together with their contract file and name as tuple `(file, contract
/// name, artifact)`
///
Expand Down

0 comments on commit bdb44cf

Please sign in to comment.