chore(solc): add another artifacts helper type (#1722)

This commit is contained in:
Matthias Seitz 2022-09-19 22:49:28 +02:00 committed by GitHub
parent f0aceb86b5
commit 23171d9299
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 64 additions and 7 deletions

View File

@ -14,7 +14,7 @@ use crate::{
}; };
use ethers_core::{abi::Abi, types::Bytes}; use ethers_core::{abi::Abi, types::Bytes};
use semver::Version; use semver::Version;
use serde::{de::DeserializeOwned, Serialize}; use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::{ use std::{
borrow::Cow, borrow::Cow,
collections::{btree_map::BTreeMap, HashSet}, collections::{btree_map::BTreeMap, HashSet},
@ -34,7 +34,7 @@ use crate::files::MappedContract;
pub use configurable::*; pub use configurable::*;
/// Represents unique artifact metadata for identifying artifacts on output /// Represents unique artifact metadata for identifying artifacts on output
#[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash)] #[derive(Debug, Clone, Ord, PartialOrd, Eq, PartialEq, Hash, Serialize, Deserialize)]
pub struct ArtifactId { pub struct ArtifactId {
/// `artifact` cache path /// `artifact` cache path
pub path: PathBuf, pub path: PathBuf,

View File

@ -1,14 +1,19 @@
use crate::{ use crate::{
artifacts::{ artifacts::{
contract::{CompactContractRef, Contract}, contract::{CompactContractRef, Contract},
FileToContractsMap, CompactContractBytecode, FileToContractsMap,
}, },
files::{MappedArtifactFile, MappedArtifactFiles, MappedContract}, files::{MappedArtifactFile, MappedArtifactFiles, MappedContract},
ArtifactOutput, OutputContext, ArtifactId, ArtifactOutput, OutputContext,
}; };
use semver::Version; use semver::Version;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Deserializer, Serialize, Serializer};
use std::{collections::BTreeMap, ops::Deref, path::Path}; use std::{
collections::BTreeMap,
iter::FromIterator,
ops::{Deref, DerefMut},
path::Path,
};
use tracing::trace; use tracing::trace;
/// file -> [(contract name -> Contract + solc version)] /// file -> [(contract name -> Contract + solc version)]
@ -344,3 +349,54 @@ pub struct VersionedContract {
pub contract: Contract, pub contract: Contract,
pub version: Version, pub version: Version,
} }
/// A mapping of `ArtifactId` and their `CompactContractBytecode`
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct ArtifactContracts<T = CompactContractBytecode>(pub BTreeMap<ArtifactId, T>);
impl<T: Serialize> Serialize for ArtifactContracts<T> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.0.serialize(serializer)
}
}
impl<'de, T: Deserialize<'de>> Deserialize<'de> for ArtifactContracts<T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
Ok(Self(BTreeMap::<_, _>::deserialize(deserializer)?))
}
}
impl<T> Deref for ArtifactContracts<T> {
type Target = BTreeMap<ArtifactId, T>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<T> DerefMut for ArtifactContracts<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<V, C: Into<V>> FromIterator<(ArtifactId, C)> for ArtifactContracts<V> {
fn from_iter<T: IntoIterator<Item = (ArtifactId, C)>>(iter: T) -> Self {
Self(iter.into_iter().map(|(k, v)| (k, v.into())).collect())
}
}
impl<T> IntoIterator for ArtifactContracts<T> {
type Item = (ArtifactId, T);
type IntoIter = std::collections::btree_map::IntoIter<ArtifactId, T>;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

View File

@ -361,9 +361,10 @@ impl ProjectCompileOutput<ConfigurableArtifacts> {
/// use std::collections::btree_map::BTreeMap; /// use std::collections::btree_map::BTreeMap;
/// use ethers_solc::artifacts::contract::CompactContractBytecode; /// use ethers_solc::artifacts::contract::CompactContractBytecode;
/// use ethers_solc::{ArtifactId, Project}; /// use ethers_solc::{ArtifactId, Project};
/// use ethers_solc::contracts::ArtifactContracts;
/// ///
/// let project = Project::builder().build().unwrap(); /// let project = Project::builder().build().unwrap();
/// let contracts: BTreeMap<ArtifactId, CompactContractBytecode> = project.compile().unwrap().into_contract_bytecodes().collect(); /// let contracts: ArtifactContracts = project.compile().unwrap().into_contract_bytecodes().collect();
/// ``` /// ```
pub fn into_contract_bytecodes( pub fn into_contract_bytecodes(
self, self,